nasm - Assembly x86 append numbers to a variable -


i'm reading input numeric string, iterate character character convert each digit in decimal.

now @ every iteration in 1 of register, example al, have single digit, let's say

input: 12345678 iteration_1 : 1 iteration_2 : 2 ... iteration_8 : 8 

i add these integers dd variable, @ end of iteration have dd variable containing whole number use operation.

has sense? how append @ each iteration current number dd variable?

zero out register use "result far".

top:

get character.

make sure have valid decimal digit.

subtract '0' convert character number.

multiply "result far" ten.

add in new number.

go top.

this use...

;-------------------- atoi: ; expects: address of string on stack ; returns: number in eax ; "trashes" ecx , edx ; actually, edx "next character" ; , ecx (cl) "invalid" character ; think of "192.168.0.1"... ; caller cleans stack push ebx  mov edx, [esp + 8]  ; pointer string xor ebx, ebx ; assume not negative  cmp byte [edx], '-' jnz .notneg inc ebx ; indicate negative inc edx ; move past '-' .notneg:  xor eax, eax        ; clear "result" .top: movzx ecx, byte [edx] inc edx cmp ecx, byte '0' jb .done cmp ecx, byte '9' ja .done  ; have valid character - multiply ; result-so-far 10, subtract '0' ; character convert ; number, , add result.  lea eax, [eax + eax * 4] lea eax, [eax * 2 + ecx - '0']  jmp short .top .done: test ebx, ebx jz .notminus neg eax .notminus: pop ebx ret ;------------------------ 

now mov [myvar], eax. has disadvantages! quit on invalid character. catches zero-terminated string, or linefeed-terminated string (as linux), or whatever. fine, don't yell @ user if screw up. can't check overflow. if need that, ditch "cute trick" , go imul eax, 10... if don't need deal negative numbers, can simplified bit.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -