Build up my OS (Wathsala’s OS) — Part 02

Wathsala Yahampath
3 min readOct 12, 2022

--

today we are going to implement our second part of the OS. I highly recommend to follow my first article on this OS developing. If you haven’t already read that article, I strongly advise you to do so because you won’t be able to understand this article otherwise. Following is the above-mentioned article.

link-

Build up my OS (Wathsala’s OS) — Part 01 | by Wathsala Yahampath | Oct, 2022 | Medium

To start off the conversation, let’s review a few points from the previous week. The two key things we accomplished were setting up our OS development environment and booting a basic OS, as was already explained. In order to do that, we used Bochs as the virtual machine and Ubuntu as the host operating system. To write the OS kernel, we primarily used Assembly. The only thing that OS did was putting a special number cafebabe in the eax Register.

Setting Up a Stack

One requirement for using C is a stack, as a stack is used by all non-trivial C programs.

No more difficult than accurately aligning the esp register to the end of a free memory space is setting up a stack.

The only things in memory so far are GRUB, BIOS, the OS kernel, and some memory mapped I/O. So, we can’t point the esp to a random section of memory since we don’t know if that memory space is available. Therefore, it is preferable to leave some uninitialized memory in the kernel’s ELF file’s bss section. We use bss section rather than the data section, to reduce the size of the OS executable. Since GRUB recognizes ELF, it will allocate any memory reserved in the bss section when the OS is loaded.

To declare uninitialized data, the NASM pseudo-instruction resb can be used.

Copy the below code into the file loader.s, right after the last definition (CHECKSUM).

Then add the following code to set up the stack pointer: This is done by pointing esp to the end of the kernel_stack memory.

first function using C language

Following is the first C function that we are going to call from our assembly code. It will take three integer arguments and return the sum.

Copy the following code to a new file named kmain.c. This will be the source file of our C code.

<script src=”https://gist.github.com/WathsalaYahampath/b5de592542aed739c3d3ef919e74c2b9.js"></script>

To achieve the task, put the following code after the esp instruction in the loader.s file.

Compiling

There are numerous flags for GCC that must be used when compiling the C code for the OS. This is because there is no standard library accessible for our operating system, therefore the C code shouldn’t assume its presence. We should also activate all warnings and handle them as errors.

When compiling the C code, we use the following flags to achieve the aforementioned goals (We will use these in the Makefile which we are going to create in the next step):

-m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector 
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror

Set up Build Tools

Now we’ll set up some build tools to make compiling and testing our operating system easier. We will use make as our build system.

Here’s a simple Makefile for our OS:

Now, Our working directory should now look like the below figure.

| — bochsrc.txt
| — iso
| | — boot
| | — grub
| | — menu.lst
| | — stage2_eltorito
| — kmain.c
| — loader.s
| — Makefile
Create a file named Ma

Create a file named Makefile and save the above text in that file.

Now we should be able to start the operating system using the simple command make run and it will compile the kernel and boot it in Bochs.

Then you will get the following output.

Finally, quit the Bochs and use the command cat bochslog.txt to display the log it generated. If you can find the summation EAX = 000000006 (the sum of the arguments we provided), you are successful!

Let’s meet next week in the third article!!

Bye!!

--

--