C++ For Setup

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
Tolga

C++ For Setup

Post 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. :D

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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C++ For Setup

Post 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.
Tolga

Re:C++ For Setup

Post by Tolga »

Oh. I m sorry. Yes, It is 16 bit.
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C++ For Setup

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

Re:C++ For Setup

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

Re:C++ For Setup

Post 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...
Every good solution is obvious once you've found it.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C++ For Setup

Post 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")
khamidou

Re:C++ For Setup

Post 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 :

Code: Select all


asm(".code16");

This will tell gas to create 16bit code ...

It works , i used to write .COM applications with gcc on linux !
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C++ For Setup

Post by Solar »

:o

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

Re:C++ For Setup

Post 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
perl

Re:C++ For Setup

Post by perl »

Try using 32bit real mode.
aka flat real mode or unreal mode.
Post Reply