43 lines
800 B
NASM
43 lines
800 B
NASM
|
; Loops over every character.
|
||
|
; Multiplies current by 10
|
||
|
; Adds [bx] - 0x30
|
||
|
; add bx, 1
|
||
|
parse_int:
|
||
|
pusha
|
||
|
|
||
|
; Contains the actual result
|
||
|
mov ax, 0
|
||
|
|
||
|
.loop:
|
||
|
mov cl, [bx]
|
||
|
|
||
|
cmp cl, 0x0 ; End on null
|
||
|
je .end
|
||
|
|
||
|
; If the character is invalid, stop here.
|
||
|
cmp cl, 0x30
|
||
|
jl .end
|
||
|
cmp cl, 0x39
|
||
|
jg .end
|
||
|
|
||
|
; Multiply by 10
|
||
|
mov cx, 10 ; NOT 0x10
|
||
|
mul cx
|
||
|
; Note: Has to be cx and not cl, otherwise it will be limited to 8-bits only
|
||
|
|
||
|
mov cl, [bx]
|
||
|
sub cl, 0x30
|
||
|
mov ch, 0x0
|
||
|
add ax, cx ; Use 16-bit addition
|
||
|
|
||
|
add bx, 1 ; Next char
|
||
|
|
||
|
jmp .loop
|
||
|
.end:
|
||
|
|
||
|
mov [result], ax
|
||
|
|
||
|
popa
|
||
|
ret
|
||
|
|
||
|
result: dw 0
|