String To Integer Muster

Reading time ~1 minute

Sting To Integer in NASM

GLOBAL strToInt

SECTION .text

strToInt:

    ; init registers
    XOR RAX, RAX
    XOR RCX, RCX
    XOR R8,  R8

    ; check base is valid
    CMP SIL, 2
    JB _exit		; not gonna do base = 1
    CMP SIL, 36
    JA _exit

    ; skip sign
    CMP BYTE [RDI], '-'
    JNE _loop
    INC RCX

_loop:
    MOV R8B, [RDI+RCX]

_testnum:
    CMP R8B, '0'
    JL _exit
    CMP R8B, '9'
    JG _testalph
    SUB R8B, '0'
    JMP _addpower

_testalph:
    CMP R8B, 'A'
    JL _exit
    CMP R8B, 'Z'
    JG _exit
    SUB R8B, 'A' - 10

_addpower:
    CMP R8B, SIL
    JGE _exit
    MUL RSI
    ADD RAX, R8
    INC RCX
    JMP _loop

_exit:
    CMP BYTE [RDI], '-'
    JNE _ret
    NEG RAX
_ret:
    RET