Talk:X86 Disassembly/Functions and Stack Frames

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Using esp for automatic variables?

Actually, I would think that programs use ebp for automatic variables, because if esp is used, the offset used will keep on changing as you push and pop things. For example:

push ebp
mov ebp, esp
sub esp, 4
mov dword [esp+0], 123 ; using our variable
push 321
mov eax, [esp+4] ; we have to use 4 as an offset, because [esp+0] contains 321.
push 432 ; and now the offset to the variable is 8
add esp, 8 ; offset is back to 0
mov esp, ebp
pop ebp
ret

However, I could use [ebp-4] as long as I like to reference the variable.

Wj32 10:22, 8 January 2007 (UTC)

My experience looking at compiled code under the debugger would seem to bear this out; auto variables are always referenced as offset from ebp for exactly this reason.
One thing never mentioned in the article is that in modern compilers, not even is the order of declarations of auto variables not mirrored in their allocation on the stack, a specific offset from ebp may, at some times, be one auto variable, sometimes another... and some auto variables are always in registers, never appearing on the stack at all. Chazz (talk) 07:42, 17 May 2009 (UTC)

[edit] Isn't EBP-4 the first local variable?

the tutorial uses

a = [ebp - 12] = [esp], b = [ebp - 8] = [esp + 4], c = [ebp - 4] = [esp + 8]

to explain

int a,b,c;

Wouldn't it be

a = [ebp - 4] = [esp], b = [ebp - 8] = [esp + 4], c = [ebp - 12] = [esp + 8]

though,
seeing that right there at the "crude" graphical representation of a stack it says that the first local variable is at ebp - 4?
Nothingist (talk) 07:54, 28 August 2009 (UTC)

this page appears to backup this up, that ebp-4 = 1st local parameter, ebp-8 = 2nd local parameter:
http://www.cs.uaf.edu/2008/fall/cs301/support/x86/index.html