因此,我目前正在遵循本教程https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm,我从中得到的是为了将ascii中的数字转换为十进制。我必须用'0‘减去它,然后执行数学运算,并通过添加'0’将它转换回它的ascii等价物。然而,当数字超过9时,对我来说问题就出现了。
我手头有一项任务,要求我逐个字符遍历文件中的所有内容,并跟踪“计数”。我不确定这样做的最好方法是什么,但我将展示我到目前为止所做的事情,它肯定不起作用,我怀疑它是因为当我将“计数器”转换为它的ascii值时,它确实存在,但它与碰巧具有该十进制值的其他字符匹配。
section .data
counter: dd '0' ;holds the num of chars read
section .bss
Buff resb 1 ;hold the value of one char
fd_in resb 4
fd_out resb 4
section .data
global _start
_start:
...
...
...
;increment the counter by 1 each time a char is wrote to file
Add_counter:
mov eax, [counter]
sub eax, '0'
inc eax
add eax, '0'
mov [counter], eax
...
...
...
;print out number of characters wrote
Show_num_char:
mov eax, 4
mov ebx, 1
mov ecx, counter
mov edx, 4
int 80h
....
...
..发布于 2016-10-01 13:50:45
在您的例子中,因为您要进行字符计数,所以使用处理器的二进制自然数系统会更实用。然后,将其转换为十进制。在本例中,假设您计算了301,927个字符。这将是0x49b67的二进制。这段代码将执行转换,然后使用RDI指向ASCII转换后的字符串。
我没有32位机器,所以你所要做的就是去掉REX限定符,把以"r“开头的寄存器改成"e”,然后同样的方法也适用于你。
section .text
global _start
_start push byte 0 ; So ASCII string will be terminated
; with NULL
mov rdi, rsp
mov eax, 0x49b67 ; = 301927
mov ecx, 10
Div:
xor edx, edx
div ecx ; DL = digit 0 - 9
or dl, '0'
mov [rdi], dl
dec rdi
or eax, eax
jnz Div
inc rdi ; RDI now points to ASCII string我在最后省略了退出程序的部分,因为在64位中,我使用SYSCALL,而您将使用int0x80,但是在GDB中执行此操作,您将看到它是如何工作的。
https://stackoverflow.com/questions/39803083
复制相似问题