繁体   English   中英

来自NASM的多位数字输入

[英]Multi-Digit Input from NASM

因此,我对汇编语言不熟悉,所以我对基础知识有相当扎实的了解,但是用户输入始终使我感到困惑。 所以现在我有以下代码来接收来自用户的一位数字:

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

然后定义如下

SECTION .bss
inStrBuf:  times StrLen resb  ' ' 

Section .data
StrLen: equ 8 

将值放入ecx之后,值就是数字+2608。所以我一直在做的就是简单地减去2608并得到数字。 现在,当我输入一个以上的数字(如46)时,我得到的是转换为十进制的669236。没有像以前一样简单地减去2608的简单方法。

首先,2608的功能是什么,有什么方法可以只接受654之类的数字并将其放入寄存器中(当然是十六进制值)。 谢谢!

我不知道2608是从哪里来的,甚至不知道669236! 总体思路是:

;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:

这就是我通常使用的...

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
;------------------------

这使用两个lea的“聪明”方法乘以10,减去“ 0”,然后加上新的数字。 它的缺点是不设置标志,因此我们无法检查溢出-它只是默默地滚动。 任何“无效”字符都会停止-适用于xero,换行符(sys_read将在其中存在)...或“垃圾”。 返回时,“无效”字符将出现在ecx中(只是cl很有趣),而edx指向下一个字符。 便于解析“ 192.168.1.1”左右。 您可能更喜欢使用更简单的方法。 :) C库“ atoi”或“ scanf”的工作方式...如果您想这样做的话...

真的很好奇2608的来源!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM