0

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

当遇到下面的这一行时,我注意到在程序集中没有引用我的 WINAPI 函数的第一个参数。WSAStartup 中的函数 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 的使用。

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

4

3 回答 3

4

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

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

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

于 2015-10-19T01:01:44.237 回答
1
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   ]
.
于 2015-10-19T01:31:24.717 回答
0
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
于 2015-10-19T01:27:35.813 回答