NASM Forum > Programming with NASM

Problems using div?

<< < (3/3)

brethren:
as i promised:) heres a working example that you can read. it produces no output though so make sure you run it through a debugger

--- Code: ---SECTION .data

        str1 db "-123", 0
        str2 db "+456", 0
        str3 db "72 years old", 10, 0

SECTION .text
GLOBAL _start:

_start:
        nop
        mov eax, str1
        call atodw      ;EAX=-123
        mov eax, str2
        call atodw      ;EAX=456
        mov eax, str3
        call atodw      ;EAX=72

        mov eax, 1
        mov ebx, 0
        int 80h

;---------------------------------------------------------
; convert an ascii string containing a number to a dword
; input: EAX=pointer to the string containing the number
; returns: EAX=returned dword
;---------------------------------------------------------
atodw:
        push ebx
        push ecx
        push esi

        xor ecx, ecx            ;ECX is used as a flag. 0=positive number, 1=negative number
        mov esi, eax            ;copy the string pointer to ESI
        xor eax, eax            ;EAX is now used for returning the value to the user

.SignTest:
       
        movzx ebx, byte[esi]    ;get the first character from the string
        cmp ebx, '-'            ;and test if it is a minus sign
        jne .PosSignCheck
        mov ecx, 1              ;set ECX=1 (number is negative)
        inc esi
        jmp .DoConversion

.PosSignCheck:

        cmp ebx, '+'             ;numbers that start with '+' aren't used often (but they are legal)
        jne .DoConversion
        inc esi

.DoConversion:

        movzx ebx, byte[esi]    ;get an ascii digit
        sub ebx, '0'            ;convert ascii digit to decimal value
        cmp ebx, 9              ;if the value in EBX is above 9 then the character
                                ;pointed to by ESI wasn't a valid digit (bewtween '0'-'9')
        ja .WasNumNeg           ;this jump is taken when the conversion is completed
                                ;ie we reached the null char or we encountered an invalid digit

        ;these 2 instructions multiply EAX by 10 and add the digit that we've retrieved to EAX
        lea eax, [eax+eax*4]
        lea eax, [ebx+eax*2]

        inc esi
        jmp .DoConversion

.WasNumNeg:

        ;we check the flag in ECX to find out if the string representation of the number
        ;was negative or positive. If it was negative we have to two's complement EAX
        or ecx, ecx
        jz .Finished
        neg eax                 ;perform two's complement

.Finished:
       
        pop esi
        pop ecx
        pop ebx
        ret

--- End code ---

Luns:
Thank you both so much! I now have a working string to integer function  ;D

So 'movzx eax ...' automatically clears the upper bits of eax, right?

Serge:

--- Quote from: Luns on April 12, 2011, 09:25:38 PM ---So 'movzx eax ...' automatically clears the upper bits of eax, right?
--- End quote ---
Yes, you are right. And it is one byte shorter than:
xor eax, eax
mov ax, [...]

Navigation

[0] Message Index

[*] Previous page

Go to full version