why preserve stack space for local variables?

the accountant

I'm new to assembly language and I was wondering about local variables, why do we (or the compilers) preserve a space for them on the stack usually by decrement the "ESP" register at the prologue of the procedure and as the procedure ends we assign the "ESP" it's old value again. like this code sample :

; a procedure that create the stack frame then assign 10, 20 values for two local variables then return to caller

two_localv_proc PROC
push ebp
mov ebp,esp
sub esp,8
mov DWORD PTR [ebp-4],10
mov DWORD PTR [ebp-8],20
mov esp,ebp
pop ebp
ret
two_localv_proc ENDP

the last code snippet will do exactly if we removed the (sub esp,8) line and the (mov esp,ebp) line, to be as this

 two_localv_proc PROC
push ebp
mov ebp,esp
mov DWORD PTR [ebp-4],10
mov DWORD PTR [ebp-8],20
pop ebp
ret
two_localv_proc ENDP

so why we (or the compilers) do such behavior! , why don't we just use the stack memory to store our local variables as long as the "ESP" pointer will not affected by storing values on the stack with codes like:

mov DWORD PTR [ebp-8],20
Jester

In general, you can only use the stack above the stack pointer. The stack pointer defines the end of the stack. Accessing under the stack pointer may or may not work. It especially won't work if you call another function, since the return address would be pushed and also the called function would start using the stack from the stack pointer, thereby overwriting your locals. Even in leaf functions, asynchronous things such as signal handlers may use the stack, and they also assume everything under the stack pointer is unused.

Furthermore, the OS may be growing your stack on-demand, and it also uses the stack pointer for that. If you access under the stack pointer, the memory might not even be mapped, and if the OS catches you doing that your program will crash.

Note that some calling conventions, such as the x86-64 abi, allow for a so-called red zone under the stack pointer. This area is guaranteed to be unmodified and can be used in leaf functions for the locals without adjusting the stack pointer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do local variables get stored in stack?

From Dev

Why are local variables not set to zero?

From Dev

Stack overflow from local variables?

From Dev

why xml:space="preserve" is not honored by php xslt transformation

From Dev

PDB: How to inspect local variables of functions in nested stack frames?

From Dev

Compiling local variables for a stack machine

From Dev

Prolog out of local stack space/ infinite recursion

From Dev

Examining local variables up the stack

From Dev

How memory space behave after function return for local variables?

From Dev

What is the idea behind using a stack for local variables?

From Dev

How does gcc push local variables on to the stack?

From Dev

Local variables on stack

From Dev

How to insist a C compiler put local variables on the stack, not in registers

From Dev

Are variables local to a class that are not declared as pointers, created on the heap or stack?

From Dev

GCC reserving more space than needed for local variables

From Dev

Local variables: are they always on the stack?

From Dev

How can I extract local variables from a stack trace?

From Dev

Linux process stack overrun by local variables (stack guarding)

From Dev

Why JVM uses heap for objects and static variables and Stack for local variables and function call?

From Dev

Allocation of space for static variables on the stack

From Dev

How does initialization of local variables (large arrays) affect stack size?

From Dev

Compiling local variables for a stack machine

From Dev

Examining local variables up the stack

From Dev

How memory space behave after function return for local variables?

From Dev

why preserve stack space for local variables?

From Dev

Why do the following ways to preserve environment variables for sudo not work?

From Dev

how to reference local variables on the stack properly

From Dev

Making a space for local variables in assembly

From Dev

Preserve White Space

Related Related

  1. 1

    How do local variables get stored in stack?

  2. 2

    Why are local variables not set to zero?

  3. 3

    Stack overflow from local variables?

  4. 4

    why xml:space="preserve" is not honored by php xslt transformation

  5. 5

    PDB: How to inspect local variables of functions in nested stack frames?

  6. 6

    Compiling local variables for a stack machine

  7. 7

    Prolog out of local stack space/ infinite recursion

  8. 8

    Examining local variables up the stack

  9. 9

    How memory space behave after function return for local variables?

  10. 10

    What is the idea behind using a stack for local variables?

  11. 11

    How does gcc push local variables on to the stack?

  12. 12

    Local variables on stack

  13. 13

    How to insist a C compiler put local variables on the stack, not in registers

  14. 14

    Are variables local to a class that are not declared as pointers, created on the heap or stack?

  15. 15

    GCC reserving more space than needed for local variables

  16. 16

    Local variables: are they always on the stack?

  17. 17

    How can I extract local variables from a stack trace?

  18. 18

    Linux process stack overrun by local variables (stack guarding)

  19. 19

    Why JVM uses heap for objects and static variables and Stack for local variables and function call?

  20. 20

    Allocation of space for static variables on the stack

  21. 21

    How does initialization of local variables (large arrays) affect stack size?

  22. 22

    Compiling local variables for a stack machine

  23. 23

    Examining local variables up the stack

  24. 24

    How memory space behave after function return for local variables?

  25. 25

    why preserve stack space for local variables?

  26. 26

    Why do the following ways to preserve environment variables for sudo not work?

  27. 27

    how to reference local variables on the stack properly

  28. 28

    Making a space for local variables in assembly

  29. 29

    Preserve White Space

HotTag

Archive