一种方法是使用操作系统syscalls,正如您在另一个答案中提到的那样。如果您使用的是x86linux,那么可以使用
sys_write
sys调用将字符串写入标准输出,如下所示(GNU汇编程序语法):
STR:
.string "message from assembler\n"
.globl asmfunc
.type asmfunc, @function
asmfunc:
movl $4, %eax # sys_write
movl $1, %ebx # stdout
leal STR, %ecx #
movl $23, %edx # length
int $0x80 # syscall
ret
但是,如果要打印数值,则最灵活的方法是使用
printf()
来自C标准库的函数(您提到您正在从C调用汇编程序rountines,因此您可能正在链接到标准库)。这是一个例子:
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
有两点需要注意:
-
您需要保存和恢复寄存器,因为它们会被调用破坏;和
-
调用函数时,参数按从右到左的顺序排列。