我编写了一个汇编程序(x86_64 Linux ),该程序将整数打印到控制台,基于该算法建议我在这个post中的注释,基本上如下:
divide number x by 10, giving quotient q and remainder r
emit r
if q is not zero, set x = q and repeat
在下面的脚本中,所有操作都很好:
section .bss
integer resb 100 ; it will hold the EOL
intAddress resb 8 ; the offset
section .text
global _start:
_start:
mov rax, 567
call _printProc
mov rax, 60
mov rdi, 0
syscall
_printProc: ; here goes the algorithm described above.
编译后,数字567
将被打印在屏幕(控制台)上。
但是,如果我尝试这样做,但允许用户输入要打印为整数的数字,我就不会得到预期的结果。为了做到这一点,我做了以下更改(算法保持不变):
section .bss
integer resb 100 ; it will hold the EOL
intAddress resb 8 ; the offset
number resb 100
section .text
global _start:
_start:
; getting user input
mov rax, 0
mov rdi, 0
mov rsi, number
mov rdx, 100
syscall
mov rax, [number] ; passing the content at address number into rax
call _printProc
mov rax, 60
mov rdi, 0
syscall
_printProc: ; here goes the algorithm described above.
但是在这种情况下,如果我输入567
,就会得到171390517
。事实上,如果我输入
0, I get 2608
1, I get 2609
2, I get 2610
诸若此类。
如果你们中的一些人知道第二种情况下的问题是什么,以及如何解决这些问题,我将不胜感激。
发布于 2017-05-08 23:34:56
当你把这叫做
; getting user input
mov rax, 0
mov rdi, 0
mov rsi, number
mov rdx, 100
syscall
是,您的条目(例如"1004")被写入内存中的“数字”,每个字符的字符。现在,您要解决的问题正好相反:“如何将ASCII字符串转换为二进制值”。
这个新问题的算法可以如下所示:
(assuming char_ptr points to the string)
result = 0;
while ( *char_ptr is a digit )
result *= 10;
result += *char_ptr - '0' ;
char_ptr++;
https://stackoverflow.com/questions/43858176
复制