From 6e714875dc56488b51f47672524b7abc6d44abbb Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Thu, 14 Nov 2019 21:16:36 -0500 Subject: [PATCH] zasm: Constants now override labels at all times Will be important for a mega-commit I'm preparing. --- apps/zasm/README.md | 12 +++++++----- apps/zasm/symbol.asm | 14 +++++++------- tools/tests/unit/test_core.asm | 10 ++++++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/apps/zasm/README.md b/apps/zasm/README.md index 0d880ae..1bc77b2 100644 --- a/apps/zasm/README.md +++ b/apps/zasm/README.md @@ -76,6 +76,12 @@ forward-reference labels. However, they *cannot* forward-reference other constants. +When defining a constant, if the symbol specified has already been defined, no +error occur and the first value defined stays intact. This allows for "user +override" of programs. + +It's also important to note that constants always override labels, regardless +of declaration order. ## Expressions @@ -119,12 +125,8 @@ allowed. An included file cannot have an `.inc` directive. **.equ**: Binds a symbol named after the first parameter to the value of the expression written as the second parameter. Example: - `.equ foo 0x42+'A'` + `.equ foo 0x42+'A'`. See "Constants" above. -If the symbol specified has already been defined, no error occur and -the first value defined stays intact. This allows for "user override" -of programs. - **.fill**: Outputs the number of null bytes specified by its argument, an expression. Often used with `$` to fill our binary up to a certain offset. For example, if we want to place an instruction exactly at diff --git a/apps/zasm/symbol.asm b/apps/zasm/symbol.asm index 1504bd3..08f4312 100644 --- a/apps/zasm/symbol.asm +++ b/apps/zasm/symbol.asm @@ -112,11 +112,11 @@ symRegister: push de ; --> lvl 3 ld d, 0 ld e, c - add hl, de ; if carry set here, sbc will carry too + add hl, de ; if carry set here, sbc will carry too ld e, (ix+2) ; DE --> pointer to record list, which is also ld d, (ix+3) ; the end of names pool ; DE --> names end - + sbc hl, de ; compares hl and de destructively pop de ; <-- lvl 3 pop hl ; <-- lvl 2 @@ -192,11 +192,11 @@ _symFind: jr z, .end ; match! Z already set, IY and HL placed. .skip: ; ok, next! - + push de ; --> lvl 1 ld de, 0x0003 add iy, de ; faster and shorter than three inc's - ld e, (iy-3) ; offset is also compulsory, so no extra bytes used + ld e, (iy-3) ; offset is also compulsory, so no extra bytes used ; (iy-3) holds the name length of the string just processed add hl, de ; advance HL by (iy-3) characters pop de ; <-- lvl 1 @@ -219,13 +219,13 @@ symFindVal: push ix call symIsLabelLocal jr z, .local - ; global. Let's try labels first, then consts + ; global. Let's try consts first, then symbols push hl ; --> lvl 1. we'll need it again if not found. - ld ix, SYM_GLOBAL_REGISTRY + ld ix, SYM_CONST_REGISTRY call _symFind pop hl ; <-- lvl 1 jr z, .found - ld ix, SYM_CONST_REGISTRY + ld ix, SYM_GLOBAL_REGISTRY call _symFind jr nz, .end .found: diff --git a/tools/tests/unit/test_core.asm b/tools/tests/unit/test_core.asm index a0a7eba..0c4fcd0 100644 --- a/tools/tests/unit/test_core.asm +++ b/tools/tests/unit/test_core.asm @@ -2,8 +2,11 @@ jp test .inc "core.asm" +dummyLabel: testNum: .db 1 +.equ dummyLabel 0x42 + test: ld hl, 0xffff ld sp, hl @@ -30,6 +33,13 @@ test: jp p, fail ; negative call nexttest + ; Test that .equ can override label + ld a, 0x42 + ld hl, dummyLabel + cp l + jp nz, fail + call nexttest + ; *** cpHLDE *** ld hl, 0x42 ld de, 0x42