Only allow 0o prefix for octal, not just 0

This commit is contained in:
PleegWat
2026-09-03 16:45:04 +02:00
parent b4af75a18e
commit f18199c5d0
2 changed files with 33 additions and 33 deletions
+26 -27
View File
@@ -1,4 +1,4 @@
; Internal convention:
; Internal register assignments:
; r1 - Partially parsed output integer
; r2 - Parsing position
; r3 - Character being parsed
@@ -13,6 +13,7 @@
; Clobbers:
; flags
; r3 - last character read
; r4 - Negative marker
; Note: Unless the input is "0", tail-calls into a base-specific specialization.
pub atoi:
mov r2, r1
@@ -23,26 +24,20 @@ cmp r3, 0x2D ; '-'
jne atoi_positive
sub r4, zr, 1 ; Set r4 to -1
add r2, r2, 1
load_8 r3, [r2]
atoi_positive:
cmp r3, 0x30 ; '0'
jne atoi_dec_loop ; No prefix, parse decimal
add r2, r2, 1
load_8 r3, [r2] ; From "boxBOX", or an octal digit
cmp r3, 0x30 ; '0'
jl atoi_done
cmp r3, 0x37 ; '7'
jle atoi_oct_loop ; Octal with just '0' prefix
add r2, r2, 1 ; 2-byte prefix
and r3, r3, 0x5F ; to uppercase
cmp r3, 0x42 ; 'B'
load_16 r3, [r2] ; 2-byte prefix "0b", "0o", "0x", etc.
or r3, r3, 0x20 ; 2nd char to lower case
add r2, r2, 2
cmp r3, 0x3062 ; "0b"
je atoi_bin_loop
cmp r3, 0x4F ; 'O'
cmp r3, 0x306F ; "0o"
je atoi_oct_loop
cmp r3, 0x58 ; 'X'
cmp r3, 0x3078 ; "0x"
je atoi_hex_loop
sub r2, r2, 2 ; no matching prefix, move pointer back
jmp atoi_dec_loop
; FALLTHROUGH: not a valid prefix, but we did see a zero
; Epilogue
atoi_done:
add r1, r1, r4 ; If r4 is -1, negate r1. Else it's 0 and no effect.
xor r1, r1, r4
@@ -58,6 +53,7 @@ jmp r13
; Clobbers:
; flags
; r3 - last character read
; r4 - Negative marker
pub atoi_dec:
mov r2, r1
mov r1, 0
@@ -76,7 +72,7 @@ cmp r3, 0x39 ; '9'
jg atoi_done
add r2, r2, 1
sub r3, r3, 0x30 ; '0'
lsl flags, r1, 2
lsl flags, r1, 2 ; Use flags to help multiply by 10
add r1, r1, flags
lsl r1, r1, 1
add r1, r1, r3
@@ -91,6 +87,7 @@ jmp atoi_dec_loop
; Clobbers:
; flags
; r3 - last character read
; r4 - Negative marker
pub atoi_bin:
mov r2, r1
mov r1, 0
@@ -102,8 +99,8 @@ sub r4, zr, 1 ; Set r4 to -1
add r2, r2, 1
atoi_bin_positive:
load_16 r3, [r2] ; check for prefix
and r3, r3, 0xFF5F ; to uppercase
cmp r3, 0x3042 ; "0B"
or r3, r3, 0x20 ; 2nd char to lower case
cmp r3, 0x3062 ; "0b"
jne atoi_bin_loop
add r2, r2, 2
atoi_bin_loop:
@@ -127,6 +124,7 @@ jmp atoi_bin_loop
; Clobbers:
; flags
; r3 - last character read
; r4 - Negative marker
pub atoi_oct:
mov r2, r1
mov r1, 0
@@ -138,8 +136,8 @@ sub r4, zr, 1 ; Set r4 to -1
add r2, r2, 1
atoi_oct_positive:
load_16 r3, [r2] ; check for prefix
and r3, r3, 0xFF5F ; to uppercase
cmp r3, 0x304F ; "0O"
or r3, r3, 0x20 ; 2nd char to lower case
cmp r3, 0x306F ; "0o"
jne atoi_oct_loop
add r2, r2, 2
atoi_oct_loop:
@@ -163,6 +161,7 @@ jmp atoi_oct_loop
; Clobbers:
; flags
; r3 - last character read
; r4 - Negative marker
pub atoi_hex:
mov r2, r1
mov r1, 0
@@ -174,8 +173,8 @@ sub r4, zr, 1 ; Set r4 to -1
add r2, r2, 1
atoi_hex_positive:
load_16 r3, [r2] ; check for prefix
and r3, r3, 0xFF5F ; to uppercase
cmp r3, 0x3058 ; "0X"
or r3, r3, 0x20 ; 2nd char to lower case
cmp r3, 0x3078 ; "0x"
jne atoi_oct_loop
add r2, r2, 2
atoi_hex_loop:
@@ -191,13 +190,13 @@ add r1, r1, r3
jmp atoi_hex_loop
atoi_hex_letter:
and r3, r3, 0x5F ; to uppercase
cmp r3, 0x41 ; 'A'
or r3, r3, 0x20 ; to lower case
cmp r3, 0x61 ; 'a'
jl atoi_done
cmp r3, 0x46 ; 'F'
cmp r3, 0x66 ; 'f'
jg atoi_done
add r2, r2, 1
sub r3, r3, 0x37 ; 'A' - 10
sub r3, r3, 0x57 ; 'a' - 10
lsl r1, r1, 4
add r1, r1, r3
jmp atoi_hex_loop
+7 -6
View File
@@ -46,9 +46,10 @@ tests:
@0x1040 U32 42 U8 2 "42\0"
@0x1050 U32 67 U8 2 "67lol\0"
@0x1060 U32 0x69a U8 5 "0x69a\0"
@0x1070 U32 0o23 U8 3 "0239\0"
@0x1080 U32 0b1011 U8 6 "0b1011\0"
@0x1090 U32 0b10001 U8 7 "0b100012\0"
@0x10a0 U32 0xFFFFFFEB U8 3 "-21\0" ; Yuk
@0x10b0
end_of_tests:
@0x1070 U32 0x4B4 U8 5 "0x4B4\0"
@0x1080 U32 0o23 U8 4 "0o239\0"
@0x1090 U32 0b1011 U8 6 "0b1011\0"
@0x10a0 U32 0b10001 U8 7 "0b100012\0"
@0x10b0 U32 0xFFFFFFEB U8 3 "-21\0" ; Yuk
@0x10c0
end_of_tests: