lib/parse: make parseBinaryLiteral "tail" HL

This commit is contained in:
Virgil Dupras 2019-12-30 13:05:21 -05:00
parent 289037a3dd
commit 73a5275b1e
1 changed files with 14 additions and 28 deletions

View File

@ -135,40 +135,24 @@ parseHexadecimal:
; Parse string at (HL) as a binary value (010101) without the "0b" prefix and ; Parse string at (HL) as a binary value (010101) without the "0b" prefix and
; return value in E. D is always zero. ; return value in E. D is always zero.
; HL is advanced to the character following the last successfully read char.
; Sets Z on success. ; Sets Z on success.
parseBinaryLiteral: parseBinaryLiteral:
push bc ld de, 0
push 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: .loop:
rlc c
ld a, (hl) ld a, (hl)
add a, 0xff-'1'
sub 0xff-1
jr c, .end
rl e
add a, e
ld e, a
jp c, unsetZ ; overflow
inc hl inc hl
cp '0' jr .loop
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 e, c
cp a ; ensure Z
jr .end
.error:
call unsetZ
.end: .end:
pop hl ; HL is properly set
pop bc xor a ; ensure Z
ret ret
; Parses the string at (HL) and returns the 16-bit value in DE. The string ; Parses the string at (HL) and returns the 16-bit value in DE. The string
@ -235,5 +219,7 @@ parseLiteral:
jr .hexOrBinEnd jr .hexOrBinEnd
.bin: .bin:
push hl
call parseBinaryLiteral call parseBinaryLiteral
pop hl
jr .hexOrBinEnd jr .hexOrBinEnd