字符串转换为int。 86的32位汇编​​使用NASM [英] Convert string to int. x86 32 bit Assembler using Nasm

查看:272
本文介绍了字符串转换为int。 86的32位汇编​​使用NASM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想将字符串转换为数字,所以我可以在以后添加另一个编号。这里是我不得不在远我的.text进行转换。 num2Entered是用户输入的内容。 Num1plusNum2是标签,我最终会增加。它们在.bss段都声明。任何帮助将是AP preciated!

  MOV AX,[num2Entered + 0]
    子斧头,'0'
    MOV BX,WORD 1000
    MUL BX
    MOV [Num1plusNum2],斧    MOV AX,[num2Entered + 1]
    子斧头,'0'
    MOV BX,WORD 100
    MUL BX
    添加[Num1plusNum2],斧    MOV AX,[num2Entered + 2]
    子斧头,'0'
    MOV BX,WORD 10
    MUL BX
    添加[Num1plusNum2],斧    MOV AX,[num2Entered + 3]
    子斧头,'0'
    添加[Num1plusNum2],斧


解决方案

每个字符是只有一个字节,但你可能想要将它添加到一个更大的结果。还不如去为32位...(可以架着你的日常到16位,如果你真的想)

  MOV EDX,num3entered;我们的字符串
与atoi:
XOR EAX,EAX;零的结果至今
。最佳:
MOVZX ECX,字节[EDX]得到一个字符
INC EDX;准备下一个
CMP ECX,'0';有效?
JB .done
CMP ECX,'9'
JA .done
子ECX,'0'; 转换字符数
IMUL EAX,10;乘造成这么远十
添加EAX,ECX;加上当前位
JMP .TOP;直到DONE
.done:
RET

这是我的头顶部,可能有错误,但这样的事情。它会以0结尾的字符串,或一个换行符结尾的字符串......或任何无效字符(你可能不希望)结束停止。修改以适应。

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 user entered. Num1plusNum2 is the label that I will eventually add to. They are both declared in the .bss section. Any help would be appreciated!

    mov ax, [num2Entered + 0]
    sub ax, '0'
    mov bx, WORD 1000
    mul bx
    mov [Num1plusNum2], ax

    mov ax, [num2Entered + 1]
    sub ax, '0'
    mov bx, WORD 100
    mul bx
    add [Num1plusNum2], ax

    mov ax, [num2Entered + 2]
    sub ax, '0'
    mov bx, WORD 10
    mul bx
    add [Num1plusNum2], ax

    mov ax, [num2Entered + 3]
    sub ax, '0'
    add [Num1plusNum2], ax

解决方案

Each character is only a single byte, but you probably want to add it to a larger result. Might as well go for 32 bits... (can hobble your routine to 16 bits if you really want to)

mov edx, num3entered ; our string
atoi:
xor eax, eax ; zero a "result so far"
.top:
movzx ecx, byte [edx] ; get a character
inc edx ; ready for next one
cmp ecx, '0' ; valid?
jb .done
cmp ecx, '9'
ja .done
sub ecx, '0' ; "convert" character to number
imul eax, 10 ; multiply "result so far" by ten
add eax, ecx ; add in current digit
jmp .top ; until done
.done:
ret

That's off the top of my head and may have errors, but "something like that". It'll stop at the end of a zero-terminated string, or a linefeed-terminated string... or any invalid character (which you may not want). Modify to suit.

这篇关于字符串转换为int。 86的32位汇编​​使用NASM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆