Search code examples
winapiassemblyx86wsastartupwindows-socket-api

x86 WinAPI - I don't understand how some function arguments are referenced in my program


I have wrote a c program that uses the WINAPI library (specifically WSA - Sockets) and instead of compiling the source code asked the compiler to emit assembly source instead to study how it works on the lower level.

When coming across this line below I noticed in the assembly that there is no reference to the first argument of my WINAPI function.The function MAKEWORD in WSAStartup.

What is really happening here? There is no references in my assembly code to MAKEWORD but a hint of push 514.


 ; source code   :     if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)

     lea     eax, DWORD PTR _wsa$[ebp]  ;_wsa$ is second arg
     push    eax
     push    514            ; 00000202H ???
     call    DWORD PTR __imp__WSAStartup@8
     test    eax, eax
     je  SHORT $LN4@main

Note: The WSAStartup function initiates use of the Winsock DLL by a process.

I can provide more info if needed


Solution

  • MAKEWORD is a function-like preprocessor macro, that is defined as

    #define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) |
                            ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
    

    Since you are using it with compile-time constants (2 and 2), the compiler can compute the final value by shifting the second argument 8 bits to the left and adding the first: 2 << 8 + 2. The result is 512 + 2, the value 514 you are seeing pushed onto the stack.