1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-12-04 04:48:06 +11:00

zasm: add support for "0b" literals

This commit is contained in:
Virgil Dupras 2019-05-17 10:34:01 -04:00
parent 0f5fab23e9
commit 92a119105d
2 changed files with 83 additions and 1 deletions

View File

@ -93,7 +93,7 @@ parseHexadecimal:
pop hl
ret
; Sets Z if (HL) has a '0x' or '0X' prefix.
; Sets Z if (HL) has a '0x' prefix.
hasHexPrefix:
ld a, (hl)
cp '0'
@ -105,6 +105,62 @@ hasHexPrefix:
pop hl
ret
; Parse string at (HL) as a binary value (0b010101) and return value in IXL.
; Clears IXH.
; Sets Z on success.
parseBinaryLiteral:
call hasBinPrefix
ret nz
push bc
push hl
xor a
ld ixh, a
inc hl ; get rid of "0x"
inc hl
call strlen
or a
jr z, .error ; empty, error
cp 9
jr nc, .error ; >= 9, too long
; We have a string of 8 or less chars. What we'll do is that for each
; char, we rotate left and set the LSB according to whether we have '0'
; or '1'. Error out on anything else. C is our stored result.
ld b, a ; we loop for "strlen" times
ld c, 0 ; our stored result
.loop:
rlc c
ld a, (hl)
inc hl
cp '0'
jr z, .nobit ; no bit to set
cp '1'
jr nz, .error ; not 0 or 1
; We have a bit to set
inc c
.nobit:
djnz .loop
ld ixl, c
cp a ; ensure Z
jr .end
.error:
call unsetZ
.end:
pop hl
pop bc
ret
; Sets Z if (HL) has a '0b' prefix.
hasBinPrefix:
ld a, (hl)
cp '0'
ret nz
push hl
inc hl
ld a, (hl)
cp 'b'
pop hl
ret
; Parse string at (HL) and, if it is a char literal, sets Z and return
; corresponding value in IXL. Clears IXH.
;
@ -147,6 +203,8 @@ parseLiteral:
ret z
call parseHexadecimal
ret z
call parseBinaryLiteral
ret z
jp parseDecimal
; Parse string in (HL) and return its numerical value whether its a number

View File

@ -16,6 +16,8 @@ testNum: .db 1
s99: .db "99", 0
s0x99: .db "0x99", 0
s0b0101: .db "0b0101", 0
s0b01010101: .db "0b01010101", 0
sFoo: .db "Foo", 0
test:
@ -38,6 +40,28 @@ test:
jp z, fail
call nexttest
ld hl, s0b0101
call parseLiteral
jp nz, fail
ld a, ixh
or a
jp nz, fail
ld a, ixl
cp 0b0101
jp nz, fail
call nexttest
ld hl, s0b01010101
call parseLiteral
jp nz, fail
ld a, ixh
or a
jp nz, fail
ld a, ixl
cp 0b01010101
jp nz, fail
call nexttest
; success
xor a
halt