Why does first parameter in x86 assembly starts from offset 8?
Asked Answered
F

1

6

I'm trying to develop understanding of Assembly language. I understand that when function creates stack frame, it pushes current EBP, than copies stack pointer value to the EBP. First (and only) function parameter is accessed by EBP + 8. But why 8? Next value after pushed EBP is logically offset 4. I read many webpages, but it seems I don't understand this part.

Faunia answered 13/3, 2017 at 19:7 Comment(0)
Y
12

The "missing" DWORD is the return address. The call stack looks like:

ebp     : saved ebp
ebp + 4 : return address
ebp + 8 : pushed parameter

And then if the function uses local variables, since stack space is (typically) reserved for those after the stack frame, they are referenced as ebp - xx:

ebp - 8 : second local
ebp - 4 : first local
ebp     : saved ebp
ebp + 4 : return address
ebp + 8 : pushed parameter
Yerkovich answered 13/3, 2017 at 19:10 Comment(2)
Thank you. I focused on the wrong part (read about calling conventions, but forgot basic thing)Faunia
Is worth mentioning that the stack in this question grows downward that's why you substrate local variables from ebp which were pushed after arguments and return address.Sebi

© 2022 - 2024 — McMap. All rights reserved.