Trying to make a simple PMODE kernel in DJGPP

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
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Trying to make a simple PMODE kernel in DJGPP

Post by inflater »

Hi all,
well, if you are all programming in Protected Mode and you suggest to make a OS in DJGPP, then I'd like to make a simple kernel in it too. (Well, I had to search in my memory for C++ language. :D All my mind is concentrated to Pascal)

OK. I downloaded DJGPP with C++ support from http://osdever.net, set up enviroment variables and come up with a tiny Hello World program:

Code: Select all

#include <conio.h>
#include <stdio.h>

void main()
{
cprintf("Hello World !");
getch();
}
I compiled and linked this program with this simple syntax:
gcc hello.c -o hello.exe
It worked, in Windows and DOS, though. Application's size was incredibly 109 kB large and it didn't work under Alexei Frounze's bootloader.

So. What to do next? Does DJGPP somehow switch *automatically* to PMODE or I must do it myself?
Or I've used wrong functions and I must make my own to access Video RAM?
How can I please reduce the file size? My kernel in Pascal (uncompressed by UPX) has with at least *some* functions has 55 kB !

Thanks for any answers.
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

I would strongly recommend reading through Bran's Kernel Development Tutorial. It uses GRUB as the boot-loader and takes you through creating a C kernel.

Firstly, a few points:
* You will need to write a stub in asm and write a linker script so that you know where the code entry point is (and the data, bss etc.. are in the right place).

* Don't include any header files if you didn't write them yourself. The options to compile without any standard libraries are shown in the tutorial above. This means you will have to write your own printf, getch etc... (including a basic display and keyboard driver).

Sorry if any of this is patronising if you have previously done a kernel in Pascal but I don't know what level you're at :?:

Hope this helps,
Adam
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Yeah, inflater those functions are in DJGPP's C library.. You can't utilize them, You have to write your own. (I'm surprised you didn't know that already..)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post by inflater »

(I'm surprised you didn't know that already..)
Well, in Pascal it was slightly different:
You can include unit CRT to "transform" commands WriteLn,ReadLn,Delay,Sound etc. to BIOS - they didn't use INT 21h anymore.

So I forgetted about this.

Thanx for responses. Anyway - how can I reduce the file size without using UPX? It is very, very large (well, Hello World in DJGPP and more than 100 kB?!)

Thanx
inflater
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Your linking the binary...

And linking with the C library..

gcc -ffreestanding -c kernel.c -o kernel.o

But -freestanding won't work with g++ (Your above code is C anyway..)

Remember, You then need to link the kernel with a stub something like this:

Code: Select all

[BITS 32]
[global start]
[extern kernel]

start:
   call kernel ; You C function..
   jmp $ ; Crash the kernel
And use NASM to compile that.. Then use LD with a linker script to link stub with kernel.o..

But all these steps have been posted in the past (Likely better steps).. Using a test C kernel (Brans?) would likely be a good template.
Last edited by Brynet-Inc on Mon Dec 18, 2006 2:19 pm, edited 1 time in total.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Try getting your linker to output flat binary. Also, when you don't have those includes you'll find the file size decreased considerably.

Still expect the final version to appear fairly large (although not 100k!) - especially if all your sections are 4k aligned.

Cheers,
Adam
Post Reply