Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 My basic understanding of stack

views
     
TSMatQuasar
post Jul 11 2023, 08:33 PM, updated 3y ago

Casual
***
Validating
329 posts

Joined: Jun 2023
A picture tells the thousand words:

user posted image

In x86 systems, stack is growing to the bottom (shrinking to the top), while heap is in opposite direction.

But today I will just focus on stack. What is stack?

QUOTE
Stack is just a memory with pointer.


When we add parameters onto stack, the stack pointer decrements. We call it "push" operation.
But when we "pop" it off the stack, the stack pointer increments.
Stack is LIFO, also known as "last in first out"

Let take an example, in 32-bit, each parameter is 4 bytes.

CODE

push val1;esp - 4
push val2;esp - 8
add esp, 8


The above push 2 DWORD parameters onto stack, then adjust the stack pointer to make it balanced.
Actually the above is the same like below:

CODE

push  val1;esp - 4
push  val2;esp - 8
pop eax;contains val2
pop ebx;contains val1


After adding the "val1" and "val2" paramters onto stack, it is restored back to ebx and eax registers respectively, and the stack pointer is automatically adjusted if you use "push" and "pop", no need to increment or decrement stack pointer manually.
Look now, the last parameter, "val2" is restored back off stack first; meanwhile the first parameter, "val1" is restored back off stack the last, this is because it follows LIFO order, not FIFO (first in first out).

As a bonus, I will show how to do an alternative of "push" operation.

CODE
sub esp, 4
mov dword [esp], val1  


Now it reserves stack space of 4 bytes first, then store the "val1" parameter onto it.

Sometimes a program may reserves stack space of 32 bytes (multiple of 16) by doing like this:
CODE

sub esp, 32


Then it can do whatever it wants with the allocated stack.

To point to any of the 8 parameters ( 32/ 4 bytes = 8), it can then use ebp (points to the value of esp before allocating 32-byte stack space), or by referencing to esp directly:

CODE

mov  eax, dword [esp + 28]


This will copy first parameter pushed to stack (remember stack grows downward) to eax register.

I hope you enjoy this lesson, as what I learned in the past two years.

Disclaimer: Information herein is made accurate at the time of posting, however, corrections are welcomed, this article is written by me for LYN forum and my personal blog.

Unless otherwise stated, all content and picture are my own.


This post has been edited by MatQuasar: Jul 15 2023, 07:01 AM
TSMatQuasar
post Jul 14 2023, 09:17 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
The original post has been replaced with my own article.
If you still want to know the deleted post, it is about a cybersecurity competition: https://2023.cor.team/
flashang
post Jul 15 2023, 12:22 AM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


Stack can used as temporary variable / parameters for call.

Beside push and pop,


Many CISC-type CPU designs, including the x86, Z80 and 6502,
have a dedicated register for use as the call stack stack pointer with dedicated call,

Full document link :
Stack (abstract data type) - Wikipedia
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

smile.gif


angch
post Jul 15 2023, 09:12 AM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
Stack vs heap comes out relatively common on our Go/Rust discussions esp when re:resource allocations.

https://medium.com/eureka-engineering/under...ry-9a2631b5035d

In general we want to avoid heap allocations for variable when stack allocation would work, to reduce Garbage Collection overhead. But that depends on the data structure.

It's ingrained enough that the built-in Go benchmarking includes tracking (heap) allocs/operation.
TSMatQuasar
post Jul 15 2023, 12:00 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
QUOTE(angch @ Jul 15 2023, 09:12 AM)
Stack vs heap comes out relatively common on our Go/Rust discussions esp when re:resource allocations.

https://medium.com/eureka-engineering/under...ry-9a2631b5035d

In general we want to avoid heap allocations for variable when stack allocation would work, to reduce Garbage Collection overhead. But that depends on the data structure.

It's ingrained enough that the built-in Go benchmarking includes tracking (heap) allocs/operation.
*
Nice to know about stack vs heap in Go. Thanks for pointing out, that article said large variables are stored in GC-heap. If I read correctly, it mentioned that it is slower if allocations are done on heap.
angch
post Jul 15 2023, 03:20 PM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
QUOTE(MatQuasar @ Jul 15 2023, 12:00 PM)
Nice to know about stack vs heap in Go. Thanks for pointing out, that article said large variables are stored in GC-heap. If I read correctly, it mentioned that it is slower if allocations are done on heap.
*
Of course. Allocs on stacks are just put stuff there, advance the stack pointer. Heap allocs you need to scan for the location of free space to use.
flashang
post Jul 15 2023, 09:57 PM

Casual
***
Junior Member
355 posts

Joined: Aug 2021


QUOTE(angch @ Jul 15 2023, 09:12 AM)
Stack vs heap comes out relatively common on our Go/Rust discussions esp when re:resource allocations.

https://medium.com/eureka-engineering/under...ry-9a2631b5035d

In general we want to avoid heap allocations for variable when stack allocation would work, to reduce Garbage Collection overhead. But that depends on the data structure.

It's ingrained enough that the built-in Go benchmarking includes tracking (heap) allocs/operation.
*
IMHO,

When people rely more on GC (since GC is designed to be more efficient / or there is no malloc option),
People may forget to properly allocate what / how much they need (use malloc and free wisely),
Rather than just use and throw away.

P/S : Languages' GC is introduced to solve malloc / free issues,
so they have to solve this problem, instead of creating problems for developer.

smile.gif



This post has been edited by flashang: Jul 15 2023, 10:50 PM
TSMatQuasar
post Jul 25 2023, 09:01 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
QUOTE(MatQuasar @ Jul 14 2023, 09:17 PM)
The original post has been replaced with my own article.
If you still want to know the deleted post, it is about a cybersecurity competition: https://2023.cor.team/
*
It will start on 7/29/2023 8:00:00 AM UTC+08:00
TSMatQuasar
post Jul 29 2023, 03:48 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
QUOTE(MatQuasar @ Jul 25 2023, 09:01 PM)
It will start on 7/29/2023 8:00:00 AM UTC+08:00
*
Currently ranked 265th, easy 1 point....

user posted image
angch
post Jul 30 2023, 12:52 PM

On my way
****
Junior Member
636 posts

Joined: Jul 2006
QUOTE(MatQuasar @ Jul 29 2023, 03:48 PM)
Currently ranked 265th, easy 1 point....

user posted image
*
user posted image

user posted image

user posted image

Ez.

(ok, the rest is harder)
TSMatQuasar
post Jul 30 2023, 06:02 PM

Casual
***
Validating
329 posts

Joined: Jun 2023
QUOTE(angch @ Jul 30 2023, 12:52 PM)
user posted image

user posted image

user posted image

Ez.

(ok, the rest is harder)
*
Nice, you can make use of your discord bot!

QUOTE
misc/touch-grass
Strellic, FizzBuzz101, 0x5a
65 solves / 147 points
Vitamin D deficiency and complications from a sedentary lifestyle are plaguing the CTF community.

To help change this, the esteemed CoR tribunal has decided that touching grass in sunlight is a vital part of this CTF experience.

Trying to trick this system will result in your team being placed on a hall of SHAME - the entire world will know that you don't touch grass!

touch-grass.be.ax


 

Change to:
| Lo-Fi Version
0.0198sec    1.03    5 queries    GZIP Disabled
Time is now: 25th December 2025 - 01:10 AM