程序调用在汇编器中如何工作? [英] how do procedure calls work in assembler?
问题描述
我刚刚开始修改ASM,但不确定我对过程调用的理解是否正确.
I just started tinkering with ASM and I'm not sure if my understanding of procedure calls is correct.
说在代码的某个地方有一个过程调用
say at some point in the code there is a procedure call
call dword ptr[123]
,该过程仅包含一个命令,请重新输入:
and the procedure consists of only one command, ret:
ret 0004
此过程调用将产生什么结果,返回值将存储在何处?我读到某处将在AX中存储2字节的返回值,但是当我用以下方式替换过程调用时:
what would be the effect of this procedure call, and where would the return value be stored? I read somewhere that a return value of 2 bytes would be stored in AX, but when I replace the procedure call by
mov AX, 0004
(连同必要的NOP一起),程序崩溃.
(together with the necessary NOPs) the program crashes.
推荐答案
在x86汇编程序中,ret
指令的参数含义:
in x86 assembler the parameter to the ret
instruction means:
RET immediate
返回调用过程,并从堆栈中弹出立即个字节.
(引自英特尔®64和IA-32体系结构软件开发人员手册 第2B卷)
因此,当您键入:
ret 0004
您要告诉CPU在call
之后立即返回指令,并从堆栈中弹出4个字节.如果您在调用之前将推入 4个字节到堆栈中,那就太好了.
You're telling the CPU to return to the instruction immediately after the call
, and to pop 4 bytes off the stack. This is great if you pushed 4 bytes onto the stack before the call.
push eax
call dword ptr[123]
请注意,这与返回值无关.实际上,Assembly中的过程无法指定值是 return 值.这一切都是按照惯例完成的.据我所知,大多数编译器都将使用EAX
来保存返回值,但这是正确的,仅是因为 calling 函数将在那里期望结果.
Note that this has nothing to do with the return value. In fact, a procedure in Assembly has no way of specifying that a value is a return value. This is all done by convention. Most compilers of which I am aware will use EAX
to hold the return value, but this is true only because the calling function will expect the result there.
因此您的呼叫代码为:
call dword ptr [123]
mov dword ptr [result], eax
,返回值4的函数将是:
and your function that returns the value 4 would be:
mov eax, 4
ret
这篇关于程序调用在汇编器中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!