Advertisement
ASP_Volume2 Miscellaneous #41225

Make a Simple Text or Graphical Operating System

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 ;-)

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
<font color=black face=verdana>
<font size=5><b>Writing a Simple Text Or Graphical OS in C and ASM</b></font><hr>
<font size=2>
Complete Source code included in Zip!<br><br>
In this small tutorial, you will learn how to make a small REAL Operating System that<br>
you can boot off of a floppy! After reading this tutorial, you will have a small understanding<br>
of how an OS works and how to create your own. Before we start coding though, I'll have to explain<br>
some things about how they work. BTW, read the legal stuff at the bottom<br><br>
<b>Bootloader</b><br>
All Operating systems have to be loaded some how. This is done by a bootloader.<br>
A bootloader is a small raw binary program that sits in the first sector of a disk (floppy disk for our OS)<br>
When a computer boots, it checks for a bootloader in the first sector of the floppy. <br>
If a bootloader is present, the computer will execute it. A bootloader isn't very complicated;<br>
All it has to do is load the kernel (the 'main operating system executable') into the RAM so the <br>
computer can run it. I won't be going over the bootloader here, <br>
but I have the source (and the binary) for the bootloader included on the attached ZIP file :)<br>
<br>
<br>
<b>CPU Modes</b><br>
The CPU can run in a few different 'modes'. These modes determin how the operating system can<br>
access memory and resources. The first mode is called 'Real Mode'. If you kernel is running in <br>
real mode, you have almost unlimited access to the RAM UP TO 1 Megabyte. This isn't that great. When<br>
your computer starts up, it starts in this 'real mode'. This is fine if you are making a simple kernel<br>
but if you want to make a nice operating system, making your CPU enter a 'protected mode' is a better<br>
idea. When your CPU is in protected mode, access to memory is limited so you (or other programs) dont<br>
screw with it. Running in protected mode will make your operating system more stable. In protected <br>
mode, you can also access up to 4 GB of ram (if you have that much ). Our operating system will run in<br>
protected mode because our bootloader sets it up for us ;) Ok, almost to the code!<br>
<br>
<b>
Interrupts</b><br>
There are 2 different types of interrupts. <br>
1) Software Interrupt: Kind of like a 'function' that resides in the BIOS. These are used<br>
   often to do simple tasks like set video modes, or requesting data from the BIOS<br>
2) Hardware interrupts: These are a bit different. A hardware interrupt is something that 'calls a function'<br>
   in your code when something happens (when a key is pressed, etc...). <br>
Interrupts are a great way of communicating with the computer's hardware, but they can<br>
only be used in real mode. If you try to call an interrupt in protected mode, you will crash your<br>
OS. There ARE some special ways to get around this problem though (which I wont talk about here<br>
because they still confuse me!)<br><br>
OK, We are going to make our operating system in assembly language and c. 
<a href="http://www11.brinkster.com/vbdn/tools.html">Click Here to download the tools needed<br>to compile and link your Operating system.<br>I couldn't include them with the zip here on PSC<br>because exe's are automaticaly deleted.</a><br><br>
Our 'main' function is going to be in our assembly code. (Don't worry, its only a few lines of code!)<br>
What this code does is call a function from our C file (which will be linked to this when compiled).<br><br>
<font color=darkblue><br>
; Note: Semicolens are comments in Assembly (ASM)<br><br>
[bits 32] ;Make our OS 32bit<br><br>
SECTION .text<br><Br>
EXTERN _c_main ;Reference to our main function in the c code.<br>
  ;Note that our function in C starts with an underscore (_)<br>
  ;This is because when we compile our C file, functions are started with<br>
  ;underscores. THis is not so for ASM files though.<br><br>
start:<br>
 call _c_main ;This calls our main function in the C file<br>
 jmp $ ;Freezes the computer because theres nowhere else to go!<br>
</font><br><br>
Save that code as 'kernel.asm'. Make a 'kernel_c.c' and put this code in it:<br><br><Font color=darkblue>
char message[]="Hello From Our First Operating System!";<br><br>
int c_main(void) {<br>
 //Notice there is no underscore infront of our c_main function.<br>
 //This is because the compiler automaticaly puts it there for us ;)<br><br>
 char *source = message; //Pointer to our message we will print to the screen<br>
 char *destination = (char *)0xB8000; //Pointer to the text video memory<br><br>
 while (*source) {<br>
		*destination++ = *source++; //Add 1 to the vid. memory offset, and save a character to it<br>
		*destination++ = 7; } //Set the color of that character.<br>
return 0;<br>
}<br><br></font>
That simple Kernel displays the message 'hello from our first operating system'<br>
Now, lets compile this code. <br>
Compile the ASM code like this:<br>
NASM -f coff kernel.asm<br>
That should create an output file called kernel.o<br><br>
Compile our C code like this:<br>
gcc -O3 -c kernel_c.c<br>
That will output a kernel_c.o<br><br>
Link the 2 files like this:<br>
ld -Ttext 0xFF800000 --oformat binary -o kernel.bin kernel.o kernel_c.o<br>
You should get a warning that says it cant find the entry symbol start. This is what you want ;)<br>
Linking those produces a flat binary called kernel.bin<br>
This is your operating system. <br>
Copy this to a normal FAT formatted floppy drive. Now, copy the compiled bootloader (included<br>
in the zip that comes with this tutorial) using the partcopy program that I I gave the link to up there<br>
Use partcopy like this to copy the bootloader to your floppy:<br>
PARTCOPY bootf.bin 0 3 -f0 0<br>
PARTCOPY bootf.bin 3E 1C2 -f0 3E<br><br>
This copies the bootloader to the first sector of your floppy drive.<br><br>
Boot your floppy (not now). There's still more tutorial here! Now, I will explain how to put graphics<br>
into your little OS!<br><Br><br>
Before we can start with graphics though, you have to use an interrupt to set the graphics mode.<br>
Open up the bootloader source (bootf.asm) that is included in the zip thats attached to this tutorial<br>
Go down to 'Start:'<br>
underneath start, put the follwoing code in:<br>
<font color=darkblue>
sti ;Enable Interrupts<br>
mov ax, 13h ;Video Mode 13h (19)<br>
int 10h ;Call video services interrupt to set the video mode.<br>
</font><br><br>
Recompile it like this:<br>
NASM -f obj -l bootf.lst bootf.asm<br>
JLOC bootf.lnk bootf.bin<br>
Note: you must have the JLOC linker (this is also included in the tools zip I gave you the link to up there)<br>
<br>
Use the partcopy commands up there again to copy the new bootf.bin to the bootsector of the floppy.<br>
Now, change your C code to this:<br>
<br>
<font color=darkblue>
int SetPixel(int x, int y, int color);
<br>
int main(void) {<br>
 typedef unsigned char byte;<br>
 byte *VGA = (byte *)0xA0000; //This gives you a pointer to the graphics video memory<br>
 <br>
 int x;<br>
 int y;<br>
 for(y=0;y<201;y++) {<br>
 for(x=0;x<321;x++) {<br>
 SetPixel(x,y,x); //Plot a pixel<br>
}<br><br>
int SetPixel(int x, int y, int color) {<br>
 VGA[320*y+x]=color; //This plots a pixel at x,y<br>
};<br><br></font>
There.
Compile this C code as before, link it as before, then copy the new kernel.bin to the floppy drive!<br>
This code will fill the screen with colors!<br><br>
Hope this helps you making your own Operating System now!<br><br>
Some cool OS dev links are:<br>
<a href="http://www.mega-tokyo.com/forum/">OS Kernel Message Board</a><br>
<a href="http://www.execpc.com/~geezer/">Another OS Development Site</a><br>
<a href="http://www.google.com/">Google- lol- the best</a>
<br>
Some IRC chat channels:<br>
#osdev on irc.openprojects.org<br>
#asm on openprojects<br>
#asm on EFnet<br><br>
If you really liked this, Please rate me ;)<br>
<br>
I'm tired now. Its like 11 PM. I also cant write much more because I filled up my 14 GB harddrive lol.
 <br><Br>
legal stuff: I am not responsible if this stuff messes up your computer in any way! (It shouldn't though. I've never<br>
heard of anybody having any problems with this kind of stuff...)
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine