Assembly 在x86 asm中输出变量值
我正在汇编中编写一个程序,但它不起作用,所以我想在x86函数中输出变量,以确保这些值是我期望的值。有没有一种简单的方法可以做到这一点,还是非常复杂Assembly 在x86 asm中输出变量值,assembly,x86,output,Assembly,X86,Output,我正在汇编中编写一个程序,但它不起作用,所以我想在x86函数中输出变量,以确保这些值是我期望的值。有没有一种简单的方法可以做到这一点,还是非常复杂 如果这样做更简单,那么汇编函数将从C函数中使用,并使用gcc进行编译。您的问题似乎是“如何在x86汇编程序中打印变量值”。x86本身不知道如何做到这一点,因为它完全取决于您使用的输出设备(以及操作系统提供的该输出设备接口的细节) 一种方法是使用操作系统系统调用,正如您在另一个答案中提到的那样。如果您在x86 Linux上,那么可以使用sys\u wr
如果这样做更简单,那么汇编函数将从C函数中使用,并使用gcc进行编译。您的问题似乎是“如何在x86汇编程序中打印变量值”。x86本身不知道如何做到这一点,因为它完全取决于您使用的输出设备(以及操作系统提供的该输出设备接口的细节) 一种方法是使用操作系统系统调用,正如您在另一个答案中提到的那样。如果您在x86 Linux上,那么可以使用
sys\u write
sys调用将字符串写入标准输出,如下所示(GNU汇编程序语法):
但是,如果您想要打印数值,那么最灵活的方法是使用C标准库中的printf()
函数(您提到您正在从C调用汇编程序Rounties,因此您可能仍然链接到标准库)。这是一个例子:
int_format:
.string "%d\n"
.globl asmfunc2
.type asmfunc2, @function
asmfunc2:
movl $123456, %eax
# print content of %eax as decimal integer
pusha # save all registers
pushl %eax
pushl $int_format
call printf
add $8, %esp # remove arguments from stack
popa # restore saved registers
ret
有两点需要注意:
- 您需要保存和恢复寄存器,因为它们会被调用破坏;及
- 调用函数时,参数按从右到左的顺序排列
int_format:
.string "%d\n"
.globl asmfunc2
.type asmfunc2, @function
asmfunc2:
movl $123456, %eax
# print content of %eax as decimal integer
pusha # save all registers
pushl %eax
pushl $int_format
call printf
add $8, %esp # remove arguments from stack
popa # restore saved registers
ret