x86 WinAPI - 我不明白我的程序中如何引用某些函数参数



我编写了一个使用WINAPI库(特别是WSA - Sockets)的c程序,而不是编译源代码,要求编译器发出汇编源代码,而不是研究它在较低级别上的工作方式。

当看到下面这一行时,我注意到在程序集中没有对WINAPI函数的第一个参数的引用。wsaststartup .

中的MAKEWORD函数

这里到底发生了什么?在我的汇编代码中没有引用MAKEWORD,但提示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

注意:WSAStartup函数启动一个进程使用Winsock DLL。

如果需要,我可以提供更多信息

MAKEWORD是一个类似函数的预处理器宏,定义为

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

由于您使用的是编译时常量(22),编译器可以通过将第二个参数向左移动8位并添加第一个参数来计算最终值:2 << 8 + 2。结果是512 + 2,即您看到的压入堆栈的值514。

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   ]
.
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

最新更新