【问题标题】:Outputting variable values in x86 asm在 x86 asm 中输出变量值
【发布时间】:2010-03-24 02:32:18
【问题描述】:

我正在用汇编语言编写一个程序,但它不起作用,所以我想在 x86 函数中输出变量,以确保这些值符合我的预期。有没有一种简单的方法可以做到这一点,还是很复杂?

如果它更简单,汇编函数是从 C 函数中使用的,并且是用 gcc 编译的。

【问题讨论】:

    标签: assembly x86 output


    【解决方案1】:

    您的问题似乎与“如何在 x86 汇编器中打印出变量值”类似。 x86 本身不知道如何做到这一点,因为它完全取决于您使用的输出设备(以及操作系统为该输出设备提供的接口的细节)。

    一种方法是使用操作系统系统调用,正如您在另一个答案中提到的那样。如果您使用的是 x86 Linux,则可以使用 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
    

    但是,如果你想打印数值,那么最灵活的方法是使用 C 标准库中的 printf() 函数(你提到你是从 C 调用你的汇编程序,所以你可能是无论如何链接到标准库)。这是一个例子:

    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
    

    需要注意的两点:

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

    【讨论】:

    • 这正是我想要的,非常感谢您的帮助。
    猜你喜欢
    • 2016-05-12
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    相关资源
    最近更新 更多