【问题标题】:Printf function usage in assembler program [duplicate]汇编程序中的Printf函数用法[重复]
【发布时间】:2021-05-16 00:47:48
【问题描述】:

我想用汇编程序编写简单的 Hello World 程序。这是我发现的:

    global  _main
    extern  _printf

    section .text
_main:
    push    message
    call    _printf
    add     esp, 4
    ret
message:
    db  'Hello, World', 10, 0

我的问题是:既然printf 函数可以采用更多的一个参数,那么当没有其他提供时,其他婴儿车的情况如何?它是否会从堆栈中获取其他参数?

【问题讨论】:

  • 它会查找与格式字符串(第一个 arg)一样多的额外参数。在您的情况下,没有额外的参数,因为没有 %d%s 或其他任何东西。如果还有更多,是的,它会像往常一样在堆栈上查找。

标签: windows assembly printf


【解决方案1】:

Printf 将分析格式字符串(第一个参数)并从堆栈中获取其他参数(如果需要)。您必须在调用之前正确推送参数并在调用之后恢复堆栈指针。例如:

    global  _main
    extern  _printf

    section .text
_main:
    push    1  
    push    message
    call    _printf
    add     esp, 8
    ret
message:
    db  '%d Hello, World', 10, 0

【讨论】:

  • 所以如果没有格式说明符printf“知道”我的例子中只有一个参数,对吧?
  • 是的,当没有格式说明符时 printf 不会搜索附加参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多