Page 1 of 1
C++ For Setup
Posted: Tue Jul 04, 2006 12:49 am
by Tolga
Hi all. My os has got 3 parts.
1. Boot: Activates A20 and loads setup.
2. Setup: Loads File System and finds Kernel in FS, Loads GDT, Enters Protected Mode and execute kernel
3. Kernel: Everybody knows this.
My problem, i'm using assemby to write setup. So it is some hard. Can i write in C++(With Some Inline Assembly)? It is 32 Bit. But it is not in protected mode.
Re:C++ For Setup
Posted: Tue Jul 04, 2006 12:59 am
by Pype.Clicker
sorry if that sounds dull, but how can it be 32-bit if it's not protected mode ? If you use something like "mov eax,[ebx+ecx*8]" in a "bits 16" code section, your assembler normally prepends "db 66h, 67h" before your instruction. A 32 bit compiler will just write the opcodes for "mov eax,[ebx+ecx*8]" and will only need "db 66h" for e.g "mov ax,0x1234" and "db 67h" for "mov [bp+6],4".
Beside the "encoding" difficulty, your compiler will assume a return address of 4 bytes while it will only be 2 bytes (messing up the arguments access)...
So, yes, you can write your "setup" (aka second stage loader) in C++, but you're really better to use a 16-bit compiler then ... and yes, that rules out GCC ...
Now, alternatively, you may prefer to leave second stage to GRUB ... that's completely up to you.
Re:C++ For Setup
Posted: Tue Jul 04, 2006 1:25 am
by Tolga
Oh. I m sorry. Yes, It is 16 bit.
Re:C++ For Setup
Posted: Tue Jul 04, 2006 1:35 am
by Solar
So the answer is, "not with GCC (perhaps with some other C++ compiler)". No, I don't know one capable of generating 16bit code.
Re:C++ For Setup
Posted: Tue Jul 04, 2006 2:48 am
by viral
Hello..
Turbo C++ can serve your purpose. It generates 16-bit code. I have never tried to generate 16-bit code as my OS boots in protected mode only..
Try it yourself, whether codes generated by Turbo C++ helps you.. Inline assembly of Turbo C++ is intel based, so quite easy to write..
Re:C++ For Setup
Posted: Tue Jul 04, 2006 3:12 am
by Solar
Be aware that TurboC++ is not standard C++ compliant, but "only" the AT&T 2.0 level.
Things already in AT&T 2.0 C++: virtual functions, overloading, references, constants, overloadable new, improved type safety, multiple inheritence, abstract classes, static member functions, constant member functions, [tt]protected[/tt].
Things not available in AT&T 2.0 C++ include templates, exceptions, namespaces, C++-type casts, and the bool type...
Re:C++ For Setup
Posted: Tue Jul 04, 2006 3:37 am
by Pype.Clicker
in clear, take the time to think about it...
- you'll end up with C++ code that doesn't exactly behave like C++
- anything explicitly related to DOS will not work
- there may be implicit dependencies to DOS hidden here and there in the runtime library (cout and cin, most likely)
- you'll have to find a way to convert the executable into a binary ... most likely, you'll need BootProg to run your 2nd stage.
all these can be fixed, accomodated ... but i guess that will explain why so many of us practice grudo (as in "the way of the GRUB")
Re:C++ For Setup
Posted: Tue Jul 04, 2006 7:00 am
by khamidou
of course you can use gcc to write 16bit code. The only thing you'll have to do will be to append the following line at the start of every c
file used by your bootloader :
This will tell gas to create 16bit code ...
It works , i used to write .COM applications with gcc on linux !
Re:C++ For Setup
Posted: Tue Jul 04, 2006 7:10 am
by Solar
That statement does
not tell GCC it is targeting a 16bit machine. It will happily continue to ignore code / data segment sizes, using 32bit far pointers and whatnot.
It's just like using C++ for kernel space without knowing
exactly what you are up to: It will actually work at first, but you will run into all kinds of weirdness once things grow beyond a certain complexity / size.
And while you are at it trying that, at least use
.code16gcc... (and note the "experimental"...)
Re:C++ For Setup
Posted: Wed Jul 05, 2006 8:39 am
by oswizard
Or you can do what I have done with my OS: Write a short stub that setus up paging and protected mode in assembler, and append a full 32-bit setup module to it.
The GDT still has to be set up in assmebly, although mine is hardcoded. I set up a new GDT in my kernel later on.
My second-stage loader is a 32-bit PE file built by VS2005 with a hand-crafted 1024-byte header (I have both 32-bit and 64-bit versions) that transitions from RM -> PM+paging.
By making it all part of the same file, your bootloader only has to load one file
From the 32-bit code, you can return to real mode to call any BIOS function you need (ie. floppy disk reads, memory config) to get your OS to the point where it can 'break free' from the BIOS.
I don't know if this is what you wanted, but it allows for the minimum amount of assembly and maximizes C++. And you don't have to deal with old C++ compilers for DOS and near and far pointers, tiny vs. small vs. ... memory models, etc.
Just a suggestion,
Mike
Re:C++ For Setup
Posted: Tue Jul 11, 2006 12:41 pm
by perl
Try using 32bit real mode.
aka flat real mode or unreal mode.