[英]Stuck on writing a print function to convert and print binary to decimal with syscalls on NASM for x86-64 Linux system
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.我对 x86-64 Linux 系统的 NASM 汇编代码还是陌生的,我正在尝试编写一个程序,将存储在 rdi 中的数字转换为十进制,以便可以打印。 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:
我不确定如何编写一个正确的 function,它将在使用余数作为数字的循环中除以 10,例如:存储在 rdi 中的数字 165 将被重复除以 165/10,余数为 5,等等。数字 output 看起来像是 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
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以下
mov r10, 10
add添加
mov r15, 9
Erase擦除
add r15, rdx
cmp rax, 1
jnle convert
and change it to并将其更改为
mov rdi, rax
add rdx, '0'
mov [r15 + buf], dl
dec r15
jns convert
This is the full printnum
function with the fix.这是带有修复程序的完整
printnum
function。
printnum:
mov r10, 10
mov r15, 9
convert:
mov rdx, 0
mov rax, rdi
div r10
mov rdi, rax
add rdx, '0'
mov [r15 + buf], dl
dec r15
jns convert
mov rax, 1
mov rdi, 1
mov rsi, buf
mov rdx, BUFLEN
syscall
ret
It's up to you to think why this works and yours doesn't.由您来思考为什么这行得通而您的行不通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.