in 32bit mode, aside from the stack pointer what other register points to variables on the stack

When a function is called, a stack frame is created to support the function's execution. The stack frame contains the part's local variables and the arguments passed to the part by its caller. The frame also contains housekeeping information that allows the called function (the callee) to return to the caller safely. The exact contents and layout of the stack vary by processor architecture and office call convention.

The stack is a final in beginning out (LIFO) structure, the data stored kickoff is retrieved last. Values are placed onto the stack via button and removed via pop.

To keep track the stack, the system uses the base pointer ebp and the stack pointer esp, whereas esp points to the superlative of the stack and ebp points to the bottom of the stack. In the Intel architecture, equally plan adds information to the stack, the stack grows downwardly from high memory to depression retention. When items removed from the stack, stack shrinks upwardly from low to loftier retentivity. When a give-and-take value is pushed onto the stack, the memory address of esp decreases ii bytes and when a word value is popped off the stack, the retention address of esp increases 2 bytes. When a double discussion value is pushed onto/popped off the stack, the memory address of esp decreases/increases 4 bytes.

Stack

So when we say "tiptop of the stack" on x86, nosotros really mean the lowest address in the memory area occupied past the stack.

To push button new data onto the stack we use the push education. What push button does is first decrement esp by iv, and and then store its operand in the location esp points to.

          push eax        

is actually equivalent to this:

          sub esp,four mov [esp],eax        

Similarly, the pop education takes a value off the meridian of stack and places it in its operand, increasing the stack pointer subsequently.

          pop eax        

is actually equivalent to this:

          mov eax,[esp] add esp,4        

Function has 3 components: part prologue, office body, and function epilogue. The purpose of part prologue is to save the previous state of the program and prepare the stack for the local variables of the function. The role trunk is usually responsible for some kind of unique and specific task. This part of the function may contains various instructions, branches (jumps) to other functions, etc. The function epilogue is used to restore the program's land to its initial.

Allow'southward take a expect at an example

          ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ ;;; ; _foobar: needs 3 arguments. The arguments are pushed onto the stack before calling this part. ;;; ; ;;; ; Examples: ;;; ;          button    110 ;;; ;          push    45 ;;; ;          push    67 ;;; ;          call _foobar ;;; ; ;;; ;REGISTERS MODIFIED: EAX ;;; ;------------------------------------------------------ ;;; ;------------------------------------------------------ _foobar:     ; ebp must be preserved across calls. Since     ; this function modifies it, it must be     ; saved.     ;     push    ebp      ; From now on, ebp points to the electric current stack     ; frame of the function     ;     mov     ebp, esp      ; Make space on the stack for local variables     ;     sub     esp, 16      ; eax <-- a. eax += 2. and so store eax in xx     ;     mov     eax, DWORD[ebp+8]     add     eax, 2     mov     DWORD[ebp-4], eax      ; eax <-- b. eax += 3. and so shop eax in yy     ;     mov     eax, DWORD[ebp+12]     add together     eax, three     mov     DWORD[ebp-eight], eax      ; eax <-- c. eax += 4. and then shop eax in zz     ;     mov     eax, DWORD[ebp+16]     add     eax, 4     mov     DWORD[ebp-12], eax      ; add xx + yy + zz and store information technology in sum     ;     mov     eax, DWORD[ebp-8]     mov     edx, DWORD[ebp-iv]     lea     eax, [edx+eax]     add     eax, DWORD[ebp-12]     mov     DWORD[ebp-16], eax      ; Compute terminal result into eax, which     ; stays there until return     ;     mov     eax, DWORD[ebp-4]     imul    eax, DWORD[ebp-8]     imul    eax, DWORD[ebp-12]     add     eax, DWORD[ebp-16]      ; The get out teaching hither is equivalent to:     ;     ;   mov esp, ebp     ;   pop ebp     ;     ; Which cleans the allocated locals and restores     ; ebp.     ;     go out     ret  department .text         global _start _start:      push    34      push    59      push button    98      phone call    _foobar       mov     ebx, 0      mov     eax, 1      int     0x80        

Let'due south examine the stack

          push    34 push    59 push    98        

First, iii values are pushed onto the stack

Stack

          phone call _foobar        

Side by side, we telephone call the function _foobar. To return to the caller, a function must have the correct render address. call pedagogy will push the return address onto the stack before jumping to the target address.

Stack

          push ebp        

This instruction saves the value of register ebp to the stack.

Stack

          mov ebp,esp        

This instruction volition make register ebp point to the aforementioned location as register esp.

Stack

          sub esp,16        

This instruction makes empty space on the stack for local variables.

Stack

          mov     eax, DWORD[ebp+8] add     eax, 2 mov     DWORD[ebp-4], eax        

Stack

          mov     eax, DWORD[ebp+12] add     eax, three mov     DWORD[ebp-8], eax        

Stack

          mov     eax, DWORD[ebp+16] add together     eax, 4 mov     DWORD[ebp-12], eax        

Stack

          mov     eax, DWORD[ebp-8] mov     edx, DWORD[ebp-4] lea     eax, [edx+eax] add     eax, DWORD[ebp-12] mov     DWORD[ebp-16], eax        

Stack

          mov     eax, DWORD[ebp-4] imul    eax, DWORD[ebp-eight] imul    eax, DWORD[ebp-12] add     eax, DWORD[ebp-16]        

Stack

          leave        

After executing teaching above, local variables will exist removed from the stack, annals esp volition bespeak to the return address, and register ebp volition be dorsum to its initial value.

Stack

          ret        

The ret instruction pops the return accost off the stack and returns control from a part to the calling program.

Stack

huffmanwitena.blogspot.com

Source: https://grandidierite.github.io/functions-and-stack-in-NASM-32-bit/

0 Response to "in 32bit mode, aside from the stack pointer what other register points to variables on the stack"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel