汇编 - 堆栈、程序、ESP、EBP、SS - 帮助我理解

Assembly - Stack , Procedures, ESP, EBP, SS - Help me understand

我很难理解这里发生了什么,就堆栈上的内容而言,以及寄存器 ESP、EBP 和 SS 在代码中指向此处的位置。 这是我的代码

include \masm32\include\masm32rt.inc
.data?
    value DWORD ?
.code
start:
    push 42
    push 5
    call xyz
    mov value, EAX
    print str$(value)
    exit
xyz:
    enter 4, 0
    ; HERE
    leave
    ret 8
end start

所以我需要查看堆栈中的内容。

这是我对所有内容的最佳猜测(下面是文本堆栈)

堆栈看起来像:

42
5
return address
previous ebp pushed by "enter"; new ebp points here
4 uninitialized bytes due to "enter"; esp points here

您当然可以在调试器中看到这一点:

6       push 42
(gdb) s
start () at test.s:7
7       push 5
(gdb) 
start () at test.s:8
8       call xyz
(gdb) p/x $eip+5
 = 0x80483e5         # This is the return address (call is 5 bytes)
(gdb) p/x $ebp
 = 0xffffda78        # This is the ebp in the caller
(gdb) s
xyz () at test.s:11
11      enter 4, 0
(gdb) 
12      leave
(gdb) p/x $ebp
 = 0xffffd9ec        # This is the current ebp
(gdb) p/x $esp
 = 0xffffd9e8        # This is esp
(gdb) x/x $esp
0xffffd9e8: 0x0804841b # Top of stack, 4 garbage bytes, esp points here
(gdb) x
0xffffd9ec: 0xffffda78 # Saved ebp, current ebp points here
(gdb) x
0xffffd9f0: 0x080483e5 # Return address
(gdb) x
0xffffd9f4: 0x00000005 # argument "5"
(gdb) x
0xffffd9f8: 0x0000002a # argument "42"

SS是堆栈段,由OS预置,不指向任何地方,但基址为0,不会改变。

注意 enter x, 0 等同于:

push ebp
mov ebp, esp
sub esp, x