diff --git a/apps/zasm/parse_z.asm b/apps/zasm/parse_z.asm index e42eeaf..3163409 100644 --- a/apps/zasm/parse_z.asm +++ b/apps/zasm/parse_z.asm @@ -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 diff --git a/tools/tests/unit/test_parse_z.asm b/tools/tests/unit/test_parse_z.asm index a605228..17e2541 100644 --- a/tools/tests/unit/test_parse_z.asm +++ b/tools/tests/unit/test_parse_z.asm @@ -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