From a3b768909971065deef7986c7014804871b784d0 Mon Sep 17 00:00:00 2001 From: jbirddog <100367399+jbirddog@users.noreply.github.com> Date: Sat, 29 Jun 2024 06:17:34 -0400 Subject: [PATCH] Add base 16 parser (#58) --- to_number.inc | 64 ++++++++++++++++++++++++++++++++++++++++------ to_number_test.inc | 13 ++++++++++ 2 files changed, 69 insertions(+), 8 deletions(-) diff --git a/to_number.inc b/to_number.inc index 9de312e..19d2b2f 100644 --- a/to_number.inc +++ b/to_number.inc @@ -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 @@ -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 diff --git a/to_number_test.inc b/to_number_test.inc index 62d0170..83a95c1 100644 --- a/to_number_test.inc +++ b/to_number_test.inc @@ -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 '!@#$' @@ -42,3 +53,5 @@ zero db '0' one db '1' thirteen db '13' thousand db '1000' +FF db 'FF' +ABCDEF db 'ABCDEF'