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

时间:2016-12-08 23:12:40

标签: assembly stack cpu-registers masm32

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

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

所以我需要了解堆栈中的内容。

我最好猜测一切都在哪里(下面是文字堆栈)

My Attempt 2.0

1 个答案:

答案 0 :(得分:1)

堆栈看起来像:

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
$5 = 0x80483e5         # This is the return address (call is 5 bytes)
(gdb) p/x $ebp
$6 = 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
$7 = 0xffffd9ec        # This is the current ebp
(gdb) p/x $esp
$8 = 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是堆栈段,它由操作系统预设,它不会指向任何地方,但基地址为0并且不会更改。

请注意enter x, 0相当于:

push ebp
mov ebp, esp
sub esp, x