Make your own OS (Part 2)

Tharindu Piyumal
3 min readOct 11, 2022

--

Hello, previously we talked how to start to make own OS. In that article explained how to write 0xCAFEBABE to the eax register. Today we are going to use C language to upgrade previous OS.

lets begin….

First we setting up memory stack.so, we need to point the esp register to the end of a correctly aligned area of free memory.using below codes, we reserve a piece of uninitialized memory in the bss section in the ELF file of the kernel for our stack.To declare uninitialized data, we use the NASM pseudo-instruction resb

And then we use below code esp to the end of the kernel_stack memory.

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

Lets connect C language

first lets make simple c function,

/* The C function */                                 
int sum_of_three(int arg1, int arg2, int arg3) {
return arg1 + arg2 + arg3;
}

Using Assembly to call a C function

we wrote a function in c language. In hare we are going to call that function using assembly language. after a successful execute the function it returns the sum of 3 arguments. then the return value saved in eax register.

Now complete loader.s file looks like below.

Compiling C code

To compile codes wrote in c, we using lot of flags to GCC like below. these flags use such as enable all warnings and treat them as errors .

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

Make Build Tool

Lets make build tool which help to compile and make .iso files easily. We will use make as our build system.

let’s save above code as Makefile. The directory look like below.

OS
| -boot
| | -grub
| | | -menu.lst
| | | -stage2_eltorito
| -kmain.c
| -loader.s
| -Makefile
| -link.ld

now we can create a new os.iso file, boot that without typing all command. we need to run below command only.

make run

after the successful booting on our new os, we can see our output on eax register.

return value of the c function saved in eax registry

Now we can use c language for our future modifications on our os.

Git repository and branch, which include above codes, you can find here.

Read next part

Thank You..

--

--