一直在为 x86-64 Linux 系统编写一个打印函数,通过 NASM 上的系统调用将二进制转换并打印为十进制
我对 x86-64 Linux 系统的 NASM 上的汇编代码仍然很陌生,我正在尝试编写一个程序,将 rdi 中存储的数字转换为十进制,以便可以打印。我不确定如何编写一个正确的函数,在使用余数作为数字的循环中除以 10,例如:存储在 rdi 中的数字 165 将被重复除以 165/10,余数为 5,等等。数字输出看起来类似于 0000000165。如有任何反馈,我们将不胜感激! 这是我的尝试:
section .data
BUFLEN: equ 25
buf: times BUFLEN db 0
section .text
global _start
_start:
mov rsi, 1
mov rdi, 453265682
call printnum
mov rax, 60
mov rdi, 0
syscall
printnum:
mov r10, 10
convert:
mov rdx, 0
mov rax, rdi
div r10
add r15, rdx
cmp rax, 1
jnle convert
mov rax, 1 ; = 1
mov rdi, 1 ; = 1
mov rsi, buf ; add of buffer to print
mov rdx, BUFLEN ; num of bytes to write
syscall
ret
I'm still new to assembly code on NASM for x86-64 Linux system and I'm trying to write a program which converts the number stored in rdi to decimal so it can be printed. I'm unsure how to write a proper function that will divide by 10 in a loop that uses the remainder as the digits for example: the number 165 stored in rdi will be divided 165/10 repeatedly and the remainder is 5, etc. and the digits output would look something to 0000000165. Any feedback would be appreciated!
Here's my attempt:
section .data
BUFLEN: equ 25
buf: times BUFLEN db 0
section .text
global _start
_start:
mov rsi, 1
mov rdi, 453265682
call printnum
mov rax, 60
mov rdi, 0
syscall
printnum:
mov r10, 10
convert:
mov rdx, 0
mov rax, rdi
div r10
add r15, rdx
cmp rax, 1
jnle convert
mov rax, 1 ; = 1
mov rdi, 1 ; = 1
mov rsi, buf ; add of buffer to print
mov rdx, BUFLEN ; num of bytes to write
syscall
ret
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有很多部分我想更改,但这里有一个最小的修复,可以让您的代码正常工作。
下面
添加
Erase
并将其更改为
这是包含修复程序的完整
printnum
函数。由你来思考为什么这个有效而你的无效。
There are so many parts in your code that I want to change, but here is a minimal fix to just make your code work.
Below
add
Erase
and change it to
This is the full
printnum
function with the fix.It's up to you to think why this works and yours doesn't.