From a03c5ac700c34794c332aa2c558979a1368d2851 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 22 Nov 2019 17:16:51 -0500 Subject: [PATCH] lib/expr: add bitwise operators --- apps/README.md | 6 +++++- apps/lib/expr.asm | 43 +++++++++++++++++++++++++++++++++++++++++- tools/tests/unit/test_expr.asm | 15 +++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/apps/README.md b/apps/README.md index 6265bd6..3623acc 100644 --- a/apps/README.md +++ b/apps/README.md @@ -90,7 +90,11 @@ this way, it's going to mess with the parser. ### Expressions An expression is a bunch of literals or symbols assembled by operators. -Supported operators are `+`, `-`, `*`, `/` and `%` (modulo). No parenthesis yet. +Supported operators are `+`, `-`, `*`, `/`, `%` (modulo), `&` (bitwise and), +`|` (bitwise or) and `^` (bitwise xor). Bitwise operator always operate on +the whole 16-bits. + +There is no parenthesis support yet. Symbols have a different meaning depending on the application. In zasm, it's labels and constants. In basic, it's variables. diff --git a/apps/lib/expr.asm b/apps/lib/expr.asm index 1816e3f..982f3fe 100644 --- a/apps/lib/expr.asm +++ b/apps/lib/expr.asm @@ -117,6 +117,12 @@ exprTbl: .dw .div .db '%' .dw .mod + .db '&' + .dw .and + .db 0x7c ; '|' + .dw .or + .db '^' + .dw .xor .db 0 ; end of table .plus: @@ -155,5 +161,40 @@ exprTbl: .mod: call .div - push hl \ pop ix + push hl \ pop ix + ret + +.and: + push ix \ pop hl + ld a, h + and d + ld h, a + ld a, l + and e + ld l, a + push hl \ pop ix + cp a ; ensure Z + ret +.or: + push ix \ pop hl + ld a, h + or d + ld h, a + ld a, l + or e + ld l, a + push hl \ pop ix + cp a ; ensure Z + ret + +.xor: + push ix \ pop hl + ld a, h + xor d + ld h, a + ld a, l + xor e + ld l, a + push hl \ pop ix + cp a ; ensure Z ret diff --git a/tools/tests/unit/test_expr.asm b/tools/tests/unit/test_expr.asm index 1c11d75..f8c3342 100644 --- a/tools/tests/unit/test_expr.asm +++ b/tools/tests/unit/test_expr.asm @@ -139,6 +139,12 @@ testParseExpr: call .testEQ ld iy, .t2 call .testEQ + ld iy, .t3 + call .testEQ + ld iy, .t4 + call .testEQ + ld iy, .t5 + call .testEQ ret .testEQ: @@ -161,6 +167,15 @@ testParseExpr: .t2: .dw 1 .db "7%3", 0 +.t3: + .dw 0x0907 + .db "0x99f7&0x0f0f", 0 +.t4: + .dw 0x9fff + .db "0x99f7|0x0f0f", 0 +.t5: + .dw 0x96f8 + .db "0x99f7^0x0f0f", 0 nexttest: ld a, (testNum)