Setting up a stack ...

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
The Legend

Setting up a stack ...

Post by The Legend »

Well I've tried learning it by looking at the
sources from GazOS, but it makes no sense to me
...
K.J.

RE:Setting up a stack ...

Post by K.J. »

>On 2002-03-21 15:11:19, The Legend wrote:
>Well I've tried learning it by looking at the
>sources from GazOS, but it makes no sense to me
>...
Here's what's in Chris Giese's kstart.asm

== start of snipt ==

; stop using bootloader GDT, and load new GDT
lgdt [gdt_ptr]

mov ax,LINEAR_DATA_SEL
mov ds,ax
mov es,ax
mov ss,ax
mov fs,ax
mov gs,ax
jmp LINEAR_CODE_SEL:sbat
sbat:

; zero the C language BSS
; 'bss' and 'end' are defined in the linker script file
EXTERN bss, end
mov edi,bss
mov ecx,end
sub ecx,edi
xor eax,eax
rep stosb

mov esp,stack ; set up stack

== bunch of code left out ==

SECTION .bss

resd 1024
stack:

== end of snippit ==

That's how his sets up a stack. However, I don't get how the code works exactly. He declares "stack" AFTER a bunch of space is made. It seems to me that it should be BEFORE the space but I'm not sure.

K.J.
Marc Schütz

RE:Setting up a stack ...

Post by Marc Schütz »

>On 2002-03-22 23:36:23, K.J. wrote:
>SECTION .bss
>
> resd 1024
>stack:
>
>== end of snippit ==
>
>That's how his sets up a stack. However, I don't get how the code works exactly. He declares "stack" AFTER a bunch of space is made. It seems to me that it should be BEFORE the space but I'm not sure.
>
>K.J.

The way the stack is set up here is correct:

On the x86 the stack grows DOWN, not up
=> whenever you push something on the stack,
the stack pointer is DECREMENTED.

Therefore the stack pointer has to point to the
end of the stack.
J. Weeks

RE:Setting up a stack ...

Post by J. Weeks »

>
>The way the stack is set up here is correct:
>
>On the x86 the stack grows DOWN, not up
>=> whenever you push something on the stack,
>the stack pointer is DECREMENTED.
>
>Therefore the stack pointer has to point to the
>end of the stack.

Indeed.

But, just to play devil's advocate, and to make sure
some people new to this don't get the wrong idea; this
isn't necessarily true in pmode. You can, in fact,
have stacks that grow up, like a regular data
segment. It's defined the the segment descriptor.

Jeff
Post Reply