diff --git a/apps/zasm/instr.asm b/apps/zasm/instr.asm index 578bf96..c8abaaf 100644 --- a/apps/zasm/instr.asm +++ b/apps/zasm/instr.asm @@ -57,7 +57,6 @@ I_SBC .equ 0x2f I_SCF .equ 0x30 I_SUB .equ 0x31 I_XOR .equ 0x32 -I_BAD .equ 0xff ; Checks whether A is 'N' or 'M' checkNOrM: @@ -77,27 +76,15 @@ checknmxy: cp 'y' ret -; Reads instruction mnemonic in (HL) and returns the corresponding ID (I_*) -; in A. I_BAD if there's no match. +; Reads string in (HL) and returns the corresponding ID (I_*) in A. Sets Z if +; there's a match. getInstID: push bc push de ld b, I_XOR+1 ; I_XOR is the last + ld c, 4 ld de, instrNames -.loop: - ld a, 4 - call JUMP_STRNCMP - ld a, 4 - call JUMP_ADDDE - jr z, .match - djnz .loop - ; no match - ld a, I_BAD - jr .end -.match: - ld a, I_XOR+1 - sub b -.end: + call findStringInList pop de pop bc ret @@ -788,7 +775,7 @@ parseTokens: push hl push de ld a, (tokInstr) - cp I_BAD + cp TOK_BAD jr z, .error ; for now, we treat blank lines as errors ld hl, tokArg1 ld de, curArg1 diff --git a/apps/zasm/tok.asm b/apps/zasm/tok.asm index 606eb58..00c74ff 100644 --- a/apps/zasm/tok.asm +++ b/apps/zasm/tok.asm @@ -5,6 +5,10 @@ ; *** Requirements *** ; JUMP_UPCASE +; *** Consts *** +TOK_INSTR .equ 0x01 +TOK_BAD .equ 0xff + ; *** Code *** ; Parse line in (HL) and read the next token in (DE). For now, it only supports ; instructions. Arguments must be tokenized with the appropriate specialized @@ -19,6 +23,10 @@ tokenize: ex hl, de call getInstID ex hl, de + jr z, .match + ; no match + ld a, TOK_BAD +.match: ld (de), a ret diff --git a/apps/zasm/util.asm b/apps/zasm/util.asm index 3af4ee2..7cd4508 100644 --- a/apps/zasm/util.asm +++ b/apps/zasm/util.asm @@ -65,4 +65,30 @@ enterParens: call JUMP_UNSETZ ret - +; Find string (HL) in string list (DE) of size B. Each string is C bytes wide. +; Returns the index of the found string. Sets Z if found, unsets Z if not found. +findStringInList: + push de + push bc +.loop: + ld a, c + call JUMP_STRNCMP + ld a, c + call JUMP_ADDDE + jr z, .match + djnz .loop + ; no match, Z is unset + pop bc + pop de + ret +.match: + ; Now, we want the index of our string, which is equal to our initial B + ; minus our current B. To get this, we have to play with our registers + ; and stack a bit. + ld d, b + pop bc + ld a, b + sub d + pop de + cp a ; ensure Z + ret