Page 2 of 2

Posted: Wed May 02, 2007 8:46 pm
by neon
How exactly does one create the stack? Does it have to be done in assembly (if so, why)?
You need to set the stack segment registers to point to the
stack, so yes it must be done in assembly.

In my OS, I set my stack from 09000h to 0FFFFh. Coinsidering the
importance of the stack, we have to insure we have enough memory
for it, so the more the better.

To set the stack, just set SS and SP:

Code: Select all

mov	ax, 09000h
mov	ss, ax	
mov	sp, 0FFFFh
Insure interrupts are cleared before this executes (with cli instruction)
What method(s) do you recommend for accessing the x86 CPU registers in C/C++?
This is dependent on the compilier. With GCC, I prefer using inline asm.

Posted: Wed May 02, 2007 9:13 pm
by Rhob
D'oh, I should've known better -- I remember reading about that now. Just to get things straight: the SS register points to the start of the stack memory segment, the SP register points to the current "top" of the stack, and the stack "grows" downwards.

Given the x86 segmented-memory model, the largest stack size would seem to be 64K. Just out of curiosity, why did you choose segment 09000h as the stack segment?

Thanks for your reply!

- Rob

Posted: Thu May 03, 2007 2:51 am
by Solar
RHaden wrote:What is the recommended stack size to create for the OS?
Large enough. 8) Seriously, we cannot know how much stack space your OS will require, as this is very much depending on your programming / design style. You either set the stack large enough for a "worst case" scenario, or you add some mechanism to expand the stack size in case of an overflow.
How exactly does one create the stack?
Setting SS and ESP registers to appropriate values. The rest is up to the CPU (push / pop).
Does it have to be done in assembly (if so, why)?
Yes, because in C you don't have access to CPU-specific registers (like SS / ESP).
What method(s) do you recommend for accessing the x86 CPU registers in C/C++?
Macros - or inline functions - doing inline assembly.

Posted: Thu May 03, 2007 6:10 am
by Rhob
Thanks for your reply, Solar.

I'm curious -- how can you use macros to access the CPU registers? That method seems to have been used in the old bios.h and dos.h header files.

- Rob

Posted: Thu May 03, 2007 6:42 am
by Solar
As I said, using inline assembly. (Check the GCC docs or the Wiki for details.) The idea is to hide the actual inline assembly behind some macros in an include file, instead of scattering inline ASM all through your sources.