a4_p0005
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* acl4のプログラムのページ0005
-(by [[K]], 2026.02.16)
** (1) 実行したい
-[[a4_p0004]]でソースコードをバイナリにするのが簡単だとわ...
** (2) p0005a.c [59行] : 即興で作った仮想マシン
#define a_Version 1
#include <acl4.c>
void myPut(Preprocessor_Put0 *w, VecChr *lin) // t0013b....
{
if (Preprocessor_isDirective(lin->p, lin->p + lin->n...
fprintf(stderr, "[err] %.*s", (int) lin->n, lin-...
else
VecChr_puts(w->dst, lin->p, lin->p + lin->n);
}
int main(int argc, const char **argv)
{
if (argc < 2) return 1;
Preprocessor pp[1]; Preprocessor_ini(pp); ...
pp->put = (void *) myPut; ...
Preprocessor_SourceFiles_addFile(pp->sfs, argv[1], s...
Preprocessor_SourceFiles_addFile(pp->sfs, "a4vm-asm-...
VecChr vc[1]; VecChr_ini(vc); pp->put0->dst = vc; ...
Preprocessor_main(pp); Preprocessor_din(pp); ...
Token0 t0[1]; Token0_ini1(t0);
t0->s = vc->p; t0->s1 = vc->p + vc->n;
Preprocessor_Eval ev[1]; ev->err = 0;
VecChr bin[1]; VecChr_ini(bin);
for (;;) {
intptr_t i = Preprocessor_eval(ev, t0, 0x7fff);
if (ev->err != 0) break;
VecChr_resizeDiff(_arg_ bin, 1); bin->p[bin->n ...
Token1_get(t0);
if (t0->c != ',') break;
}
int32_t *bp = (int32_t *) bin->p, bn = bin->n / size...
for (pc = 0; pc < bn; pc += 4) {
if (bp[pc] == 0x02)
lab[bp[pc + 1]] = pc + 4; // Lbl_T(t)命令の...
}
for (pc = 0;;) {
int32_t op = bp[pc], a = bp[pc + 1], b = bp[...
switch (op) {
case 0x01: reg[a] = b; ...
case 0x02: ...
case 0x03: reg[a] += reg[b]; ...
case 0x04: reg[a] += b; ...
case 0x05: if (reg[a] <= b) { pc = lab[c]; }...
case 0x06: if (reg[a] < b) { pc = lab[c]; }...
case 0x10: goto fin;
case 0x11: goto fin;
case 0x12: putchar(reg[a]); ...
case 0x13: putchar(a); ...
case 0x16: printf("%d", reg[a]); ...
}
}
fin:
VecChr_din4(vc, bin, 0, 0);
a_malloc_debugList(_arg);
return 0;
}
-適当に作った定義ファイル: a4vm-asm-p0005a.h (内容は仮...
#define R00 0
#define R01 1
#define R02 2
#define R03 3
#define L0000 0
#define L0001 1
#define L0002 2
#define L0003 3
#define _i8(i) (i)&0xff
#define _i16(i) _i8(i),_i8((i)>>8)
#define _i32(i) _i16(i),_i16((i)>>16)
#define _op(i) _i32(i)
#define _op128(i0,i1,i2,i3) _i32(i0),_i32(i1),_i32(i2),...
#define Lod_RI(r,i) _op128(0x01,r,i,0),
#define Lbl_T(t) _op128(0x02,t,0,0),
#define Add_RR(r,s) _op128(0x03,r,s,0),
#define Add_RI(r,i) _op128(0x04,r,i,0),
#define Jle_RIT(r,i,t) _op128(0x05,r,i,t),
#define Jlt_RIT(r,i,t) _op128(0x06,r,i,t),
#define Dbg_Ret_R(r) _op128(0x10,r,0,0),
#define Dbg_Ret_I(i) _op128(0x11,i,0,0),
#define Dbg_Putc_R(r) _op128(0x12,r,0,0),
#define Dbg_Putc_I(i) _op128(0x13,i,0,0),
#define Dbg_PutHex_R(r) _op128(0x14,r,0,0),
#define Dbg_PutHex_I(i) _op128(0x15,i,0,0),
#define Dbg_PutDec_R(r) _op128(0x16,r,0,0),
#define Dbg_PutDec_I(i) _op128(0x17,i,0,0),
** (3) 実行結果
-p0004a0.txt:
#define c R00 // 整数レジスタ
#define lp L0000 // ラベル
Lod_RI(c, 0x20)
Lbl_T(lp)
Dbg_Putc_R(c)
Add_RI(c, 1)
Jlt_RIT(c, 0x7f, lp)
Dbg_Putc_I('\n')
Dbg_Ret_I(0)
>p0005a p0004a0.txt
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVW...
--こんな感じでちゃんと動いています。
-p0005a0.txt:
#define i R00 // 整数レジスタ
#define s R01
#define lp L0000 // ラベル
Lod_RI(s, 0)
Lod_RI(i, 0)
Lbl_T(lp)
Add_RR(s, i)
Add_RI(i, 1)
Jle_RIT(i, 1000, lp)
Dbg_PutDec_R(s)
Dbg_Putc_I('\n')
Dbg_Ret_I(0)
>p0005a p0005a0.txt
500500
--これも問題なく動きました。
** (99) 更新履歴
-2026.02.16(月) 初版
終了行:
* acl4のプログラムのページ0005
-(by [[K]], 2026.02.16)
** (1) 実行したい
-[[a4_p0004]]でソースコードをバイナリにするのが簡単だとわ...
** (2) p0005a.c [59行] : 即興で作った仮想マシン
#define a_Version 1
#include <acl4.c>
void myPut(Preprocessor_Put0 *w, VecChr *lin) // t0013b....
{
if (Preprocessor_isDirective(lin->p, lin->p + lin->n...
fprintf(stderr, "[err] %.*s", (int) lin->n, lin-...
else
VecChr_puts(w->dst, lin->p, lin->p + lin->n);
}
int main(int argc, const char **argv)
{
if (argc < 2) return 1;
Preprocessor pp[1]; Preprocessor_ini(pp); ...
pp->put = (void *) myPut; ...
Preprocessor_SourceFiles_addFile(pp->sfs, argv[1], s...
Preprocessor_SourceFiles_addFile(pp->sfs, "a4vm-asm-...
VecChr vc[1]; VecChr_ini(vc); pp->put0->dst = vc; ...
Preprocessor_main(pp); Preprocessor_din(pp); ...
Token0 t0[1]; Token0_ini1(t0);
t0->s = vc->p; t0->s1 = vc->p + vc->n;
Preprocessor_Eval ev[1]; ev->err = 0;
VecChr bin[1]; VecChr_ini(bin);
for (;;) {
intptr_t i = Preprocessor_eval(ev, t0, 0x7fff);
if (ev->err != 0) break;
VecChr_resizeDiff(_arg_ bin, 1); bin->p[bin->n ...
Token1_get(t0);
if (t0->c != ',') break;
}
int32_t *bp = (int32_t *) bin->p, bn = bin->n / size...
for (pc = 0; pc < bn; pc += 4) {
if (bp[pc] == 0x02)
lab[bp[pc + 1]] = pc + 4; // Lbl_T(t)命令の...
}
for (pc = 0;;) {
int32_t op = bp[pc], a = bp[pc + 1], b = bp[...
switch (op) {
case 0x01: reg[a] = b; ...
case 0x02: ...
case 0x03: reg[a] += reg[b]; ...
case 0x04: reg[a] += b; ...
case 0x05: if (reg[a] <= b) { pc = lab[c]; }...
case 0x06: if (reg[a] < b) { pc = lab[c]; }...
case 0x10: goto fin;
case 0x11: goto fin;
case 0x12: putchar(reg[a]); ...
case 0x13: putchar(a); ...
case 0x16: printf("%d", reg[a]); ...
}
}
fin:
VecChr_din4(vc, bin, 0, 0);
a_malloc_debugList(_arg);
return 0;
}
-適当に作った定義ファイル: a4vm-asm-p0005a.h (内容は仮...
#define R00 0
#define R01 1
#define R02 2
#define R03 3
#define L0000 0
#define L0001 1
#define L0002 2
#define L0003 3
#define _i8(i) (i)&0xff
#define _i16(i) _i8(i),_i8((i)>>8)
#define _i32(i) _i16(i),_i16((i)>>16)
#define _op(i) _i32(i)
#define _op128(i0,i1,i2,i3) _i32(i0),_i32(i1),_i32(i2),...
#define Lod_RI(r,i) _op128(0x01,r,i,0),
#define Lbl_T(t) _op128(0x02,t,0,0),
#define Add_RR(r,s) _op128(0x03,r,s,0),
#define Add_RI(r,i) _op128(0x04,r,i,0),
#define Jle_RIT(r,i,t) _op128(0x05,r,i,t),
#define Jlt_RIT(r,i,t) _op128(0x06,r,i,t),
#define Dbg_Ret_R(r) _op128(0x10,r,0,0),
#define Dbg_Ret_I(i) _op128(0x11,i,0,0),
#define Dbg_Putc_R(r) _op128(0x12,r,0,0),
#define Dbg_Putc_I(i) _op128(0x13,i,0,0),
#define Dbg_PutHex_R(r) _op128(0x14,r,0,0),
#define Dbg_PutHex_I(i) _op128(0x15,i,0,0),
#define Dbg_PutDec_R(r) _op128(0x16,r,0,0),
#define Dbg_PutDec_I(i) _op128(0x17,i,0,0),
** (3) 実行結果
-p0004a0.txt:
#define c R00 // 整数レジスタ
#define lp L0000 // ラベル
Lod_RI(c, 0x20)
Lbl_T(lp)
Dbg_Putc_R(c)
Add_RI(c, 1)
Jlt_RIT(c, 0x7f, lp)
Dbg_Putc_I('\n')
Dbg_Ret_I(0)
>p0005a p0004a0.txt
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVW...
--こんな感じでちゃんと動いています。
-p0005a0.txt:
#define i R00 // 整数レジスタ
#define s R01
#define lp L0000 // ラベル
Lod_RI(s, 0)
Lod_RI(i, 0)
Lbl_T(lp)
Add_RR(s, i)
Add_RI(i, 1)
Jle_RIT(i, 1000, lp)
Dbg_PutDec_R(s)
Dbg_Putc_I('\n')
Dbg_Ret_I(0)
>p0005a p0005a0.txt
500500
--これも問題なく動きました。
** (99) 更新履歴
-2026.02.16(月) 初版
ページ名: