WVB Operating System

Warunajith Bandara
3 min readJul 23, 2021

Second Stage

Welcome back to the second stage of my Operating system article series. In my first article, I discussed how to build a simple operating system using ubuntu. This article will explain how growing up that simple operating system from the scratch.

Up to now, We have been using Assembly language to build our operating system. But when we deep dive into coding parts, we need to use a convenient programming language like C. This article will be explaining how to use both C and Assembly language to improve our simple operating system.

In order to jump into C language, we need to create a stack using assembly language. In our first implementation, We could point “esp to a random area in memory. But This is not a good idea because we don’t know how much memory is available or if the area “esp would point to is used by something else. In order to overcome that, It is better to use the “bss” section in the “loader.s” file to reduce the size of the OS executable. The below code will create a stack using assembly.

KERNEL_STACK_SIZE equ 4096                  ; size of stack in bytes

section .bss
align 4 ; align at 4 bytes
kernel_stack: ; label points to beginning of memory
resb KERNEL_STACK_SIZE ; reserve stack for the kernel

Next, The stack pointer is then set up by pointing “espto the end of the “kernal_stack” memory. The below code will implement that part.

mov esp, kernel_stack + KERNEL_STACK_SIZE   ; point esp to the start of the
; stack (end of memory area)

In the next steps, We are going to call a C function from the assembly code. In order to do that we have to have a simple C program called “kmain.C” separate from others files. The function inside the C file would be like this and the return value of the function is placed in the “eax” register.

 
int sum_of_three(int arg1, int arg2, int arg3)
{
return arg1 + arg2 + arg3;
}

After that, We can call “kmain.C” function using assembly language in the “loader.s” file like below.

; The assembly code
extern sum_of_three ; the function sum_of_three is defined elsewhere
push dword 3 ; arg3
push dword 2 ; arg2
push dword 1 ; arg1
call sum_of_three ; call the function, the result will be in eax

After the above changes, The final “loader.s” file would be like this.

Now is a good time to set up some build tools to make it easier to compile and test-run the operating system. A simple Makefile for the Operating system could look like the following code.

Now I am going to start the Operating system with the simple command “make run” which will compile the kernel and boot it up in Bochs as defined in the Makefile above.

My Final outcome would be like this.

References:

https://github.com/Warunajith/wvbOS

--

--

Warunajith Bandara

Software Engineering Undergraduate at University of Kelaniya Sri Lanka