在ASM 86 FASM功能参数 [英] Function Parameters in ASM x86 FASM
问题描述
我如何传递参数给汇编函数?
我没有推最后参数,推二参数,按下第一个参数。
但我不能咩
功能..我在做什么导致程序崩溃。
格式PE控制台;格式PE OUT GUI 4.0
进入主包括宏观/ import32.inc部分的.idata导入数据可读;进口部分。
库MSVCRT,MSVCRT.DLL
进口MSVCRT,printf的,'printf的',\\
退出,退出,getchar函数的getchar节'。数据的数据可读可写;常量/静态部分。
与InitialValue DD 0节'。code'code可读可执行
主要:
推67
推66
推65
调用MEH 调用[的getchar]
MOV EAX,0
RET 0MEH:
推EBX
MOV EBP,ESP
子ESP,0 MOV EAX,[EBP + 8];要打印第一个参数..
推EAX
调用[的printf]
ADD ESP,EAX MOV ESP,EBP
流行EBX
RET
让我们来看看...
说你的ESP是0x00180078在一开始就那么三推之后,你有
00180078:67
00180074:66
00180070:65
然后调用MEH,立即推EBX所以现在你有堆栈
00180078:67
00180074:66
00180070:65
0018006C:返回地址
00180068:EBX值
现在装入ESP EBP = 00180068
子ESP,0什么也不做MOV EAX,[EBP + 8]〜00180068 + 8 = 00180070 = 65
所以不是第一次,而是最后一个参数
呼叫[printf的]
下面来你的问题,但:
ADD ESP,EAX
什么是好这该怎么办?假设printf的preserves这个论点(它被顺便不做要求)过去了,你为什么会在参数添加到堆栈指针?这是一定要弄乱你的回报。
你想要做什么是ESP恢复到EBP的价值和弹回保存的EBX值。
How do I pass parameters to a function in Assembly? I did push Last Param, push Second Param, push First Param..
But I cannot access the parameters within Meh
Function.. What I'm doing crashes the program..
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
Let's see...
Say your ESP is 0x00180078 on the outset, then after the three pushes you have
00180078: 67
00180074: 66
00180070: 65
then you call MEH, which immediately pushes ebx so now you have the stack as
00180078: 67
00180074: 66
00180070: 65
0018006C: return address
00180068: ebx value
you now load EBP with ESP = 00180068
sub esp,0 does nothing
mov eax, [ebp+8] ~ 00180068 + 8 = 00180070 = 65
so not the first but rather the last argument
call [printf]
Here comes your problem, though:
add esp, eax
What good was this supposed to do? Assuming printf preserves this argument passed in (which it is incidentally not required to do), why would you add the argument to the stack pointer? That is sure to mess up your return. What you want to do is restore esp to the value of ebp and pop back the saved ebx value.
这篇关于在ASM 86 FASM功能参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!