Skip to content
← Back to Community
A beginners guide to OS Dev: Part 1
Profile icon
CSharpIsGud

It will help greatly to go into this guide with some understanding of C and Assembly.

Before you start you will need to install nasm, genisoimage and may want to install qemu or bochs in order to test your OS without rebooting your computer, bochs is preferred as it has more debugging capability than qemu

By the end of this part you should have a very basic OS that can boot.
This part of the guide will not cover anything but making a basic OS that does absolutely nothing due to how long it already is.
If people like this I will make a part 2 going over how to use C with the OS and write text to the screen.

Booting

The first thing to do is create the main assembly file, I will call it Boot.asm

global entry MAGIC equ 0x1BADB002 ; These tell the bootloader that we are bootable FLAGS equ 0 SUM equ -MAGIC section .text: align 4 ; Align everything after this to a 4 byte boundary, which the boot header needs to be aligned to dd MAGIC ; dd means "define double word", a word is usually 2 bytes on most computers, so a dword is 4 bytes. You can think of a word as being a short in C and a dword being an int. dd FLAGS dd SUM entry: jmp entry ; For now we won't do anything but loop forever.

This should boot and do nothing.

But how do we compile it?

First we need a linker script since grub(the bootloader this tutorial will be using) is loaded below 1 MB so we should load above that!

Now I don't expect you to know how to do this, and neither do I, so I will copy this from littleosbook which is another good tutorial on writing an operating system from scratch.

ENTRY(entry) /* the name of the entry label */ SECTIONS { . = 0x00100000; /* the code should be loaded at 1 MB */ .text ALIGN (0x1000) : /* align at 4 KB */ { *(.text) /* all text sections from all files */ } .rodata ALIGN (0x1000) : /* align at 4 KB */ { *(.rodata*) /* all read-only data sections from all files */ } .data ALIGN (0x1000) : /* align at 4 KB */ { *(.data) /* all data sections from all files */ } .bss ALIGN (0x1000) : /* align at 4 KB */ { *(COMMON) /* all COMMON sections from all files */ *(.bss) /* all bss sections from all files */ } }

nasm -f elf32 Boot.asm should create a file called Boot.o

Which we can then turn into an elf binary by linking it with ld

ld -T link.ld -melf_i386 Boot.o -o kernel

We then must download grub, which can also be found in littleosbook, http://littleosbook.github.com/files/stage2_eltorito

Now we create the folder structure our os iso will be built from

mkdir iso mkdir iso/boot mkdir iso/boot/grub cp FILE_FROM_LITTLEOSBOOK iso/boot/grub

And our grub configuration to put in /boot/grub/menu.lst

default=0 timeout=0 title Replit kernel /boot/kernel

Now to run it!

A bash script to automatically compile/link and run the OS

nasm -f elf32 Boot.asm ld -T link.ld -melf_i386 Boot.o -o iso/boot/kernel genisoimage -b boot/grub/stage2_eltorito -R -boot-load-size 4 -no-emul-boot -boot-info-table -A Replit iso > os.iso # Uncomment for Qemu # qemu-system-x86_64 -kernel iso/boot/kernel # Uncomment for Bochs # bochs -q

To use bochs you must make a configuration file named bochsrc

megs: 32 romimage: file=/usr/share/bochs/BIOS-bochs-latest vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest ata0-master: type=cdrom, path=os.iso, status=inserted boot: cdrom log: bochslog.txt clock: sync=realtime, time0=local cpu: count=1, ips=1000000
Voters
Profile icon
ichbindede
Profile icon
DaRubyMiner360
Profile icon
temporarilyMe
Profile icon
z80
Profile icon
programmeruser
Profile icon
elipie
Profile icon
Kookiez
Profile icon
FedericaC
Profile icon
ChristopherHosk
Profile icon
EpicGamer007
Comments
hotnewtop
Profile icon
Highwayman

hum. Time to try to learn asm again ;-;

Profile icon
programmeruser

Question: if you write a VGA driver will it work for something like HDMI?

Profile icon
MocaCDeveloper

I mean there is allot more than just this.

This is the very literal bare bare bones of an OS..there is much more things that need to be implemented before you have an actual functional OS

Profile icon
CSharpIsGud

@MocaCDeveloper it just isn't one that competes with any graphical OS. Or any OS in general.

Profile icon
[deleted]

well im on a mac so this really sucks for me
:(

Profile icon
AmazingMech2418

I'm trying to use QEMU and I got this:
image

Any idea how to fix it?

Profile icon
CSharpIsGud

@AmazingMech2418

mkdir libtmp; cd libtmp; wget https://repl.it/@CSharpIsGud/VolantOS.zip; unzip VolantOS; mv qemu/lib* ../; mv qemu/*.bin ../; mv qemu/*.rom ../; cd ..; rm -rf libtmp

try this

Profile icon
AmazingMech2418

@CSharpIsGud That works! Thank you!

Profile icon
LoganSpong

Should I make a bash repl to do this?

Profile icon
LoganSpong

Is it possible to plugin python in assembly or C for like, running python scripts?

Profile icon
DungeonMaster00

@LoganSpong you probably need a python intepreter first then you could add some python stuff

or you could make a compiler instead of an interpreter and compile python to machine code

Profile icon
LoganSpong

@DungeonMaster00 the 2nd option legit scared me. Compiling Python to machine code? No way man.

Profile icon
CSharpIsGud

@LoganSpong https://repl.it/@CSharpIsGud/CPython2
At first I was writing the parser by hand, then I realized why waste my time doing that when I can just use py's ast module.
I didn't even finish it because what's the point?
There are other more complete ones(Though most of them are abandoned or not meant to be a fully featured compiler) like https://nuitka.net/
Though mine was going to be completely bare and therefor safe for OS dev

Profile icon
AmazingMech2418

On genisoimage, using this exactly as you did it, I get
image

Profile icon
akkina

AWESOME!

Profile icon
akkina

AWESOME!

Profile icon
staticvoidliam7

how do i install nasm and the others with $npm install

Profile icon
CSharpIsGud

@LiamDonohue npm is the package manager for nodejs, this isn't a javascript project

Profile icon
staticvoidliam7
Profile icon
CodeLongAndPros

When is the C coming out?

Profile icon
CSharpIsGud

@CodeLongAndPros in the second part

Profile icon
lynnlo

@CSharpIsGud
What about B#?

Profile icon
CodeLongAndPros
Profile icon
AmazingMech2418

Since you are using an ELF executable, what platform does this work for? ARM? x86? x64? AMD?

Profile icon
CSharpIsGud

@AmazingMech2418 no idea, I only have intel computers

Profile icon
AmazingMech2418

@CSharpIsGud I think x86 then.

Profile icon
AmazingMech2418

@CSharpIsGud Also, I don't see any C in this tutorial. Will that be in the second part?

Profile icon
CSharpIsGud
Profile icon
xxpertHacker

@CSharpIsGud By "C," you're referring to C++, right... right!?

Profile icon
AmazingMech2418
Profile icon
CSharpIsGud

@StudentFires You could probably do it in C++ too but that will only add more ways for things to go wrong, using C++ in os dev is really only useful if you are addicted to classes

Profile icon
xxpertHacker

@CSharpIsGud Isn't Java meant for those who are addicted to classes?

Profile icon
AmazingMech2418

@StudentFires Pretty much, but it still can be useful for some things like Android development (Kotlin is used there too).

Profile icon
CSharpIsGud

@StudentFires or C#, im not addicted to classes but I like having vaguely C like syntax without having to worry about memory

Profile icon
Navinor

no idea what this is but it's cool nevertheless

Profile icon
CSharpIsGud

@Warhawk947 Think of "WarhawkOS" but its an OS and not a console program

Profile icon
Navinor
Profile icon
[deleted]

You don't need to use GRUB though - it's more fun to write your own bootloader.

Profile icon
CSharpIsGud

@sugarfi It just makes it more painful if you make a mistake

Profile icon
[deleted]

@CSharpIsGud what do you mean?

Profile icon
CSharpIsGud

@sugarfi Do you have any IDEA how long this small mistake took me to fix

loadidt: cli push edx mov edx, [esp + 4] lidt [edx] pop edx sti ret

imagine not being able to tell if your os is crashing or if the bootloader messed something up

Profile icon
[deleted]

@CSharpIsGud fair. that's the painful part - having to write asm code so that c can call it. if you leave out the pop ebp or something then you will most likely get a segfault

Profile icon
CSharpIsGud

@sugarfi a segfault, I wish. I wish I had segfaults, they would of saved much time (I do have segmentation enabled but I have the segments set to span the entire address space as I have paging working)

Profile icon
[deleted]

@CSharpIsGud you still get them, just not the message. qemu will freeze up and reboot. i think it's actually called a triple fault or something, but i just call it a segfault.

Profile icon
CSharpIsGud

@sugarfi Yeah, you get them if you turn on segmentation and actually use it, you get a triple fault if the interrupt handler for them isn't there or causes another fault. however if you don't use segmentation its not even gauranteed that it was caused by something memory related

Profile icon
CSharpIsGud

@sugarfi also your running out of time to make your os, as soon as I get into user mode im going straight to writing syscalls and vesa drivers

Profile icon
[deleted]

@CSharpIsGud i already got syscalls
i'm working on loading an elf file

Profile icon
CSharpIsGud

@sugarfi im not doing syscalls yet because user mode will add some extra effort to the interrupts which I will deal with once I get there instead of changing it later

Profile icon
[deleted]

@CSharpIsGud fair. do you have an fs yet?

Profile icon
CSharpIsGud

@sugarfi not yet, im saving a file system for when I have disk io

Profile icon
[deleted]

@CSharpIsGud i just read the whole disk in my bootloader, and i'm going to use v86 mode to write it

Profile icon
CSharpIsGud

@sugarfi im going to use the ata apis instead of switching out of protected mode every time

Profile icon
[deleted]

@CSharpIsGud the problem with that is, not all disks support those. and i would only enter v86 mode when the os was shutting down, so it wouldn't matter.

Profile icon
CSharpIsGud

@sugarfi yeah but most should, and if I want to be able to read and write files at will its a necessity

Profile icon
[deleted]

@CSharpIsGud not really. if you load the files into memory, you can just write them there, and not "save" your changes to the disk until the computer powers off.

Profile icon
CSharpIsGud

@sugarfi it wastes memory that could otherwise be used for programs and if the os crashes or someone hard resets the pc all the changes are lost

Profile icon
[deleted]

@CSharpIsGud think about it. this is a small disk; all the files are stored as a tarball. there are 2 - 4gb of memory available, possibly more. and also, who's ever going to use a hobby os?

Profile icon
[deleted]

@CSharpIsGud i mean, i was going to try the ata apis too, but i couldn't find a good tutorial

Profile icon
CSharpIsGud

@sugarfi Why make it low quality when you can make it high quality, also. I bet thats what they said about linux when it was just a hobby kernel

Profile icon
[deleted]

@CSharpIsGud both good points. but like i said, lack of docs is a problem; the osdev wiki can be really bad for things beyond a bare-bones kernel, and most google searches yield at best two stackoverflow pages.

Profile icon
CSharpIsGud

@sugarfi I usally found about 10 but they were never about my specific problem and were usually caused by someone making an obvious mistake I would of caught instantly

Profile icon
[deleted]

@CSharpIsGud i have the same problem.

Profile icon
Navinor

@sugarfi big brain moment