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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
nightcrawler
Posts: 17
Joined: Thu Aug 08, 2019 8:21 am

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

Post 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?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post by iansjack »

The Wiki is correct.
klange
Member
Member
Posts: 679
Joined: Wed Mar 30, 2011 12:31 am
Libera.chat IRC: klange
Discord: klange

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

Post 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...
Post Reply