Convert string to int. x86 32 bit Assembler using Nasm

后端 未结 3 1374
遇见更好的自我
遇见更好的自我 2020-12-06 21:11

So I\'m trying to convert a string to a number so I can add another number to it later. here is what I have to far in my .text for the conversion. num2Entered is what the us

3条回答
  •  太阳男子
    2020-12-06 21:55

    or we can say:
    "123"  -> starting from 1
    
    1 + 0 * 10  = 1
    2 + 1 * 10  = 12
    3 + 12 * 10 = 123
    
    This will match to atoi function as below:
    
    atoi:
    
    push    %ebx        # preserve working registers
    push    %edx
    push    %esi
    
    mov $0, %eax        # initialize the accumulator
    nxchr:
        mov $0, %ebx        # clear all the bits in EBX
        mov (%esi), %bl     # load next character in BL
        inc %esi            # and advance source index
    
        cmp $'0', %bl       # does character preceed '0'?
        jb  inval           # yes, it's not a numeral jb:jump below
        cmp $'9', %bl       # does character follow '9'?
        ja  inval           # yes, it's not a numeral ja:jump above
    
        sub $'0', %bl       # else convert numeral to int
        mull ten            # multiply accumulator by ten. %eax * 10
        add %ebx, %eax      # and then add the new integer
        jmp nxchr           # go back for another numeral
    
    inval:
       pop  %esi            # recover saved registers
       pop  %edx
       pop  %ebx
       ret
    

    Hope this will help.

提交回复
热议问题