Skip to content

Commit

Permalink
Add base 16 parser (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbirddog authored Jun 29, 2024
1 parent 4d2fed7 commit a3b7689
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
64 changes: 56 additions & 8 deletions to_number.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,28 @@ to_number:
mov cl, [_blue.word_len]
mov rsi, _blue.word

.loop:
xor ebx, ebx
mov bl, [_blue.base]

cmp bl, 10
je .base10_loop

cmp bl, 16
je .base16_loop

.failure:
mov eax, FAILURE
ret

.success:
mov eax, SUCCESS
ret

;
; decimal
;
.base10_loop:
lodsb

cmp al, 57
Expand All @@ -19,15 +40,42 @@ to_number:
js .failure

xchg eax, edi
mul [_blue.base]
mul ebx
xadd edi, eax

dec cl
jnz .loop
jnz .base10_loop
jmp .success

mov eax, SUCCESS
ret
;
; hex
;
.base16_loop:
lodsb

cmp al, 57
jg .base16_AF

sub al, 48
js .failure

.base16_mul:
xchg eax, edi
mul ebx
xadd edi, eax

dec cl
jnz .base16_loop
jmp .success

.base16_AF:
cmp al, 70
jg .failure

sub al, 55
cmp al, 10
jl .failure

jmp .base16_mul

.failure:
mov eax, FAILURE
ret
13 changes: 13 additions & 0 deletions to_number_test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ to_number_test:
tc1 thirteen, 2, SUCCESS, 13
tc1 thousand, 4, SUCCESS, 1000

mov [_blue.base], 16
tc1 not_number, 4, FAILURE, 0
tc1 not_number2, 5, FAILURE, 4660
tc1 zero, 1, SUCCESS, 0
tc1 one, 1, SUCCESS, 1
tc1 thirteen, 2, SUCCESS, 19
tc1 thousand, 4, SUCCESS, 4096
tc1 FF, 2, SUCCESS, 255
tc1 ABCDEF, 6, SUCCESS, 11259375

ret

not_number db '!@#$'
Expand All @@ -42,3 +53,5 @@ zero db '0'
one db '1'
thirteen db '13'
thousand db '1000'
FF db 'FF'
ABCDEF db 'ABCDEF'

0 comments on commit a3b7689

Please sign in to comment.