Multi-Digit Input from NASM

可紊 提交于 2020-01-06 08:17:15

问题


So I'm fairly new to Assembly language I have a pretty solid grasp on the fundamentals but user input has always baffled me. So right now I have the following code to receive a single digit from the user:

mov eax, 3
mov ebx, 0
mov ecx, inStrBuf
mov edx, StrLen
int 80h

And then the definitions are as follows

SECTION .bss
inStrBuf:  times StrLen resb  ' ' 

Section .data
StrLen: equ 8 

After I put the value in ecx, the value is the digit + 2608. So what I have been doing is simply subtracting 2608 and getting the digit. Now when I put in more than one digit, like the number 46, I get, when I convert to decimal, 669236. There is no simple way of just subtracting 2608 like I was before.

First of all, what's up with the 2608, and is there any way to just accept a number like 654 and put it in a register (in a hex value of course). Thanks!


回答1:


I have no idea where 2608 came from, even less 669236! The general idea is:

;zero out someplace to put result
top:
;get a digit/character
;make sure it represents a decimal digit
;(if not - go to done)
;subtract '0' to convert character to number
;multiply "result so far" by 10
;add in the new number
;go to top
done:

This is what I usually use...

section .bss
    inStrBuf resb StrLen ; 12+ is good...

section .text
    ...
    push inStrBuf ; pass parameter on stack
    call atoi
    add esp, 4 ; clean up stack
    mov [someplace], eax
    ...

;--------------------
atoi:
    push ebx

    mov edx, [esp + 8]  ; pointer to string
    xor ebx, ebx ; assume not negative

    cmp byte [edx], '-'
    jnz .notneg
    inc ebx ; indicate negative
    inc edx ; move past the '-'
.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

    ; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to 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
;------------------------

This uses the "clever" method of two leas to multiply by ten, subtract '0', and add in the new number. It has the disadvantage of not setting flags, so we can't check for overflow - it just silently rolls over. Any "invalid" character stops - works for xero, linefeed (which sys_read will have there)... or "garbage". When it returns, the "invalid" character will be in ecx (just cl is interesting), and edx points to the next character. Handy for parsing "192.168.1.1" or so. You may prefer to use something more straightforward. :) The C library "atoi" or "scanf" work... if ya wanna do it THAT way...

Really curious where that 2608 came from!



来源:https://stackoverflow.com/questions/13696983/multi-digit-input-from-nasm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!