0

I'd like to write simple Hello World program in assembler. Here what I've found:

    global  _main
    extern  _printf

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

My question is: since printf function can take more the a one parameter whats with other prams when there is no other provide as in the example? Will it take the other params just from the stack whenever it is there?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Daros911
  • 435
  • 5
  • 14
  • 1
    It looks for as many extra args as the format string (first arg) makes it look for. In your case, no extra args because no `%d` or `%s` or whatever. If there were more, yes it would look on the stack, same as always. – Peter Cordes Feb 12 '21 at 09:29

1 Answers1

1

Printf will analyze format string (first argument) and get additional arguments, if any needed, from stack. You have to correctly push arguments before the call and restore stack pointer after it. For example:

    global  _main
    extern  _printf

    section .text
_main:
    push    1  
    push    message
    call    _printf
    add     esp, 8
    ret
message:
    db  '%d Hello, World', 10, 0
nevilad
  • 932
  • 1
  • 7
  • 14
  • So if there are no format specifiers `printf` "know" that there is only one parameter as in my example, right? – Daros911 Feb 13 '21 at 12:06
  • Yes, when there are no format specifiers printf will not search for additional parameters. – nevilad Feb 13 '21 at 12:44