您的位置:首页 > 其它

nasm中的enter

2015-10-11 19:18 323 查看

nasm手册:B.4.65    enter:     Create stack frame

ENTER imm,imm                 ; C8 iw ib             [186]

ENTER
constructs a
stack frame
for a high-level language procedure call. The first operand (the
iw
in the opcode definition above refers to the first operand) gives the amount of
stack space to allocate for local variables; the second (the
ib
above) gives the nesting level of the procedure (for languages like Pascal, with nested procedures).

The function of
ENTER
, with a nesting level of zero, is equivalent to

PUSH EBP            ; or PUSH BP         in 16 bits
MOV EBP,ESP         ; or MOV BP,SP       in 16 bits
SUB ESP,operand1    ; or SUB SP,operand1 in 16 bits

This creates a stack frame with the procedure parameters accessible upwards from
EBP
, and local variables accessible downwards from
EBP
.

With a nesting level of one, the stack frame created is 4 (or 2) bytes bigger, and the value of the final frame pointer
EBP
is accessible in memory at
[EBP-4]
.

This allows
ENTER
, when called with a nesting level of two, to look at the stack frame described by theprevious value of
EBP
, find the frame pointer at offset -4 from that, and push it along with its new frame pointer, so that when a level-two procedure is called from within a level-one procedure,
[EBP-4]
holds the frame pointer
of the most recent level-one procedure call and
[EBP-8]
holds that of the most recent level-two call. And so on, for nesting levels up to 31.

  翻译并增加些解释:

 ENTER指令为高级语言的调用过程构造所必须的栈帧。第一个操作数(机器指令码中的iw(也就是伪指令中的imm))给出为本地参数所需要的栈帧空间的大小(此子函数没有用到本地局部变量,股此处的栈帧控件大小为0),第二个参数给出进程调用的嵌套级别(对于一些编程语言,像C(或者原文中的Pascal)语言之类的,都是支持函数的嵌套调用的)。

  enter指令的功能,对于指定嵌套级别为0,功能等价于一下几条汇编指令:

PUSH EBP            ; or PUSH BP         in 16 bits
MOV EBP,ESP         ; or MOV BP,SP       in 16 bits
SUB ESP,operand1    ; or SUB SP,operand1 in 16 bits

上述指令构建出一个栈帧,栈帧大小为operand1.在EBP之上,是进程的参数,在EBP之下,是本地变量。

对于嵌套级别为1的函数的栈帧,栈帧要比嵌套级别为0的大4(或2,对于16为的来说)个字节,最后一个嵌套层的EBP可以在第0层的EBP地址减小4位后找到(注意栈是向下增长的)。

当enter调用嵌套级别为2的内嵌函数时,通过查找寄存器EBP描述的栈帧指针的以前的值,查找栈指针偏移量为-4的地址值,把这个值和新的栈指针值压栈,如此一来,当嵌套级别为2的进程调用嵌套级别为1的进程时,[EBP-4]保存最近的嵌套级别为1的栈指针,[EBP-8]保存级别为2的指针。按照这个规律,可以支持的最大嵌套级别为31层。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nasm enter功能解释