繁体   English   中英

在Assembly x86中将一串数字转换为整数

[英]Converting a string of numbers into an integer in Assembly x86

我正在尝试将用户输入的数字字符串转换为整数。

例如,用户输入“1234”作为我希望1234存储在DWORD变量中的字符串。
我正在使用lodsbstosb来获取单个字节。 我的问题是我无法得到适合它的算法。 我的代码如下:

mov ecx, (SIZEOF num)-1
mov esi, OFFSET num
mov edi, OFFSET ints
cld

counter:
   lodsb
   sub al,48
   stosb
   loop counter

我知道ECX计数器也会有点关闭因为它正在读取整个字符串而不仅仅是4个字节,所以它实际上是9因为字符串是10个字节。

我试图使用10的幂来乘以单个字节,但我对Assembly很新,并且无法为它获得正确的语法。 如果有人可以帮助解决那些很棒的算法。 谢谢!

一个简单的实现可能是

    mov ecx, digitCount
    mov esi, numStrAddress

    cld                     ; We want to move upward in mem
    xor edx, edx            ; edx = 0 (We want to have our result here)
    xor eax, eax            ; eax = 0 (We need that later)

counter:
    imul edx, 10            ; Multiply prev digits by 10 
    lodsb                   ; Load next char to al
    sub al,48               ; Convert to number
    add edx, eax            ; Add new number
    ; Here we used that the upper bytes of eax are zeroed
    loop counter            ; Move to next digit

    ; edx now contains the result
    mov [resultIntAddress], edx

当然有一些方法可以改进它,比如避免使用imul

编辑:修正了ecx值

暂无
暂无

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

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