stderr How did it work ?

Started by ToutEnMasm, August 30, 2011, 07:05:02 AM

Previous topic - Next topic

ToutEnMasm


The stderr is defined as this:
Quote
C:\Program Files\Microsoft Visual Studio 10.0\VC\include\stdio.h
159   : #define stderr (&__iob_func()[2])
When used:
Quote
    invoke fprintf,stderr,addr BVCallback,hInternet
;disassemble
   mov   eax,hInternet
   push   eax
   push   OFFSET BVCallback                                                 
   call   __iob_func
   add   eax, 64               ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
   push   eax
   call   fprintf
   add   esp, 12               ; 0000000cH
The C compiler add 64 to eax (return value of __iob_func ),What it means ?



Vortex

Hi ToutEnMasm,

If I am not wrong, __iob_func is redirected to __p__iob

The value returned by __p__iob is used to set stdin,stdout and stderr.

_iobuf STRUCT
    _ptr        DWORD ?
    _cnt        DWORD ?
    _base       DWORD ?
    _flag       DWORD ?
    _file       DWORD ?
    _charbuf    DWORD ?
    _bufsiz     DWORD ?
    _tmpfname   DWORD ?
_iobuf ENDS

FILE TYPEDEF _iobuf


SIZEOF(FILE) = 8 *4 = 32


start:

    call    crt___p__iob
    mov     stdin,eax           ; #define stdin  (&__iob_func()[0])
    add     eax,SIZEOF(FILE)
    mov     stdout,eax          ; #define stdout (&__iob_func()[1])
    add     eax,SIZEOF(FILE)
    mov     stderr,eax          ; #define stderr (&__iob_func()[2])
    invoke  crt_fprintf,stdout,ADDR msg,stdout       
    invoke  ExitProcess,0


call    crt___p__iob  returns stdin

stdin + 2 * SIZEOF(FILE) = stderr

2 * SIZEOF(FILE) = 64  This is the value 64 you are looking for.

Have a look at this thread :

http://www.masm32.com/board/index.php?topic=7523.0

ToutEnMasm