cdecl和stdcall调用约定-汇编演示

.386
.model flat, stdcall
.stack 4096

ExitProcess PROTO, dwExitCode:DWORD

.data
val2 sdword 1
result dword ?

.code
main proc
    call Example_cdecl
    call Example_stdcall
    invoke ExitProcess, 0
main endp

comment /*C调用约定;参数入栈:从右到左;调用者恢复栈顶指针*/
Example_cdecl proc
    push 6
    push 5
    xor eax,eax
    call AddTwo_cdecl
    add esp,8
    ret
Example_cdecl endp

AddTwo_cdecl proc
    push ebp
    mov ebp,esp
    
    add eax, [ebp + 12]
    add eax, [ebp + 8]
    
    pop ebp
    ret
AddTwo_cdecl endp


comment /*stdcall调用约定;参数入栈:从右到左;调用者恢复栈顶指针*/
Example_stdcall proc
    push 6
    push 5
    xor eax, eax
    call AddTwo_stdcall
    comment /* add esp, 8 ; 此由被调用者恢复栈顶指针(ret 8),不再需要调用*/
    ret
Example_stdcall endp

AddTwo_stdcall proc
    push ebp
    mov ebp, esp

    add eax, [ebp + 12]
    add eax, [ebp + 8]

    pop ebp
    ret 8
AddTwo_stdcall endp

end main

cdecl由调用者恢复栈顶指针,所以支持可变参数的传入。

stdcall由被调用者恢复栈顶指针,不支持可变参数,但目标代码少了一条指令(少了一个字节的长度)。

posted @ 2015-05-02 18:30  庚武  Views(662)  Comments(0Edit  收藏  举报