|
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Writing a Simple Text Or Graphical OS in C and ASM
Complete Source code included in Zip!
In this small tutorial, you will learn how to make a small REAL Operating System that
you can boot off of a floppy! After reading this tutorial, you will have a small understanding
of how an OS works and how to create your own. Before we start coding though, I'll have to explain
some things about how they work. BTW, read the legal stuff at the bottom
Bootloader
All Operating systems have to be loaded some how. This is done by a bootloader.
A bootloader is a small raw binary program that sits in the first sector of a disk (floppy disk for our OS)
When a computer boots, it checks for a bootloader in the first sector of the floppy.
If a bootloader is present, the computer will execute it. A bootloader isn't very complicated;
All it has to do is load the kernel (the 'main operating system executable') into the RAM so the
computer can run it. I won't be going over the bootloader here,
but I have the source (and the binary) for the bootloader included on the attached ZIP file :)
CPU Modes
The CPU can run in a few different 'modes'. These modes determin how the operating system can
access memory and resources. The first mode is called 'Real Mode'. If you kernel is running in
real mode, you have almost unlimited access to the RAM UP TO 1 Megabyte. This isn't that great. When
your computer starts up, it starts in this 'real mode'. This is fine if you are making a simple kernel
but if you want to make a nice operating system, making your CPU enter a 'protected mode' is a better
idea. When your CPU is in protected mode, access to memory is limited so you (or other programs) dont
screw with it. Running in protected mode will make your operating system more stable. In protected
mode, you can also access up to 4 GB of ram (if you have that much ). Our operating system will run in
protected mode because our bootloader sets it up for us ;) Ok, almost to the code!
Interrupts
There are 2 different types of interrupts.
1) Software Interrupt: Kind of like a 'function' that resides in the BIOS. These are used
often to do simple tasks like set video modes, or requesting data from the BIOS
2) Hardware interrupts: These are a bit different. A hardware interrupt is something that 'calls a function'
in your code when something happens (when a key is pressed, etc...).
Interrupts are a great way of communicating with the computer's hardware, but they can
only be used in real mode. If you try to call an interrupt in protected mode, you will crash your
OS. There ARE some special ways to get around this problem though (which I wont talk about here
because they still confuse me!)
OK, We are going to make our operating system in assembly language and c.
Click Here to download the tools needed to compile and link your Operating system. I couldn't include them with the zip here on PSC because exe's are automaticaly deleted.
Our 'main' function is going to be in our assembly code. (Don't worry, its only a few lines of code!)
What this code does is call a function from our C file (which will be linked to this when compiled).
; Note: Semicolens are comments in Assembly (ASM)
[bits 32] ;Make our OS 32bit
SECTION .text
EXTERN _c_main ;Reference to our main function in the c code.
;Note that our function in C starts with an underscore (_)
;This is because when we compile our C file, functions are started with
;underscores. THis is not so for ASM files though.
start:
call _c_main ;This calls our main function in the C file
jmp $ ;Freezes the computer because theres nowhere else to go!
Save that code as 'kernel.asm'. Make a 'kernel_c.c' and put this code in it:
char message[]="Hello From Our First Operating System!";
int c_main(void) {
//Notice there is no underscore infront
// of our c_main function.
//This is because the compiler automatic
// aly puts it there for us ;)
char *source = message; //Pointer to our message we will print to the screen
char *destination = (char *)0xB8000; //Pointer to the text video memory
while (*source) {
*destination++ = *source++; //Add 1 to the vid. memory offset, and save a character to it
*destination++ = 7; } //Set the color of that character.
return 0;
}
That simple Kernel displays the message 'hello from our first operating system'
Now, lets compile this code.
Compile the ASM code like this:
NASM -f coff kernel.asm
That should create an output file called kernel.o
Compile our C code like this:
gcc -O3 -c kernel_c.c
That will output a kernel_c.o
Link the 2 files like this:
ld -Ttext 0xFF800000 --oformat binary -o kernel.bin kernel.o kernel_c.o
You should get a warning that says it cant find the entry symbol start. This is what you want ;)
Linking those produces a flat binary called kernel.bin
This is your operating system.
Copy this to a normal FAT formatted floppy drive. Now, copy the compiled bootloader (included
in the zip that comes with this tutorial) using the partcopy program that I I gave the link to up there
Use partcopy like this to copy the bootloader to your floppy:
PARTCOPY bootf.bin 0 3 -f0 0
PARTCOPY bootf.bin 3E 1C2 -f0 3E
This copies the bootloader to the first sector of your floppy drive.
Boot your floppy (not now). There's still more tutorial here! Now, I will explain how to put graphics
into your little OS!
Before we can start with graphics though, you have to use an interrupt to set the graphics mode.
Open up the bootloader source (bootf.asm) that is included in the zip thats attached to this tutorial
Go down to 'Start:'
underneath start, put the follwoing code in:
sti ;Enable Interrupts
mov ax, 13h ;Video Mode 13h (19)
int 10h ;Call video services interrupt to set the video mode.
Recompile it like this:
NASM -f obj -l bootf.lst bootf.asm
JLOC bootf.lnk bootf.bin
Note: you must have the JLOC linker (this is also included in the tools zip I gave you the link to up there)
Use the partcopy commands up there again to copy the new bootf.bin to the bootsector of the floppy.
Now, change your C code to this:
int SetPixel(int x, int y, int color);
int main(void) {
typedef unsigned char byte;
byte *VGA = (byte *)0xA0000; //This gives you a pointer to the graphics video memory
int x;
int y;
for(y=0;y<201;y++) {
for(x=0;x<321;x++) {
SetPixel(x,y,x); //Plot a pixel
}
int SetPixel(int x, int y, int color) {
VGA[320*y+x]=color; //This plots a pixel at x,y
};
There.
Compile this C code as before, link it as before, then copy the new kernel.bin to the floppy drive!
This code will fill the screen with colors!
Hope this helps you making your own Operating System now!
Some cool OS dev links are:
OS Kernel Message Board
Another OS Development Site
Google- lol- the best
Some IRC chat channels:
#osdev on irc.openprojects.org
#asm on openprojects
#asm on EFnet
If you really liked this, Please rate me ;)
I'm tired now. Its like 11 PM. I also cant write much more because I filled up my 14 GB harddrive lol.
legal stuff: I am not responsible if this stuff messes up your computer in any way! (It shouldn't though. I've never
heard of anybody having any problems with this kind of stuff...) | |
Download article
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzipto decompress it.
Virus note:All files are scanned once-a-day by Planet Source Code for viruses,but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE: 1)Re-scan downloaded files using your personal virus checker before using it. 2)NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com
|
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description. |
Other 2 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
8/4/2002 1:45:10 AM:Matt Carpenter Please rate it if you like it! Thanks
|
8/4/2002 2:00:16 AM:Abdel Jabbar Baig Pretty good tutorial to start with! I
liked the part that you combined C and
Assembly where most tutorials just talk
about writing the Kernel and the
Bootloader only in ASM. You should
almost tell in which line of code you
changed to protected mode;)
Also, if
you were to use C++ instead of C, the
function names will be mangled very
differently. A function won't just
have an underscore in front of it so
this way your ASM code won't work.
Overall great beginner tutorial(to
start writting an OS)!
|
8/4/2002 4:14:48 AM:Matthew Li (mazzanet) I like it! Have a look at
mine:
http://mazzanetos.sourceforge.net
It is open-source and uses nasm,
jloc, copyboot and gcc. If you know
alot of C++ or asm join our development
team.
|
8/4/2002 11:12:08 AM:Matt Carpenter I've looked at your mazzanet OS before.
Pretty cool ;)
|
8/4/2002 5:17:18 PM:Simon Sweet article man that was awesome
|
8/4/2002 5:57:54 PM:Matthew Li (mazzanet) At the moment on MazzanetOS, we are
writing a text editor and a GUI.
|
8/5/2002 9:24:10 PM:KDDFLX Hey, i think you forgot something in
your code(both): for the assembler, the
boot sector must be 512 bytes long, the
size of a sector, which it probably
isnt, AND, you left the bootsector
signature - Awwwsomething, i forget.
anyways, without theise two things, the
boot sector wont work and with some
cases, wont be saved to the first sector
|
8/5/2002 9:27:15 PM:KDDFLX Ok, i got what I need. This was taken
from bootf on your zip. and it has what
your asm code doesnt: resb
0x1FE+$$-$
This makes the file 512
bytes long
db 0x55, 0xAA
This is
the Boot signature
I hope this helps
people who are having problems with
this code
|
8/5/2002 10:17:52 PM:Matt Carpenter my code in the tutorial isn't a
bootsector. the bootf thats in my zip
is the bootsector and it DOES assemble
to 512 bytes...
|
8/8/2002 2:59:12 AM:Matthew Li (mazzanet) just a note, bootsector code actually
has to be 496 bytes. this allows for 16
bytes for disk related data. in some
cases the bootsector code can be 512
bytes, but i won't go into that......
|
8/16/2002 1:44:29 AM:Anthony I love this article even though i cant
get it to work, can someone help me im
still fairly new at this and dont
understand how to complie the code.
PLEASE HELP
|
8/16/2002 5:42:37 PM:Matt Carpenter I just sent you an email. It should
help :)
|
8/19/2002 12:28:08 AM:eliscool I really liked this tutorial, mainly
cause I dont know asm yet (im going to
start learning it soon) and it walked
you through everything whereas others
didnt, anyway I really dont wanna work
in mode 13h, its just to limiting, so
how can I increase the res, bit depth
etc? or is this just a whole other back
0 cats? eg writing vid card drivers?
anyway, do you know of any good sites
about this sortof thing?
|
8/19/2002 11:43:38 AM:Sachin G hey matt,nice work done man....
can u
please forward the email about
compiling this thing to me along with
some links or tutorials/name of books
which u think can help a beginner like
me to get deep into this....thanks
|
8/19/2002 6:13:25 PM:Matt Carpenter NOTE TO ALL: If you are having troubble
with GCC, download DJGPP (its free).
Should save you alot of time :)
|
8/20/2002 12:58:13 AM:Lee Trager Very nice tutorial. You should now
write something on file systems ;)
|
8/20/2002 4:33:58 AM:Sushain Pandit HI MATT.
I AM TOTALLY NEW TO THESE
O.S. STUFFS.
I REALLY WOULD APPRECIATE
IF U WOULD PLEASE MAIL ME THE STEP BY
STEP PROCEDURE ON HOW TO COMPILE THE
CODE.
ALSO, PLEASE MENTION WHEN TO USE
WHICH FILE AND COMPILER. AGAIN, I WOULD
BE HIGHLY GRATEFUL TO U IF I WAS ABLE
TO COMPILE IT. PLEASE DO ME THIS
FAVOUR.
THANKS IN ADVANCE.
BYE.
|
8/20/2002 9:58:59 AM:Sachin G thanks for the email matt...i really
appreciate that..good luck for the
contest
|
8/21/2002 1:24:29 PM:tommacco Nice job, it's always helpful to have
something basic to go by when creating
a new project, 5 big ones
|
8/25/2002 1:10:46 PM:Oli Cool ! My dream come true! but do you
know a way tu include header file like
stdio.h for make it easier! email me
please !!!!
|
8/29/2002 5:43:45 PM:Frederico Machado I really want to see this code working,
but the I couldn't download the tools
by the link in the brinkster page.
Please, can you help me with this Matt?
Can you send me the file by e-mail??
fredisoft@terra.com.br
Thank you very
much. I cant wait to get it to work.
|
8/29/2002 5:45:39 PM:Frederico Machado My site on Brinkster isn't working
too... Is it a problem in the server?
|
9/3/2002 1:32:41 PM: hmmmmmmm, thats fine
|
9/28/2002 7:28:21 PM: didn't like it. the compiler kept
complaining about DJGPP not being
properly installed, and i tried
properly installing it, and it got onto
the wrong drive and it didn't even put
any files there.
|
9/30/2002 5:13:47 PM:Steve Mack Yeah the kernel_c.c won't compile for
me, something about not installed
properly :(
|
10/1/2002 5:26:54 PM:Coding Genius Great tutorial man. I got as far as
having a kernel.Bin on my floppy disk.
I can't get that boot sector on. I
couldn't find bootf.bin in the
download. I saw bootf.asm and bootf.lnk
and also makeboot.bat, but it wouldn't
create it...errors. Can you do me a
huge favour and send me bootf.bin
(imgonnadothingsmyway@hotmail.com)
|
10/1/2002 6:18:20 PM:Coding Genius Just here to let you all know. I found
compilatio instructions at the bottom
of bootf.asm, followed them on my own
and I can now verify that this really
does work! Amazing! 5 G's. It took me
many hours trying to get the GCC
compiler, figure out command lines, get
right files...but it's worth it just to
see it boot and work! I think I have to
go wipe away a tear now :)
|
11/11/2002 8:25:31 AM: Hey matt thanks for the tutorial it's
better than the rest I've seen... Just
a couple of questions: /nIn the gfx
part I typed it all in as per but then
when I link it i get the message
"undefined reference to _SetPixel". So
I tried putting EXTERN _SetPixel in
kernel.asm but to no avail. Any clues?
Keep writing ; )
|
12/14/2002 8:44:13 PM:Ark Hello Matt!
Your article seems to be
popular even in VB area (though by
another author)
:)
http://www.planet-source-code.com/
vb/scripts/showcode.asp?txtCodeId=41529&
lngWId=1
Regards
Ark
|
12/31/2002 3:18:15 PM: Great Tutorial!! However when i try to
make kernel.bin i get an error message
that says there is a undefined refrence
to _c_main.
ld.exe: warning: cannot
find entry symbol start; defaulting to
ff800000
kernel.o(.text+0x1):kernel.asm
: undefined reference to
`_c_main'
can you please help me!
|
12/31/2002 3:20:04 PM: great article!! However when i try to
link the files and make the kenrel.bin
i get an error saying there is a
undefined refrence to
_c_main.
ld.exe: warning: cannot
find entry symbol start; defaulting to
ff800000
kernel.o(.text+0x1):kernel.asm
: undefined reference to
`_c_main'
can you please tell me why
its undefined?
|
2/15/2003 12:54:42 AM:Matt Carpenter Sorry, havn't been around psc lately,
my only idea is that you might not be
linking the compiled object that was
made from the c file :-/
|
2/23/2003 4:45:54 AM: Help! I cant get the ld -Ttext
0xFF800000 --oformat binary -o
kernel.bin kernel.o kernel_c.o line to
work. Please E-mail it to me
|
3/21/2003 6:56:04 AM: When you get the error:
"ld.exe:
warning: cannot find entry symbol
start; defaulting to ff800000
kernel.o(.text+0x1):kernel.asm :
undefined reference to `_c_main'
"
Check the target file formats in
your compile stage. The output formats
of NASM and gcc (or djgpp) have to
match (or at least ld needs to be
informed of the individual formats).
Try having NASM output in aout format
like so:
NASM -f aout kernel.asm -o
kernel.o
|
4/5/2003 4:07:09 PM:Alexander Borgerth good work, this was amazing.... 5globes
|
4/22/2003 9:33:49 AM:Subliminal The link for the tools needed is
broken, could you send me the zip
someone or other urls were i can get
all the tools
(micropedia@hotmail.com)
|
4/23/2003 3:28:29 PM:Subliminal For the C code that makes it graphical
I had to use:
typedef unsigned char
byte;
byte *VGA = (byte *)0xA0000;
//This gives you a pointer to the
graphics video memory
int
SetPixel(int x, int y, int
color);
int main(void) {
int
x;
int y;
for(y=0;y<201;y++)
{
for(x=0;x<321;x++)
{
SetPixel(x,y,x); //Plot a
pixel
}
}
}
int SetPixel(int x,
int y, int color)
{
VGA[320*y+x]=color; //This plots a
pixel at x,y
};
Otherwise gcc gave
me errors, but it works fine now
|
4/23/2003 8:17:42 PM: the command ld -Ttext 0xFF800000
--oformat binary -o kernel.bin kernel.o
kernel_c.o does not seem to work for
me. I get the error:
|
4/23/2003 8:19:41 PM: I'm having a bit of a problem compiling
the source. I am able to compile using
nasm and gcc but when I try to link
them I get an error: "kernel.o: file
not recognized: File format not
recognized". I've tried using "NASM -f
aout kernel.asm -o kernel.o" to no
avail. I'm compiling the source on my
FreeBSD box but I really don't see why
this would make a difference.
|
5/10/2003 9:58:34 PM: the link to the tools doesn't work -
please send to me at
tytanic11@comcast.net thanks
|
5/16/2003 3:43:46 AM: change the kernel.asm from
EXTERN
_c_main
to
EXTERN
c_main
and
call _c_main
to
call
c_main
it will fix the
problem:
undefined reference to
`_c_main'
then complie
NASM -f aout
kernel.asm -o kernel.o
you need aout
i belive... i complie it in linux and
coff wont allow me to link the files
together I got the error
"File format
not recognized"
when i try to link the
files together when i compile it using
coff
Then Just link.
That all...
It work for me after thoose changes. Oh
yeah one more thing ld dont work well
for me in windows.
If you are having
problem.. send me a mail
razifhoe@yahoo.com
ps: I did not do
the graphic version, so please dont ask
me about it.
|
5/27/2003 6:28:39 PM: The links broken could yu plz update it
or send the files to
thedarkone_2003@hotmail.com plz....
realy wana have a go a making an os
|
6/5/2003 6:55:53 PM:Richard Sullivan yea, your link to the tools is broken.
maybe you can post a new one.
|
6/16/2003 1:06:19 AM: really want to see this code working,
but the I couldn't download the tools
by the link in the brinkster page.
Please, can you help me with this Matt?
Can you send me the file by e-mail??
(ace_nee@yahoo.com)Thank you very much.
I cant wait to get it to work.
|
6/29/2003 3:48:15 PM:Matt Carpenter TO ALL WHO CAN'T FIND THE TOOLS
LINK:
Brinkster has deleted my account
and I DO have a real host right now,
but I lost my zip. Here's the tools
that you'll need:
DJGPP (more
specificaly gcc)
JLoc ( a
linker)
Partcopy (copies files to
floppies)
NASM (assembler)
LD (a
linker, I THINK it comes with djgpp)
I
think thats all, if you're still having
problems please email me :)
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|