Writing *External* programs for my kernel

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
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Writing *External* programs for my kernel

Post by t0xic »

I have already made my kernel, and it works great, but I was wondering how to execute programs and how to start them... Here is my compile script and I was wondering how I could get a simple program like this to run:

Code: Select all

int progstart(void)
{
    clrscr();
    kprint("@",6,0x02);
}
Here is the compile script

Code: Select all

@echo off
Echo -- Compiling Bootsecter --
tools\nasmw -f bin bootsect.asm -o bootsect.sys
Echo.
Echo -- Compiling Loader --
tools\nasmw -f aout loader.asm -o loader.sys
Echo.
Echo -- Compiling Kernel Source --
tools\gcc -ffreestanding -c main.c -o main.o
tools\gcc -c video.c -o video.o
tools\gcc -c ports.c -o ports.o
tools\gcc -c prog.c -o prog.o
Echo.
Echo -- Linking Kernel --
tools\ld -e _main -Ttext 0x1000 -o kernel.k main.o video.o ports.o
tools\ld -i -e _main -Ttext 0x1000 -o kernel.k main.o video.o ports.o
tools\ld -e _progstart -Ttext 0x1000 -o program.o kernel.k prog.o
tools\objcopy -R .note -R .comment -S -O binary kernel.k kernel.sys
tools\objcopy -R .note -R .comment -S -O binary program.o program.p
Echo.
Echo -- Making Kernel Image --
copy bootsect.sys + kernel.sys + program.p /B slx.img /Y
Echo.
Echo -- Erasing Temporary Files --
erase *.o
erase *.sys
Echo.
set Compile=%cd%
Echo.
pause
cls
title=Testing Kernel
Echo --- Copying "slx.img" to Bochs ---
copy slx.img "C:\Program Files\Bochs-2.3\slx.img" /Y
Echo.
Echo --- Starting Bochs ---
cd /D C:\Program Files\Bochs-2.3\
Bochs -q

cd /D %Compile%
cls
title=Command Prompt
Thanks in advance
Last edited by t0xic on Sat May 05, 2007 4:37 pm, edited 1 time in total.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

should I really even say anything....

ok, so we know exactly how your kernel works apparently from your compile script, and from a bit of code that isn't self-explanatory...genius...
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Although I will ignore your sarcasm, as I am a beginner at this i will say the following
A) I can do that through the kernel
B) I want to be able to execute with the kernel already linked so I dont have to re-link the kernel each time

Thanks
-t0xic
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

so you have a filesystem?
if not, you will need one to execute files easily..unless you want ot just hardcode sector numbers..

edit:
and if you don't have a filesystem, then do you have floppy disk functions..

is your kernel 16 or 32bit...

it helps to give us a bit of info!


it's like saying
Hi, I've made an OS, I am trying to get interrupts to work but they wont, please help
no one can possibly know what's wrong...[/quote]
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Writing *External* programs for my kernel

Post by jnc100 »

t0xic wrote:

Code: Select all

tools\ld -i -e _main -Ttext 0x1000 -o kernel.k main.o video.o ports.o
tools\ld -e _progstart -Ttext 0x1000 -o program.o kernel.k prog.o
Its really not a good idea to have both the kernel and a program execute from the same linear address... (I'm referring to the -Ttext option here).

If you're using grub then you can have it provide a module for you. It details where it loads it in the multiboot header, otherwise you're probably going to need some file system support to load a program. You generally load an image of the program, then assuming its in, e.g. elf format (which I see yours isn't) you either copy or map each section to the place in memory where it expects to be (i.e. where the linker puts it) and then jump to the entry point (again specified in a decent executable format). I suggest you read the sections on ELF and/or PE in the wiki.

Regards,
John.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

I went past what most people would do and looked at your source on your website...
what exactly does this do?

Code: Select all

[BITS 32]
[GLOBAL _readdrive]
[SECTION .text]

_readdrive:
          mov ah, 02h    ;Read sector command
          mov al, 0      ;Drive number (0=A,1=B...)
          mov dx, 0      ;Starting sector
          mov cx, 2      ;Number of sectors to read
          int 25h
even if they did point to (I guess) the right BIOS interrupt, it's 32bit, so it wouldn't work

and you don't even have floppy disk reading due to the lack of this, so how are you going to load something externally?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Post by jnc100 »

Oh dear. Int 25h = DOS 1+ absolute disk read.

You might want to look up int 13h, ah 02, but only before you jump to protected mode.

Regards,
John.
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Thanks everyone...

I'll try to get that to work
So i could change -Ttext to 0x2000 for example and that might load?

Thanks again

-Michael
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

You are probably another person trying to learn assembly language by writing an operating system, and that really is like trying to run before you can walk - big time. Most of the beginner's assembly language projects I see on other sites are MS-DOS programs. Probably that is the only way to learn assembly language, especially at systems level, because then you haven't got a protected mode operating system like Windows getting in your way all the time.

Absolute disk reads and writes via int 25h and 26h were functions built into MS-DOS, and they are not available if an operating system is the very thing you are trying to write. You can use the BIOS interrupts until you switch into protected mode, if you are trying to write a protected mode OS, but once in pm you are really on your own without any support from pre-written software, either in the BIOS or MS-DOS. Those kinds of services are the very thing you are supposed to be providing to other programs by writing an OS, and the OS itself can only access disks by directly programming the chips which control the floppy and hard disks.

One of the services your OS would need to provide is the ability to allocate memory, read an executable file into that memory, do whatever initialisation is necessary, and then jump to the entry point of that program (as well as making that functionality available to other applications via a call to your OS). The exact details would depend upon the design of your operating system, and have you even thought of that yet?
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Wow... thats a lot to think about lol... How do I know if my os is loaded into pmode or not? Is that in the bootsector, sorry for being a total noob

-Michael
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

When the computer is first switched on it always starts executing at the real mode address F000:FFF0, and it is running in real mode. When it loads your boot sector it is still running in real mode, and when the boot sector loads your OS it will probably still be running in real mode. If you are writing a protected mode OS it is then up to that OS's initialisation code to switch into pm at and appropriate point. But really, if you are thinking of writing an OS, you should know all that already.
Post Reply