Make a Simple Text or Graphical Operating System

Submitted on: 1/4/2015 3:13:00 AM
By: Matt Carpenter (from psc cd)  
Level: Advanced
User Rating: By 33 Users
Compatibility: C
Views: 4221
     This tutorial explains in plain english how to create a real text or graphical Operating System that can be booted from a floppy. Source Code and compiling tools included (Link Fixed). Source code is in C and assembly. Vote if you like it, thanks ;-)

This article has accompanying files
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

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!

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


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.

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 automaticaly 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
#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...)

winzip iconDownload 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 Winzip to 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

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.