From 8ddb3bd53b7064548fc726a278599fca5fd7b6c4 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Tue, 13 Jun 2023 14:49:49 +0300 Subject: [PATCH 1/8] "99 Bottles of Beer" song added --- examples/99.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 examples/99.c diff --git a/examples/99.c b/examples/99.c new file mode 100644 index 0000000..706f037 --- /dev/null +++ b/examples/99.c @@ -0,0 +1,73 @@ +int c; // input +int shift; int x; + +void x() { if( c == ( x >> 8 ) ){ print_ch = x & 255; } } +void p() { print_ch = c; print_char(); } + +void c2c() { + c = c + shift; + x = 288; x(); x = 613; x(); x = 879; x(); x = 1140; x(); x = 1388; x(); + x = 1634; x(); x = 1902; x(); x = 2145; x(); x = 2419; x(); x = 2674; x(); + x = 2918; x(); x = 3191; x(); x = 3338; x(); x = 3684; x(); x = 4200; x(); + x = 4396; x(); x = 4654; x(); x = 4981; x(); x = 5204; x(); x = 5481; x(); + x = 5739; x(); x = 6000; x(); x = 6253; x(); x = 6471; x(); x = 6734; x(); + x = 7033; x(); + + if( c != 15 ){ print_char(); } + shift = 0; if( c == 15 ){ shift = 15; } +} + +int s; // input +void ps() { + while( s ){ + c = s & 15; + c2c(); + s = s >> 4; + if( s == ( 0 - 1 ) ){ s = 0; } // signed workaround + } +} + +void bottle() { s = 17249; ps(); s = 596; ps(); } +void of_beer() { s = 6961; ps(); s = 41510; ps(); } +void on_wall() { of_beer(); s = 5937; ps(); s = 8692; ps(); s = 22721; ps(); print_char(); } +void o_more() { s = 40723; ps(); s = 675; ps(); } + +int b; // input +void bottle_b() { + if( b == 0 ){ s = 55; ps(); } + if( b > 0 ){ print_num = b; print_u16(); } + bottle(); + if( b != 1 ){ c = 115; p(); } // s +} + +void main() { + shift = 0; b = 99; + + while( b > 0 ){ + bottle_b(); on_wall(); + + s = 303; ps(); /* ', ' */ bottle_b(); of_beer(); + c = 46; p(); // '.' + print_newline(); + b = b - 1; + + s = 2143; ps(); s = 4735; ps(); s = 4723; ps(); s = 31806; ps(); + s = 59265; ps(); s = 35057; ps(); s = 409; ps(); s = 5231; ps(); + s = 936; ps(); s = 59215; ps(); s = 303; ps(); + + bottle_b(); on_wall(); c = 46; p(); // '.' + print_newline(); print_newline(); + } + + c = 78; p(); /* 'N' */ o_more(); bottle(); c = 115; p(); // 's' + on_wall(); + s = 28975; ps(); /* ', n' */ o_more(); bottle(); c = 115; p(); // 's' + on_wall(); + c = 46; p(); // '.' + print_newline(); + + s = 5039; ps(); s = 16692; ps(); s = 4639; ps(); s = 41801; ps(); + s = 30738; ps(); s = 1566; ps(); s = 53071; ps(); s = 913; ps(); + s = 4767; ps(); s = 41887; ps(); s = 4850; ps(); + b = 99; bottle_b(); on_wall(); c = 46; p(); // '.' +} \ No newline at end of file From b064246aed178e97ab0e70f47680341b13514f12 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Thu, 15 Jun 2023 20:28:06 +0300 Subject: [PATCH 2/8] Division operation added --- lint/lint.c | 2 ++ sectorc.s | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lint/lint.c b/lint/lint.c index 206075f..752035a 100644 --- a/lint/lint.c +++ b/lint/lint.c @@ -47,6 +47,7 @@ typedef int64_t i64; _( TOK_SUB, "-", 65533 )\ _( TOK_ADD, "+", 65531 )\ _( TOK_MUL, "*", 65530 )\ + _( TOK_DIV, "/", 65535 )\ _( TOK_OR, "|", 76 )\ _( TOK_XOR, "^", 46 )\ _( TOK_SHL, "<<", 132 )\ @@ -405,6 +406,7 @@ static bool tok_oper_is(void) tok_kw_is(TOK_ADD) || tok_kw_is(TOK_SUB) || tok_kw_is(TOK_MUL) || + tok_kw_is(TOK_DIV) || tok_kw_is(TOK_ADDR) || // "AND" in this context tok_kw_is(TOK_OR) || tok_kw_is(TOK_XOR) || diff --git a/sectorc.s b/sectorc.s index b803d3f..a87902e 100644 --- a/sectorc.s +++ b/sectorc.s @@ -24,6 +24,7 @@ %define TOK_SUB 65533 %define TOK_ADD 65531 %define TOK_MUL 65530 +%define TOK_DIV 65535 %define TOK_AND 65526 %define TOK_OR 76 %define TOK_XOR 46 @@ -403,6 +404,7 @@ binary_oper_tbl: dw TOK_ADD,0xc103 ; add ax,cx dw TOK_SUB,0xc12b ; sub ax,cx dw TOK_MUL,0xe1f7 ; mul ax,cx + dw TOK_DIV,0xf1f7 ; div ax,cx dw TOK_AND,0xc123 ; and ax,cx dw TOK_OR,0xc10b ; or ax,cx dw TOK_XOR,0xc133 ; xor ax,cx From e2ea25a6d25ca50230b3cd194c19043834de70bf Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Thu, 15 Jun 2023 22:23:50 +0300 Subject: [PATCH 3/8] Remove warning --- run_raw.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run_raw.sh b/run_raw.sh index 8230d6e..89843d6 100755 --- a/run_raw.sh +++ b/run_raw.sh @@ -8,4 +8,4 @@ if [ "$#" -lt 1 ]; then exit 1 fi -cat $@ | qemu-system-i386 -hda build/sectorc.bin -serial stdio -audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0 +cat $@ | qemu-system-i386 -drive file=build/sectorc.bin,format=raw -serial stdio -audiodev coreaudio,id=audio0 -machine pcspk-audiodev=audio0 From c3f63c1dccdf1358162a315e2dfe03cda1fa68a6 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Thu, 15 Jun 2023 22:31:29 +0300 Subject: [PATCH 4/8] Patch dump_code_segment_and_shutdown function --- rt/lib.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rt/lib.c b/rt/lib.c index 52facd2..7d7509f 100644 --- a/rt/lib.c +++ b/rt/lib.c @@ -239,10 +239,8 @@ void dump_code_segment_and_shutdown() i = 0; while( i < 8192 ){ /* Just assuming 8K is enough.. might not be true */ - // (put "i" in ax); mov si,ax; mov ax,cs:[si]; mov [&a],ax - i = i; asm 137; asm 198; asm 46; asm 139; asm 4; asm 137; asm 133; asm 98; asm 0; - - print_ch = a; + // (put "i" in ax); mov si,ax; mov ax,cs:[si]; mov [&print_ch],ax + i = i; asm 137; asm 198; asm 46; asm 139; asm 4; asm 163; asm 100; asm 132; print_char(); i = i + 1; } From 3420e7c6f03c3939a097e04d2dd4c48491f58641 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Fri, 16 Jun 2023 21:28:52 +0300 Subject: [PATCH 5/8] -2 bytes --- sectorc.s | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sectorc.s b/sectorc.s index a87902e..3109387 100644 --- a/sectorc.s +++ b/sectorc.s @@ -115,7 +115,8 @@ compile_stmts: mov ax,[bx] ; load function offset from symbol-table sub ax,di ; compute relative to this location: "dest - cur - 2" - sub ax,2 + dec ax + dec ax stosw ; emit target jmp compile_stmts_tok_next2 ; loop to compile next statement @@ -149,7 +150,8 @@ _patch_back: stosb pop ax ; restore loop start location sub ax,di ; compute relative to this location: "dest - cur - 2" - sub ax,2 + dec ax + dec ax stosw ; emit target ;; [fall-through] _patch_fwd: From 6d2e927072c4416998de3b6cbb70802e942616c2 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Fri, 16 Jun 2023 21:33:57 +0300 Subject: [PATCH 6/8] -2 bytes --- sectorc.s | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sectorc.s b/sectorc.s index 3109387..7ffac74 100644 --- a/sectorc.s +++ b/sectorc.s @@ -104,8 +104,7 @@ compile_stmts_tok_next2: compile_stmts_tok_next: call tok_next compile_stmts: - mov ax,bx - cmp ax,TOK_BLK_END ; if we reach '}' then return + cmp bx,TOK_BLK_END ; if we reach '}' then return je return test dh,dh ; if dh is 0, it's not a call From 6d8bb6bfaf4aaa65af8d0e2e6e3c1e23821e7e52 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Sat, 17 Jun 2023 22:02:23 +0300 Subject: [PATCH 7/8] Added naccounted register bx --- sectorc.s | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sectorc.s b/sectorc.s index 7ffac74..9d564fe 100644 --- a/sectorc.s +++ b/sectorc.s @@ -248,6 +248,11 @@ _found: stosw ; emit pop bx ; restore 16-bit of machine-code + cmp bx,0xf1f7 ; detect the special case for division opcode + jne check_cmp_op + mov ax, 0xd231 ; code for "xor dx,dx" + stosw +check_cmp_op: cmp bh,0xc0 ; detect the special case for comparison ops jne emit_op emit_cmp_op: @@ -260,7 +265,7 @@ emit_cmp_op: ;; [fall-through] emit_op: - mov ax,bx + xchg ax,bx stosw ; emit machine code for op pop ds ret From ce99f8449275646151b3d96425b9a8dba028e611 Mon Sep 17 00:00:00 2001 From: Evgeny Stepanischev Date: Sat, 17 Jun 2023 22:08:29 +0300 Subject: [PATCH 8/8] -2 bytes --- sectorc.s | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sectorc.s b/sectorc.s index 9d564fe..be65cba 100644 --- a/sectorc.s +++ b/sectorc.s @@ -312,7 +312,7 @@ emit_var: ;; [fall-through] emit_tok: - mov ax,bx + xchg ax,bx stosw ; emit token value jmp tok_next ; [tail-call] @@ -352,7 +352,7 @@ _nextch: jmp _nextch ; [loop] _done: - mov ax,cx + xchg ax,cx cmp ax,0x2f2f ; check for single-line comment "//" je _comment_double_slash cmp ax,0x2f2a ; check for multi-line comment "/*"