From b0e9193b0aa2f75eaf9c6697a1e55b2cbd36e198 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Sun, 20 Jul 2025 12:18:29 +0200 Subject: [PATCH] atoi(): Add support for lowercase hexadecimal letters --- src/convert.asm | 23 ++++++++++++++++++++--- src/tests.asm | 4 +++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/convert.asm b/src/convert.asm index e606725..5f10c7d 100644 --- a/src/convert.asm +++ b/src/convert.asm @@ -71,19 +71,31 @@ atoi: je .calculate cmp byte [rdi], '0' jb .quit - ; Test if valid + cmp rsi, 16 je .getLenLoop_testHex + ; Check non-hexadecimal number cmp byte [rdi], dl ja .quit jmp .getLenLoop_cnt + ; Check hexadecimal number .getLenLoop_testHex: + cmp byte [rdi], 'f' + ja .quit + cmp byte [rdi], 'a' + jb .testHex_validUpper + ; byte[rdi] is in range a-f + jmp .getLenLoop_cnt + .testHex_validUpper: cmp byte [rdi], 'F' ja .quit cmp byte [rdi], 'A' - jb .testHex_isNum + jb .testHex_validNum + ; byte[rdi] is in range A-F jmp .getLenLoop_cnt - .testHex_isNum: + .testHex_validNum: + cmp byte [rdi], '0' + jb .quit cmp byte [rdi], '9' ja .quit @@ -125,8 +137,13 @@ atoi: mov al, byte [rdi] cmp al, 'A' jb .calcNum_notHex + cmp al, 'F' + ja .calcNum_lowerHex sub al, 55 ;'A'-10 (65-10) jmp .calcNum_cnt + .calcNum_lowerHex: + sub al, 87 ;'a'-10 (97-10) + jmp .calcNum_cnt .calcNum_notHex: sub al, '0' diff --git a/src/tests.asm b/src/tests.asm index ca5216c..35f508d 100644 --- a/src/tests.asm +++ b/src/tests.asm @@ -207,7 +207,7 @@ section .rodata ; atoi() msgAtoi db NL,"TEST atoi()",NL,EOS - atoiOutput db TAB,"%s = %d",NL,EOS + atoiOutput db TAB,"atoi(",DQUO,"%s",DQUO,"): %d",NL,EOS atoi1 db "1234567890",EOS atoi2 db "0xFFFFFFFF",EOS ;4 294 967 295 atoi3 db "0o776655",EOS ;261 549 @@ -218,6 +218,7 @@ section .rodata atoi8 db "0x0962GBCDEF",EOS ;0 atoi9 db "0o77865",EOS ;0 atoi10 db "0b102001",EOS;0 + atoi11 db "0xffffffff",EOS ;4 294 967 295 section .data section .bss @@ -1105,6 +1106,7 @@ _start: testatoi atoi8 testatoi atoi9 testatoi atoi10 + testatoi atoi11 %endif ;---