From 068e4327ec8876ff91b67cb114598b1bf56ba907 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 18 May 2019 14:51:11 -0400 Subject: [PATCH] zasm: fix false truncation error on "-" expressions --- apps/zasm/instr.asm | 16 +++++++++++++--- tools/tests/unit/test_expr.asm | 12 ++++++++++++ tools/tests/zasm/test1.asm | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/apps/zasm/instr.asm b/apps/zasm/instr.asm index b022b5f..356ab40 100644 --- a/apps/zasm/instr.asm +++ b/apps/zasm/instr.asm @@ -128,7 +128,7 @@ parseIXY: ; the value of that constant. parseArg: call strlen - cp 0 + or a ret z ; empty string? A already has our result: 0 push bc @@ -157,7 +157,7 @@ parseArg: call enterParens jr z, .withParens ; (HL) has no parens - call parseExpr + call .maybeParseExpr jr nz, .nomatch ; We have a proper number in no parens. Number in IX. ld a, 'N' @@ -181,7 +181,7 @@ parseArg: .notY: ld c, 'x' .parseNumberInParens: - call parseExpr + call .maybeParseExpr jr nz, .nomatch ; We have a proper number in parens. Number in IX ld a, c ; M, x, or y @@ -200,6 +200,16 @@ parseArg: pop bc ret +.maybeParseExpr: + ; Before we try to parse expr in (HL), first check if we're in first + ; pass if we are, skip parseExpr. Most of the time, that parse is + ; harmless, but in some cases it causes false failures. For example, + ; a "-" operator can cause is to falsely overflow and generate + ; truncation error. + call zasmIsFirstPass + ret z + jp parseExpr + ; Returns, with Z, whether A is a groupId isGroupId: cp 0xc ; max group id + 1 diff --git a/tools/tests/unit/test_expr.asm b/tools/tests/unit/test_expr.asm index 96572b3..7493d4c 100644 --- a/tools/tests/unit/test_expr.asm +++ b/tools/tests/unit/test_expr.asm @@ -19,6 +19,7 @@ s1: .db "2+2", 0 s2: .db "0x4001+0x22", 0 s3: .db "FOO+BAR", 0 s4: .db "3*3", 0 +s5: .db "FOO-3", 0 sFOO: .db "FOO", 0 sBAR: .db "BAR", 0 @@ -82,6 +83,17 @@ test: jp nz, fail call nexttest + ld hl, s5 + call parseExpr + jp nz, fail + ld a, ixh + cp 0x3f + jp nz, fail + ld a, ixl + cp 0xfd + jp nz, fail + call nexttest + ; success xor a halt diff --git a/tools/tests/zasm/test1.asm b/tools/tests/zasm/test1.asm index 96880f1..7636934 100644 --- a/tools/tests/zasm/test1.asm +++ b/tools/tests/zasm/test1.asm @@ -19,3 +19,5 @@ label2: .dw 0x42 ld (ix+1), l ld l, (ix+1) ld hl, 0x100 +.equ baz 0x20 + ld b, baz-3