Where and when create 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
pepito

Where and when create a STACK

Post by pepito »

I copy a boot sector and use it to load a very simple OS kernel. This boot sector have at the begin this lines:

cli
mov     ax, 0x07C0
mov     ds, ax
mov     es, ax
mov     fs, ax
mov     gs, ax

; Create a stack..

mov     ax, 0x0000
mov     ss, ax
mov     sp, 0xFFFF
sti

Then, when I load the kernel (and after switching to protected mode) the code have this lines:

mov ax, DATA     ; DATA is the value of a selector into the GDT
mov ds, ax
mov ss, ax       ; <- (?)
mov es, ax
mov fs, ax
mov gs, ax

My questions:

1) My kernel use the STACK created at the boot sector time? It is correct?
2) I nead assign a value to the 'esp' register? What value is necesary?
3) May I create a STACK descriptor at the GDT?
4) Where can I get information about the STACK strategy I must to follow?

Thank you very much!
jamethiel

RE:Where and when create a STACK

Post by jamethiel »

In order:

1) Sometimes. My system reloads the stack as one of the first things it does, but it puts one of them at the same place as the one set up by the bootsector.
2) A pointer to the high end of the area that you want your stack in.
3) You can. Most people don't, preferring to use the same descriptor for the ds, es and ss registers.
4) There is no stack strategy that you -must- follow. It is theoretically possible to write a system that doesn't use a stack at all.

Hope this helps.

--Jamethiel
Anton

RE:Where and when create a STACK

Post by Anton »

"It is theoretically possible to write a system that doesn't use a stack at all"-this is a lie.
Anton.
jamethiel

RE:Where and when create a STACK

Post by jamethiel »

By "lie", you mean "goes against your preconceptions", right?

I know -exactly- how to write such a beast for the PC. It would be of no practical value except as a proof of point or excersize in programming masochism, but it -is- doable.

Yes, you have to use polled-mode device drivers, because you can't have interrupts without a stack. No, you can't use CALL/RET. No, you can't have reentrant functions. Yes, you have to explicitly store the return address for a function you call. Yes, it would be a royal pain in the @$$. But that doesn't mean it can't be done.

--Jamethiel
Anton

RE:Where and when create a STACK

Post by Anton »

Since you are talking about theory, then what is a stack - a stack is method to remember the state of the machine(The CPU itself is a finit state machine -  it has a finit number of regs and units). So, one way ot the other, you'll have a stack - software or hardware. And it does not matter if special instructions are used. And if there not, it does not mean, that you are not using a stack. For example, a software stack is used in compilers, and that does not mean, that a stack is not used, it is.
Anton.
jamethiel

RE:Where and when create a STACK

Post by jamethiel »

Thank you, sir, for redefining "stack" in support of your argument.

When you're ready to discuss this without resorting to deconstructionism to "prove" your point let me know. Until then I have no desire to waste my time.

(Well, except maybe by writing a PC OS that doesn't use a stack. That might be fun.)

--jamethiel
Squrky

RE:Where and when create a STACK

Post by Squrky »

I do love it when the kiddies bicker :) I read somewhere that when the processor receives any interrupt that requires it to drop what its doing and call a handler, it saves its current execution state. Guess where! The program stack. Correct me if I am wrong, but it would be very hard to write an OS that doesn't use any interrupts at all ;)
Tim Robinson

RE:Where and when create a STACK

Post by Tim Robinson »

jamethiel did mention that:

"Yes, you have to use polled-mode device drivers, because you can't have interrupts without a stack."

Yes, it is possible to do anything without a stack. Anything you can do on one Turing-complete system (a CPU with a stack) you can do on another (a CPU without a stack). It's just harder to do some things on one than on another.
pepito

RE:Where and when create a STACK

Post by pepito »

Thank you, for everybody !

pepito
Post Reply