ASM x86 FASM中的函数参数

Bra*_*don 2 parameters x86 assembly function fasm

如何将参数传递给Assembly中的函数?我确实推了Last Param,推动Second Param,推进First Param ..

但是我无法访问MehFunction中的参数..我正在做的事情崩溃了程序..

format PE console                                ;Format PE OUT GUI 4.0
entry main

include 'macro/import32.inc'

section '.idata' import data readable           ;Import Section.
library msvcrt,'msvcrt.dll'
import msvcrt, printf, 'printf',\
exit,'exit', getchar, 'getchar'

section '.data' data readable writeable         ;Constants/Static Section.
InitialValue dd 0

section '.code' code readable executable
main:    
   push 67
   push 66
   push 65
   call MEH

   call [getchar]
   mov eax, 0
   ret 0

MEH:
   push ebx
   mov ebp, esp
   sub esp, 0

   mov eax, [ebp + 8]   ; Trying to print first parameter..
   push eax
   call [printf]
   add esp, eax

   mov esp, ebp
   pop ebx
ret
Run Code Online (Sandbox Code Playgroud)

joh*_*und 5

小额外说明.该过程的正确页眉/页脚使用push/pop ebp:

MEH:
   push ebp
   mov ebp, esp

   mov esp, ebp
   pop ebp
   ret
Run Code Online (Sandbox Code Playgroud)

原因是我们需要在将ebp寄存器用作参数和局部变量的指针之前保存/恢复它.

其次,CCALL调用约定,其中调用程序在返回过程后恢复堆栈指针对于C/C++语言是常见的,但对于汇编编程则不常见.原因很明显 - 编译器可以正确计算堆栈中推送的参数数量.在手写的汇编程序中,使用此约定将使代码不易读.

更好的方法是使用STDCALL调用约定:

MEH:
   push ebp
   mov  ebp, esp

   mov  esp, ebp
   pop  ebp
   retn 12   ; how many bytes to be automatically 
             ; removed from the stack after return.
Run Code Online (Sandbox Code Playgroud)

更好的做法是使用一些宏来自动创建标准过程元素,并为参数和局部变量提供人类可读的标签.例如,FreshLib库中提供的宏具有以下语法:

proc MEH, .arg1, .arg2, .arg3
; define local variables here, if needed.
begin
     ; place your code here without headers and footers
     return  ; will clean the stack automatically.
endp

; pushes the arguments in the stack and call MEH
stdcall MEH, 65, 66, 67   
Run Code Online (Sandbox Code Playgroud)

随FASM软件包提供的标准宏库的语法略有不同,FASM程序员手册对此进行了详细介绍.