ASM 堆栈帧分配大小差异

ASM stack frame allocation size difference

目前,我正在阅读 Karen Miller 的书“An assembly language introduction to computer architecture using the intel pentium”。

首先给出了callret的解释,解释了return地址的保存。 其次,解释叶过程和非叶过程之间的区别:叶过程不调用任何其他东西,而非叶过程调用。这将减少 1 个双字(32 位)space 用于堆栈帧的其余部分:

This return address is considered part of the frame for the procedure. The first doubleword within the frame is the return address. For a non-leaf procedure, the amount of space allocated for the remainder of the frame is one doubleword fewer than the size of the frame, since the call instruction allocates (and uses) space for the return address.

之后给出了一个代码示例,这让我想到了我的主要问题。

A: sub ESP, 20 ; allocate frame for A
               ; return address is at [ESP+20] in A's frame
   call B
   call C
   add ESP, 20 ; deallocate A's frame
   ret

B: sub ESP, 20 ; allocate frame for B
               ; return address is at [ESP+20] in B's frame
   call D
   add ESP, 20 ; deallocate B's frame
   ret

C: sub ESP, 12 ; allocate frame for C
   ; unnecessary cope of C's return address is at [ESP+12]
   add ESP, 12 ; deallocate C's frame
   ret

D: sub ESP, 20 ; allocate frame for D
               ; return address is at [ESP+20] in D's frame
   call D
   add ESP, 20 ; deallocate D's frame
   ret

E: sub ESP, 12 ; allocate frame for E
   ; unnecessary cope of E's return address is at [ESP+12]
   add ESP, 12 ; deallocate E's frame
   ret

过程 C 和 E 都是叶过程,因为它们不调用任何东西。为什么这些叶子程序只得到 12 位(如果我在那里也错了请纠正我)而非叶子程序得到 20 位(?)?

A compiler could use SUB ESP, something to allocate space for local variables and/or properly align the stack pointer according to some convention. In pure assembly you set the convention and can do whatever you like. No idea where the numbers 12 and 20 come from. I would ignore this and try the next chapter.

博佩尔森

Likely these code pieces are invented to implement something described later in the book, or they was got from a higher-level compiler output without proper rethinking. I hope the first variant is true, but in this case you should read the whole book before deciding on its quality

通过 Netch

That's bytes and makes no sense to me.

来自小丑