Page 1 of 1

Setting up a stack ...

Posted: Thu Mar 21, 2002 12:00 am
by The Legend
Well I've tried learning it by looking at the
sources from GazOS, but it makes no sense to me
...

RE:Setting up a stack ...

Posted: Fri Mar 22, 2002 12:00 am
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.

RE:Setting up a stack ...

Posted: Sat Mar 23, 2002 12:00 am
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.

RE:Setting up a stack ...

Posted: Sat Mar 23, 2002 12:00 am
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