atoi(): Add support for lowercase hexadecimal letters

This commit is contained in:
2025-07-20 12:18:29 +02:00
parent 318ccc960f
commit b0e9193b0a
2 changed files with 23 additions and 4 deletions

View File

@@ -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'

View File

@@ -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
;---