How To Make a Basic OS
How To Make a Basic OS
In this tutorial, I will not teach you how to make a bunch of if statements in Python or Node.js or Java, but will teach you how to make a real OS, although it won't be able to do much. Basic experience with NASM x86 Assembly, but it is not required since this will explain everything needed along the way.
To create an OS, you need two packages: NASM and QEMU. NASM is the Assembler we will use and QEMU is the VM we will use to run the OS.
You can use this basic Bash script to automatically download them if needed.
Next up, you need to install the required libraries.
There are a number of libraries needed to run everything in QEMU. Thanks to @CSharpIsGud , this is fairly easy...
Using the following Bash script, you can install all of the required libraries.
You only need to run this once, so if you put it in your
main.sh file, you can comment it out after running once.
Setting Up The File System
Now, we need to set up the proper files for this all to work.
First, create a folder named
iso and inside that, create a folder named
boot. You also, in the root directory, need to create two files:
Link.ld tells the linker how to put together the binary file so it can be used and
Loader.asm is your main Assembly code.
Setting Up The Linker File
Link.ld, put the following code:
Of course, this is copied from @CSharpIsGud 's tutorial which was in turn copied from LittleOSBook, so I cannot really explain all of this that well.
The Assembly Template
To start working on your OS, you need to follow a basic template which makes the code bootable. Here is the template:
This is just a basic template so that you can start writing your OS... Now, for the actual coding!
Making The OS
All of the code in this section will be done in the
First off, in the
.data section, we need to set up a few variables. In the section, you should add the following code.
All of the variables that have "Required" commented next to them are absolutely required for this. The other lines are messages to be printed and you can feel free to change them in any way you want.
Now, we need to create a few macros for this program to run!
We need to have a way to print characters at a specified position. We can use a macro for that!
The comments explain how this works. Now, we need to be able to clear the screen.
Clearing the Screen
Now that we can print single characters, we need to be able to clear the screen! We need two macros for that!
First off, the macro to clear a given number of characters!
Once again, the comments explain this macro.
Now, we need to use this to clear the entire screen.
This is a crude way of doing it, but it works!
This just repeats the
clearscreen macro multiple times. Since the
al register is only a 8-bit register (at least I think... It might be 16-bit), there are limits to the amount it can go, so each 4000 doesn't actually clear 4000 digits.
This macro will be used for clearing the screen at the start of the program.
Now, we need a macro to print strings.
Now, we need one last macro...
This macro will allow you to skip to certain lines.
Now that we have the macros, we can start programming the rest of the OS!
Programming the OS
Now that we have the macros, we need to use them in the
Currently we have
but we will need to change this code.
The code for this basic OS is
Of course, you can change things like the color, number of messages, order of messages, etc. This is just a basic OS like HelloWorld OS which can be found here: https://repl.it/talk/share/HelloWorld-OS/52435
Compiling and Running QEMU
To compile and run QEMU, you simply use the following Bash script:
The next steps will be adding in C and then user input, so stay tuned to my next OSs and the tutorials for those next steps!
If you have any questions about this tutorial, please just ask!
Also, @DynamicSquid , here is a ping since you said you wanted this tutorial!
Can you personally help @FluidCycling on his Repl
Because he is following your tutorial and got stuck on some error
Dude, could you ping me too? I learned from looking at the files and changed it up a bit. This s freaking awesome
I was messing around with it, then accidentally deleted a letter of code in the largest file. luckily it reloaded. then i went back there to search for a word, and it broke. the whole webpage. I went back on and ran it, but i lagged it up so much it couldnt reach the biggest file and i had to delete it XD @AmazingMech2418
So how would I add more to this OS? It would be nice to make it do something a little bit more complicated than to just make it say a certain sentence.
By the way, amazing tutorial! Keep it up!
@AmazingMech2418 Oh, one edit: I get the error "nasm: fatal: unable to open input file 'Loader.asm'
ld: cannot open linker script file Link.ld: No such file or directory", so I assume I'm actually supposed to write
nasm -f elf32 ./iso/boot/Loader.asm -o Loader.o and
ld -T ./iso/boot/Link.ld -melf_i386 Loader.o -o ./iso/boot/kernel?
i wanna create a kernel here, i get this error:
qemu-system-x86_64: error while loading shared libraries: libbrlapi.so.0.6: cannot open shared object file: No such file or directory
An OS talks to hardware directly. I haven't looked at the code too closely but it only seems to be writing some text, how would you consider this an OS?
I recommend explaining what 0xb8000 actually is and how the code works instead of just saying "here's the code, please copy it."
Does this work on MacOS (64bit, Catalina)?
@AmazingMech2418 I already have QEMU and NASM. My only issue is how to compile the stuff in that ELF format, since MacOS seems to not want to let me. Please tell me if there is any way to get it working on MacOS, so I don't have to make a VM. But if Linux is the only way then I will use it. (my mac is slow so VMs arent the best alternative)
Also, I have a rasberri pi with rasbianOS on it. Will that work for OSDEV?
AmazingMech2418: In this tutorial, I will not teach you how to make a bunch of if statements in Python or Node.js or Java.
Me after creating MelnykOS: I am 2 parallel universes behind you.
Me after compiling my 32-Bit Edition Of Coresync OS: I am 48 parallel universes ahead of you.
@firefish Well, if I have a specific project, I can do it. It's just that I'd rather make my first program something that I can do easily as I'm still learning the language so that I can focus more on learning the language itself than certain APIs, modules, or algorithm development as I'm just starting out.