如何在程序集 NASM 中打印数字? [英] How to print a number in assembly NASM?
本文介绍了如何在程序集 NASM 中打印数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在寄存器中有一个整数,我如何打印它?你能展示一个简单的示例代码吗?
Suppose that I have an integer number in a register, how can I print it? Can you show a simple example code?
我已经知道如何打印诸如hello, world"之类的字符串.
I already know how to print a string such as "hello, world".
我正在 Linux 上开发.
I'm developing on Linux.
推荐答案
如果您已经在使用 Linux,则无需自己进行转换.只需使用 printf 代替:
If you're already on Linux, there's no need to do the conversion yourself. Just use printf instead:
;
; assemble and link with:
; nasm -f elf printf-test.asm && gcc -m32 -o printf-test printf-test.o
;
section .text
global main
extern printf
main:
mov eax, 0xDEADBEEF
push eax
push message
call printf
add esp, 8
ret
message db "Register = %08X", 10, 0
注意 printf
使用 cdecl 调用约定,所以我们需要恢复之后的堆栈指针,即为传递给函数的每个参数添加 4 个字节.
Note that printf
uses the cdecl calling convention so we need to restore the stack pointer afterwards, i.e. add 4 bytes per parameter passed to the function.
这篇关于如何在程序集 NASM 中打印数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文