diff --git a/README.md b/README.md index fc65417..59194c3 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ unary = deref identifier | "(" expr ")" | identifier | integer -op = "+" | "-" | "&" | "|" | "^" | "<<" | ">>" +op = "+" | "-" | "*" | "/" | "&" | "|" | "^" | "<<" | ">>" | "==" | "!=" | "<" | ">" | "<=" | ">=" ``` diff --git a/examples/test_division.c b/examples/test_division.c new file mode 100644 index 0000000..845324e --- /dev/null +++ b/examples/test_division.c @@ -0,0 +1,28 @@ +int x; +int y; +int result; + +void test_division() +{ + x = 100; + y = 3; + result = x / y; + + print_num = result; + print_i16(); + print_newline(); + + x = 50; + y = 5; + result = x / y; + + print_num = result; + print_i16(); + print_newline(); +} + +void _start() +{ + test_division(); + while( 1 ){ } +} diff --git a/lint/lint.c b/lint/lint.c index 12fcad8..b4f74aa 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 0379092..cb361c5 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 @@ -247,7 +248,18 @@ _found: pop bx ; restore 16-bit of machine-code cmp bh,0xc0 ; detect the special case for comparison ops + je emit_cmp_op + cmp bh,0xd0 ; detect division op jne emit_op + + ; division operator inline + mov ax,0xd231 ; code for "xor dx,dx" + stosw ; emit + mov ax,0xf1f7 ; code for "div cx" + stosw ; emit + pop ds + ret + emit_cmp_op: mov ax,0xc839 ; code for "cmp ax,cx" stosw ; emit @@ -258,7 +270,7 @@ emit_cmp_op: ;; [fall-through] emit_op: - mov ax,bx + xchg ax,bx ; 1 byte instead of mov ax,bx (2 bytes) stosw ; emit machine code for op pop ds ret @@ -402,7 +414,8 @@ getch_done: 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_MUL,0xe1f7 ; mul cx + dw TOK_DIV,0xd0f1 ; div cx (marker: 0xd0, low byte is F1 for "div cx") dw TOK_AND,0xc123 ; and ax,cx dw TOK_OR,0xc10b ; or ax,cx dw TOK_XOR,0xc133 ; xor ax,cx