Reputation: 4561
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
Upvotes: 0
Views: 377
Reputation: 51345
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.
Upvotes: 4
Reputation: 2180
MAKEWORD(a,b) is a macro that combine two BYTES (LOBYTE & HIBYTE) to make a word as the name says
the result you have in: push 514 ; 00000202H
is a (DWORD)(WORD) 0x0202
00 00 02 02
HB LB
[WORD] [WORD]
[ DWORD ]
.
Upvotes: 1
Reputation: 14858
lea eax, DWORD PTR _wsa$[ebp] ; eax = pointer to WSADATA structure
push eax ; set second argument = eax
push 514 ; set first argument = version 2.2
call DWORD PTR __imp__WSAStartup@8 ; call WSAStartup
test eax, eax ; eax = result. Is it zero?
je SHORT $LN4@main ; yes, success. Go do stuff.
; no, error code starts here
Upvotes: 0