0

Say I have this assembly code below. So like how does the call printf knows what to print? Does it just print out whatever is at the address that is pushed just above it all the time? In this case "push msg"?

SECTION .data

msg: db "Hello World!",10,0

SECTION .text

extern printf
global main

main:
     push ebp
     move ebp, esp

     push msg
     call printf

     move esp, ebp
     pop ebp
     ret
DiderDrogba344
  • 534
  • 8
  • 17
  • 3
    Yes, it looks at its args like any other function. Read the ABI / calling convention to find out how args are passed to functions. http://stackoverflow.com/tags/x86/info – Peter Cordes Dec 17 '16 at 03:13
  • It takes the format string address from the stack memory (`ss:esp`), as the next value after the return address, it doesn't matter how you set that memory to some value, doing `push` ahead of `call` is convenient and common "normal" way, but I would be able to simulate the same with just `mov`, `sub` and `jmp` instruction (no `push` or `call`). – Ped7g Dec 17 '16 at 13:16

1 Answers1

1

When you push msg, you're pushing the addres of the first byte of the vector of bytes that is your variable msg to the stack. When printf is called, it executes and prints the vector, byte per byte, from the first (the one you passed), until a signal character that tells it to stop. That signal character is the 0 after the Enter on the string.