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 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/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; } 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 diff --git a/sectorc.s b/sectorc.s index b803d3f..be65cba 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 @@ -103,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 @@ -114,7 +114,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 @@ -148,7 +149,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: @@ -246,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: @@ -258,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 @@ -305,7 +312,7 @@ emit_var: ;; [fall-through] emit_tok: - mov ax,bx + xchg ax,bx stosw ; emit token value jmp tok_next ; [tail-call] @@ -345,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 "/*" @@ -403,6 +410,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