Greetings and Noob Questions

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.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post 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.
Rhob
Posts: 22
Joined: Fri Apr 13, 2007 10:08 am
Location: Florida

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
Rhob
Posts: 22
Joined: Fri Apr 13, 2007 10:08 am
Location: Florida

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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.
Every good solution is obvious once you've found it.
Post Reply