进入并离开装配体? [英] ENTER and LEAVE in Assembly?
问题描述
我正在阅读汇编语言的艺术(Randall Hyde,链接到Amazon ),我在那本书中尝试了一个控制台应用程序.它是一个使用Win32 API函数为其自身创建了新控制台的程序.该程序包含一个名为LENSTR
的过程,该过程将字符串的长度存储在EBP
寄存器中.该函数的代码如下:
I was reading The Art of Assembly Language (Randall Hyde, link to Amazon) and I tried out a console application in that book. It was a program that created a new console for itself using Win32 API functions. The program contains a procedure called LENSTR
, which stores the length of string in the EBP
register. The code for this function is as follows:
LENSTR PROC
ENTER 0, 0
PUSH EAX
;----------------------
CLD
MOV EDI, DWORD PTR [EBP+08H]
MOV EBX, EDI
MOV ECX, 100 ; Limit the string length
XOR AL, AL
REPNE SCASB ; Find the 0 character
SUB EDI, EBX ; String length including 0
MOV EBX, EDI
DEC EBX
;----------------------
POP EAX
LEAVE
RET 4
LENSTR ENDP
您能在这里解释enter
和leave
命令的用法吗?
Could you explain the usage of the enter
and leave
commands here?
推荐答案
这是该函数的堆栈框架(激活记录)的设置.内部通常看起来像这样:
This is the setup for the stack frame (activation record) for the function. Internally it normally looks something like this:
push( ebp ); // Save a copy of the old EBP value
mov( esp, ebp ); // Get ptr to base of activation record into EBP
sub( NumVars, esp ); // Allocate storage for local variables.
然后,当要再次销毁堆栈框架时,您必须执行以下操作:
Then when the stack frame is to be destroyed again, you have to do something along the following lines:
mov( ebp, esp ); // Deallocate locals and clean up stack.
pop( ebp ); // Restore pointer to caller's activation record.
ret(); // Return to the caller.
此处是使用HLA对其的更好解释.尽管您正在阅读的书中对它的解释很充分,但我也有这本书,而且我已经阅读了解释它的部分.
Here is a better explanation of it using HLA. Though it is well explained in the book you're reading, as I have that book too, and I've read the section explaining it.
这篇关于进入并离开装配体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!