Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Make a Basic OS
AmazingMech2418

How To Make a Basic OS

Introduction

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.

Installing Packages

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.

Installing 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 and Loader.asm. 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

Inside 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 Loader.asm file.

Declaring Variables

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.

Macros

Now, we need to create a few macros for this program to run!

Printing Characters

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.

Printing Strings

Now, we need a macro to print strings.

Moving Lines

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 .text section.

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!

Voters
JerichoBondi
KaiserFranz
Kai_Justice
VL07
ANDREWVOSS
RevathyPurush
Kookiez
ss629435
DaRubyMiner360
greatusername
Comments
hotnewtop
PattanAhmed

WoW... @AmazingMech2418
Can you personally help @FluidCycling on his Repl
Because he is following your tutorial and got stuck on some error
https://repl.it/talk/ask/Hey-guys-I-just-started-devving-to-make-an-OS-but-it-has-an-error-called-exit-status1/53308

That's it.

HahaYes

yay 800 cycle congrats

AmazingMech2418

@HahaYes Thank you!

gibbsfreenergy

Can someone explain what this does?

AmazingMech2418

@JosephSanthosh What do you mean exactly?

gibbsfreenergy

Like what is an OS, I'm a potato when it comes to coding. @AmazingMech2418

AmazingMech2418

@JosephSanthosh An OS is what runs on the computer natively. Like think about Windows, Linux, or MacOS and how they don't run inside an environment and runs completely natively on the device.

gibbsfreenergy

OHhhhhhhh, that's a thing! @AmazingMech2418

CodeLongAndPros

@JosephSanthosh An operating system is a runtime environment with shared libraries.

ZenoxHosting

@JosephSanthosh Yes, they are right, an OS is what a computer runs natively. For example, Windows, Linux, and MacOS are a few types of OSes.

HahaYes

Yay! This is going to be great

AmazingMech2418

@HahaYes Thank you!

DynamicSquid

This is amazing! Thank you! I'm gonna have so much fun today!

AmazingMech2418

@DynamicSquid Thank you and you're welcome! :D

AverseABFun

@DynamicSquid I agree this is so much better than ALL the other OS tutorials on the internet!

AmazingMech2418

@AverseABFun Thank you!

AverseABFun

@AmazingMech2418 Yours shows how to install the packages while every other one does not also i want a ping for the next one.

AmazingMech2418

@AverseABFun Thank you! And will do! :D I've been thinking about making the next one on AT&T Assembly, but am probably going to just make it on adding C. LOL!

DynamicSquid

@AmazingMech2418 if you do make another one, I would also like a ping :)

AmazingMech2418

@DynamicSquid Will do! :D

EpicGamer007

AmazingMech2418

@EpicGamer007 What? LOL! It's called word wrapping.

EpicGamer007

@AmazingMech2418 i know but that funny lol

AmazingMech2418

@EpicGamer007 Ah. LOL!

dudeactualdev

Nice

AmazingMech2418

@RyanGardiner1 Thank you!

angrydoge

Dude, could you ping me too? I learned from looking at the files and changed it up a bit. This s freaking awesome

AmazingMech2418

@dabombdgdzjr Thank you! And sure!

angrydoge

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

Maitreya29

please please PLEASE ping me when the next part comes out, you're a god!

CoolGuy27

@AmazingMech2418 can you ping me when the next part comes out?

AmazingMech2418
CoolGuy27

@AmazingMech2418 Thanks! and thanks for the cycle too!

DiveshTheReal

not working

AmazingMech2418

@DiveshTheReal What do you mean?

DiveshTheReal

@AmazingMech2418 Run and see it

AmazingMech2418

@DiveshTheReal I just ran it and it worked...

DiveshTheReal

@AmazingMech2418
i got stuck here

AmazingMech2418

@DiveshTheReal That's all it does. LOL! Read the post. This doesn't include any input.

DiveshTheReal

@AmazingMech2418 ok nvm them

DiveshTheReal

@AmazingMech2418 any btw how do you install the firefox

AmazingMech2418

@DiveshTheReal What do you mean?

DiveshTheReal
ironblockhd

Woah

ZenoxHosting

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

@ZenoxHosting Thank you! I'm actually working on figuring out that kind of stuff myself now, so, hopefully, I'll be able to make the tutorial on that soon!

DungeonMaster00

i want next tutorial

AmazingMech2418

@nk1rwc It's coming soon!

DungeonMaster00
gibbsfreenergy

Me waiting for the repl to load ...

fuzzyastrocat

Noice. I have a sudden urge to go make an OS... that runs a custom lang

AmazingMech2418

@fuzzyastrocat Thank you!

fuzzyastrocat

@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?

fuzzyastrocat

Ok yep that worked, so maybe update that in tutorial?

AmazingMech2418

@fuzzyastrocat Loader.asm is in the root directory, right?

fuzzyastrocat

@AmazingMech2418 Oh no, I put loader.asm and link.ld in ./iso/boot, whoops ok

AmazingMech2418

@fuzzyastrocat Oh! That makes sense then!

fuzzyastrocat

@AmazingMech2418 Thx for this tutorial, got my basic OS running — now to expand on it!

CodeLongAndPros

Perhaps add C?

AmazingMech2418

@CodeLongAndPros That's what I'll figure out next and then make a tutorial for!

Codemonkey51

Imma have to try this out

EpicGamer007

As I said, I am so envious of low level lang devs.

JaydenSavarinat

Hey @AmazingMech2418, I'm getting an error saying there was an error while loading a shared library. Please help.

AmazingMech2418

@JaydenSavarinat That happens to me too. It's an issue with Repl.it's storage system, so it won't save the file since it is a large binary file. I don't really know how to fix it though.

JaydenSavarinat

@AmazingMech2418 Oh, ok. Nice project though!

AmazingMech2418

@JaydenSavarinat Thank you!

RevathyPurush

What language does this code uses??

AmazingMech2418

@RevathyPurush Bash for the compiler script, but mostly NASM Assembly.

zplusfour

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

AmazingMech2418

@zplusfour Did you add the library files?

zplusfour

@AmazingMech2418 oh I didn't
thank you for reminding me

maxina

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?

AmazingMech2418

@maxina The text is through a raw memory buffer by the way. And this can run freestanding on a device, but I just used QEMU to emulate it.

maxina
programmeruser

I recommend explaining what 0xb8000 actually is and how the code works instead of just saying "here's the code, please copy it."

AmazingMech2418

@programmeruser That's the memory location where the data on what gets printed on the screen starts...

programmeruser

@AmazingMech2418 I know that, I was saying that you should explain it in the tutorial...

[deleted]

Does this work on MacOS (64bit, Catalina)?

AmazingMech2418

@LucasAllori No. Only Linux. You can create a VM though with like VMWare or VirtualBox!

[deleted]

@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

@LucasAllori I'm pretty sure it has to be an x86 Linux device. You could make a VM, but I don't think it will work on a Mac. Macs are ARM I think anyways...

[deleted]

@AmazingMech2418 Macs are x86-64. I will now proceed to find myself a new laptop with Linux on it.

[deleted]

@AmazingMech2418 Actually nevermind, I just need to learn ARM assembly language

AmazingMech2418

@LucasAllori You could probably dual-boot then!

[deleted]

@AmazingMech2418 Thats a good idea actually
It is now time for a 1 hour youtube session on how on earth you do that

thanks for your help though!

AmazingMech2418

@LucasAllori If you use Ubuntu MATE, all you need is a flash drive or DVD drive and disk. It can get a little difficult with setting up BIOS and stuff, but it's not too hard...

programmeruser

@AmazingMech2418 @LucasAllori this does work on Linux. Just install the tools and it should work.

AmazingMech2418

@programmeruser That's what I said. LOL!

programmeruser

@AmazingMech2418 I misread what you said. Anyways, I invited you to a repl with a link to free hacker.

AmazingMech2418

@programmeruser I saw, but I already used that before, so it won't work again...

angrydoge

Um so I added more text variables, but dont know how to make it print them

angrydoge

@AmazingMech2418 can u answer?

AmazingMech2418

@dabombdgdzjr You have to use the gotoline and printstr functions as defined. I explain these in the tutorial.

angrydoge

Wait, in which file @AmazingMech2418 cuz im blind XD i can never find what im looking straight at.

AmazingMech2418

@dabombdgdzjr Loader.asm

angrydoge
angrydoge

in the printstr and gotoline places, I dont see it declaring a variable or calling it. @AmazingMech2418

AmazingMech2418

@dabombdgdzjr You are looking in the macros. Look in the actual code of the program, starting at entry.

angrydoge

@AmazingMech2418 it has this code:
html`
global entry

MAGIC equ 0x1BADB002 ; These tell the bootloader that we are bootable
FLAGS equ 0
SUM equ -MAGIC
SYS_WRITE equ 4
SYS_READ equ 3
STDOUT equ 1
STDIN equ 2
SYS_EXIT equ 1
WRITESTART equ 0xB8000
SCREEN_SIZE equ 12850

AmazingMech2418

@dabombdgdzjr What is the "html" thing? Also, I mean in the entry label, not where it is declared global in the memory. SYS_READ is just a constant for an instruction.

angrydoge

@AmazingMech2418 sorry the html thing was me forgetting markdown XD. ok then, I have no idea, sorry for tha troubles

AmazingMech2418

@dabombdgdzjr Look at line 107.

angrydoge

ah, thx u @AmazingMech2418

AmazingMech2418

@dabombdgdzjr You're welcome!

iocoder

Never turns on or starts or whatever @AmazingMech2418

AmazingMech2418

@ridark What do you mean? It should start...

[deleted]

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.

[deleted]

@AJDevelopment dont punch me for using an old meme

omarElgohary

which coding language is this?

AmazingMech2418

@omarElgohary This is Assembly. However, you create a Bash repl for it since there aren't Assembly repls yet.

firefish

WHY DO YOU USE NASM INSTEAD OF AT&T GGGAHAHHAGH

AmazingMech2418

@firefish Because I don't know AT&T yet...

firefish

@AmazingMech2418 Just swap the args for every instruction, put a $ sign infront of a numeric literal and a percent in front of a register name.... that easy

firefish

@AJDevelopment That's why you learn AT&T, for precisely because NASM is easier

firefish

@AJDevelopment are you sure

firefish
firefish

@AJDevelopment No look at the comment I've linked, scroll

D
O
W
N

AmazingMech2418

@firefish The next version will be AT&T by the way! I just recently learned it. I'm also going to post a calculator I made soon...

firefish

@AmazingMech2418 Ok! also what's the calculator got to do with this

AmazingMech2418

@firefish It's made with AT&T Assembly. LOL! I always choose a basic project like a calculator or pi approximator to learn a new language.

firefish

@AmazingMech2418 Ah ok, alright then, so if you were in charge of dusk, then nothing would've happened because dusk is quite complex for being my first ever program in golang

AmazingMech2418

@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.