Page 1 of 1

Is the top and bottom of the stack defined backwards here?

Posted: Tue Jul 27, 2021 9:00 pm
by nightcrawler
Reading this page:
https://wiki.osdev.org/Bare_Bones

I noticed this code snippet setting the stack pointer:

Code: Select all

.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:
...
...
As this gets compiled and linked, `stack_bottom` will get a lower memory address than `stack_top`, i.e it will look like this:

Code: Select all

  Low address                                                             High address
[.......... stack_bottom ............... <-- 16 KiB -- > ......... stack_top.........]
The stack point will point to stack_top and grow toward stack_bottom.

But looking at memory layout of C programs, "bottom" should be at the high address and "top" at a lower address:
https://media.cheggcdn.com/media%2Fa00% ... plvboo.png

Shouldn't the labels be reversed in the assembly code?

Re: Is the top and bottom of the stack defined backwards her

Posted: Tue Jul 27, 2021 11:33 pm
by iansjack
The Wiki is correct.

Re: Is the top and bottom of the stack defined backwards her

Posted: Wed Jul 28, 2021 12:13 am
by klange
The use of "bottom" and "top" in combination with "growing down" can be ambiguous - who's to say the bottom of a stack that grows down isn't the downward side of it?
As long as you point the stack pointer at the correct end, the names aren't too important...