zasm: add support for subtractions in expressions
This commit is contained in:
parent
67803f6cb5
commit
1010e8372c
@ -2,34 +2,69 @@
|
|||||||
; We expect (HL) to be disposable: we mutate it to avoid having to make a copy.
|
; We expect (HL) to be disposable: we mutate it to avoid having to make a copy.
|
||||||
; Sets Z on success, unset on error.
|
; Sets Z on success, unset on error.
|
||||||
parseExpr:
|
parseExpr:
|
||||||
|
push bc
|
||||||
|
push de
|
||||||
push hl
|
push hl
|
||||||
ld a, '+'
|
ld a, '+'
|
||||||
call JUMP_FINDCHAR
|
call JUMP_FINDCHAR
|
||||||
|
jr z, .hasExpr
|
||||||
|
pop hl
|
||||||
|
push hl
|
||||||
|
ld a, '-'
|
||||||
|
call JUMP_FINDCHAR
|
||||||
jr nz, .noExpr
|
jr nz, .noExpr
|
||||||
; Alright, we have a + and we're pointing at it. Let's advance HL and
|
ld c, '-'
|
||||||
|
jr .hasExpr
|
||||||
|
.hasPlus:
|
||||||
|
ld c, '+'
|
||||||
|
jr .hasExpr
|
||||||
|
.hasExpr:
|
||||||
|
; Alright, we have a +/ and we're pointing at it. Let's advance HL and
|
||||||
; recurse. But first, let's change this + into a null char. It will be
|
; recurse. But first, let's change this + into a null char. It will be
|
||||||
; handy later.
|
; handy later.
|
||||||
xor a
|
xor a
|
||||||
ld (hl), a ; + changed to \0
|
ld (hl), a ; + changed to \0
|
||||||
|
|
||||||
inc hl
|
inc hl
|
||||||
call parseExpr
|
pop de ; we pop out the HL we pushed earlier into DE
|
||||||
; Whether parseExpr was successful or not, we pop hl right now
|
; That's our original beginning of string.
|
||||||
pop hl
|
call _applyExprToHL
|
||||||
ret nz ; return immediately if error
|
|
||||||
; Now we have parsed everything to the right and we have its result in
|
|
||||||
; IX. the pop hl brought us back to the beginning of the string. Our
|
|
||||||
; + was changed to a 0. Let's save IX somewhere and parse this.
|
|
||||||
push de
|
|
||||||
ld d, ixh
|
|
||||||
ld e, ixl
|
|
||||||
call parseNumberOrSymbol
|
|
||||||
jr nz, .end ; error
|
|
||||||
; Good! let's do the math!
|
|
||||||
add ix, de
|
|
||||||
.end:
|
|
||||||
pop de
|
pop de
|
||||||
|
pop bc
|
||||||
ret
|
ret
|
||||||
|
|
||||||
.noExpr:
|
.noExpr:
|
||||||
pop hl
|
pop hl
|
||||||
|
pop de
|
||||||
|
pop bc
|
||||||
jp parseNumberOrSymbol
|
jp parseNumberOrSymbol
|
||||||
|
|
||||||
|
; Parse number or symbol in (DE) and expression in (HL) and apply operator
|
||||||
|
; specified in C to them.
|
||||||
|
_applyExprToHL:
|
||||||
|
call parseExpr
|
||||||
|
ret nz ; return immediately if error
|
||||||
|
; Now we have parsed everything to the right and we have its result in
|
||||||
|
; IX. What we need to do now is parseNumberOrSymbol on (DE) and apply
|
||||||
|
; operator. Let's save IX somewhere and parse this.
|
||||||
|
ex hl, de
|
||||||
|
push ix
|
||||||
|
pop de
|
||||||
|
call parseNumberOrSymbol
|
||||||
|
ret nz ; error
|
||||||
|
; Good! let's do the math! IX has our left part, DE has our right one.
|
||||||
|
ld a, c ; restore operator
|
||||||
|
cp '-'
|
||||||
|
jr z, .sub
|
||||||
|
; addition
|
||||||
|
add ix, de
|
||||||
|
jr .end
|
||||||
|
.sub:
|
||||||
|
push ix
|
||||||
|
pop hl
|
||||||
|
sbc hl, de
|
||||||
|
push hl
|
||||||
|
pop ix
|
||||||
|
.end:
|
||||||
|
cp a ; ensure Z
|
||||||
|
ret
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
; test expressions
|
; test expressions
|
||||||
ld a, 'A'+3
|
ld a, 'A'+3
|
||||||
|
ld a, 'A'-0x20
|
||||||
|
Loading…
Reference in New Issue
Block a user