在x86 asm中输出变量值

Jos*_*eph 3 x86 assembly output

我正在汇编程序中编写一个程序并且它不起作用,所以我想在x86函数中输出变量以确保值是我期望的值.有一种简单的方法可以做到这一点,还是非常复杂?

如果它更简单,则从C函数中使用汇编函数,并使用gcc进行编译.

caf*_*caf 7

看来你的问题是"如何在x86汇编程序中打印变量值".x86本身不知道如何做到这一点,因为它完全取决于您正在使用的输出设备(以及OS提供的输出设备接口的具体细节).

一种方法是使用操作系统系统调用,正如您在另一个答案中提到的那样.如果您使用的是x86 Linux,则可以使用sys_writesys调用将字符串写入标准输出,如下所示(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
Run Code Online (Sandbox Code Playgroud)

但是,如果要打印数值,那么最灵活的方法是使用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
Run Code Online (Sandbox Code Playgroud)

有两点需要注意:

  • 您需要保存和恢复寄存器,因为它们会受到呼叫的破坏; 和
  • 调用函数时,参数按从右到左的顺序推送.