2

In order to call fprintf from assembly code, I write the following

; push arguments...
push dword [stdout] ; given from "extern stdout"
call fprintf
; clean stack..

And it works just fine.

However, I would expect that I need to write push dword stdout because stdout is already a pointer (FILE*). Doing so gives me "wrong stdio handle error". Why is that?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
McLovin
  • 3,295
  • 7
  • 32
  • 67
  • `stdin` is defined externally as a `FILE *` . `push dword stdout` is pushing the address of `stdin` which is a pointer to a `FILE *`. So it actually acts like `FILE **` (a pointer to a pointer). `push dword [stdout]` pushes the contents at `stdout` onto the stack which is a `FILE *` – Michael Petch Apr 22 '19 at 18:44
  • Seems weird. Why don't we `call [fprintf]` then? hehe – McLovin Apr 22 '19 at 18:52
  • Because the label`fprintf` is a pointer to the actual function already. `call [fprintf]` would use the first 4 bytes (32-bits) of the actual function `fprintf` as the address to jump to. – Michael Petch Apr 22 '19 at 19:03

0 Answers0