1

I want to call c function 'fputc', so that I need FILE pointer (in my case it's stdout). I know that I can use putc equivalent, but I'm curious if it is even possible to get pointer to stdout in asm. In c or c++ I would write something like 'stdout' or '&_iob[1]'. Is it even possible to get this working without replacing 'fputc' with 'putc'? (Target architecture is Windows on x86-32).

Example code (I want to print 'A'):

    push <the value I need>
    push 0x41
    call _fputc
    add esp, 8
  • What operating system and architecture are you programming for? – fuz Apr 11 '20 at 16:26
  • `fdopen(STDOUT_FILENO, "r")`. Ouch, you need Windows solution, no idea then – qrdl Apr 11 '20 at 16:27
  • I use msvcrt so it is windows – qwertyuiqwertyui Apr 11 '20 at 16:27
  • @qwertyuiqwertyui Are you writing a 16 bit, a 32 bit, or a 64 bit program? It looks like x86, but I really don't like having to guess. – fuz Apr 11 '20 at 16:29
  • 32 bit program. – qwertyuiqwertyui Apr 11 '20 at 16:30
  • 1
    @qwertyuiqwertyui Please add these details to your question. Be sure to mention x86, too. Also, remove the C++ tag as there doesn't seem to be anything about C++ in your question. – fuz Apr 11 '20 at 16:32
  • If you have the stdlib linked, you should just be able to use the `stdout` symbol – Artyer Apr 11 '20 at 16:36
  • @Artyer If there is such a symbol, that is. I'm not super familiar with MSVCRT, but it is likely that `stdin` is a macro expanding to something like `&_iob[1]` as OP hinted. In this case, the address must be computed from the size of a file file structure plus the `_iob` symbol. – fuz Apr 11 '20 at 16:37
  • 2
    Last time I had this question, I searched for an answer for a while before just trying the symbol `stdout`, which worked. YMMV depending on the implementation of your C library. – prl Apr 11 '20 at 17:36
  • @qwertyuiqwertyui: you usually don't have to open stdout yourself; ISO C's `FILE *stdout` should exist as a static symbol you can load the pointer value from. CRT is responsible for initializing it. Unless it's only a macro in MSVC's CRT? If it's a real pointer variable, the asm name should be `_stdout` So `push dword ptr [_stdout]` should work. – Peter Cordes Apr 11 '20 at 17:53
  • I think stdout just calls the OS to do putc (ah=2,dh=0,dl=char,int 21h) am I wrong? – Stephen Duffy Apr 12 '20 at 05:38

1 Answers1

0

You call __iob_func from MSVCRT.dll, it returns the iob struct and you add 0x30 to it and that's the address for stdout.

anon9000
  • 45
  • 4