Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 汇编代码帮助(将字符串转换为int)_Assembly_X86_Nasm - Fatal编程技术网

Assembly 汇编代码帮助(将字符串转换为int)

Assembly 汇编代码帮助(将字符串转换为int),assembly,x86,nasm,Assembly,X86,Nasm,我正在试着用汇编语言编写这个程序。 它应该只是将字符串转换为十进制 int main (int argc, char **argv) { int result = 0; char *c = argv[1]; while(*c != 0) { result *= 10; result += (*c - 48); c++; } return result; } 但是没有从堆

我正在试着用汇编语言编写这个程序。 它应该只是将字符串转换为十进制

int main (int argc, char **argv)
{
    int result = 0; 
    char *c = argv[1];  
    while(*c != 0)
    { 
        result *= 10;           
        result += (*c - 48);
        c++;    
    }
return result;
}
但是没有从堆栈中获取argv。。。所以我在asm中编写了一个小代码,我认为应该这样做,但它不起作用

有人能指出我做错了什么吗? 我对代码进行了注释,以便您了解我的想法,请纠正我的错误

section .bss
        buf resb 8  ; define an array of 8 uninitialised bytes
section .text

global _start
_start:
    mov rax, 3  ; system call number (sys_read)
    xor rbx, rbx    ; file descriptor 0 (stdin)
    mov rcx, buf    ; buffer to store data
    mov rdx, 8  ; Lenght of buffer
    int 0x80

    xor rax,rax ; rax = 0
    mov rbx, buf    ; rbx = &buf
    xor rcx, rcx    

StrToDec:
        mov cl, [rbx]   ; cl = *rbx 
        cmp rcx, 0      ; check for nul THIS IS WRONG
        cmp rcx, 10     ; we have to check for NL ascii code 
        je end          ; if rcx == 0 goto end
        imul rax, 10    ; rax *= 10
        sub rcx, 48     ; rcx -= 48 (48 is acii for '0')
        add rax, rcx    ; rax += rcx
        inc rbx         ; rbx++
        jmp StrToDec

end:
    mov rbx, rax    ; rbx = rax
    mov rax, 1      ; rax = 1
    int 0x80
当我运行这个程序并键入100例如 然后键入echo$?到终端,它应该打印100

谢谢

    mov rcx, [rbx]  ; rcx = *rbx 
这是错误的,它从缓冲区加载了8个字节。您只想加载1。使用
mov-cl[rbx]
movzx-ecx,字节[rbx]

固定:

我发现这个程序有什么问题

首先,您需要检查值10,以便

cmp rcx, 0      ; check for nul 
是错误的,因为我们查找的新行ascii代码不是空的

cmp rcx, 10      ; check for NL
是的,我认为如果您从堆栈中获取args,那么它不起作用,当从堆栈中获取args时,您应该检查0

第二:对于大于255的数字,
echo$
?会给我错误的结果,但这没关系,因为
echo$
?最多只能显示255个数字,因此即使echo也可能显示错误的数字,寄存器rbx也会保留正确的值


我调试过了,现在可以正常工作了

你得到了什么结果?什么是“不起作用”?当我输入100它返回194,当我输入10它返回62你能再举几个例子吗?当您键入
543
1000
时会发生什么?相关:我将上述行更改为您所说的内容,但仍然无效返回完全相同的错误结果或更一般地说,检查非数字字符以停止循环!除非你不介意把“123xyz\n”变成
1*10^5+2*10^4+3*10^3+('x'-'0')*10^2+('y'-'0')*10+('z'-'0')
或其他什么。使用
movzx ecx,byte[rbx]
/
子ecx,'0'
/
cmp ecx,9
/
ja.非数字
从ASCII转换为整数,并将范围检查功能集于一身。