My Operating system Development Experience and understanding -Part 02

M.R.M Abdullah
4 min readJul 22, 2021

--

In this article , I will introduce the use of C instead of assembly to write the operating system. Although the compilation and CPU and registers are very efficient. But using C is more friendly for us.

on last article I had written about how to setup OS development environment and First basics you can read that here : https://bit.ly/OSpart01

1. Create a Stack

A prerequisite to use C is the stack because all Non-Trivial C programs use the stack.

Setting the stack is not more difficult to make the ESP register to correct alignment (recommended from performance recommended 4-byte aligned) idle memory area (remember, the stack is more difficult to grow on X86).

We can point ESP to the random area in memory, because so far, the only thing in memory is the I / O of GRUB, BIOS, OS kernel, and some memory maps. But this is not a good idea — we don’t know how many available memory, or whether the area points to the ESP is used by other things.

A better idea is to keep an unin-initialized memory in the BSS section of the ELF file of the kernel. It is best to use the BSS portion instead of the DATA section to reduce the size of the OS executable file.

Since GRUB understands ELF, GRUB will allocate all memory reserved in the BSS section when loading OS. NASM Pseudo-Directive RESB can be used to declare an unmelted data:

you need to type this on loader.s file

There is no need to worry about the memory that will not be initialized for the stack, because it is not possible to read the stack position that has not been written (no manual pointer is required).

(The correct) The program cannot pop up the element from the stack without pushing the elements in the stack. Therefore, the storage location of the stack will always be written before being read. The stack pointer is then set by pointing the ESP to the end of the kernel_stack memory:

2.Call C code in assembly code

The next step we have to do is Call Function from Assembly Code.
There are many different conventions for how to call C code from the assembly code. The CDECL call convention is used here because this is the agreement of GCC. CDECL call conventions indicate that the parameters of the function should be passed through the stack (on x86).
The parameters of this function should be pushed in the stack in the order from right to left, ie, first push the rightmost parameter. The return value of the function is placed in the EAX register. The following code shows an example:

this is just an example

3. Packaging structure

You often encounter “configuration bytes”, which are a collection of bits arranged in a particular order. Below is an example of 32-bit:

In the previous example, we cannot guarantee that the size of this structure is 32 bit. Because the compiler will do something. It will increase padding so that it is aligned. In order to achieve our goal, we need to tell the compiler, where you don’t need you to intervene.

4. Compile C code

When compiling, we need a lot of logo parameters. This is because C code should not assume that there is a standard library because our operating system is not available standard library. For more information on the logo, see the GCC manual. The flag used to compile C code is:

his recommendation plus, after all, we can’t ignore Warning.

5.build tool

Here we use Make to compile and test run the operating system.
A Makefile file is as follows:

After writing Kmain.c we can use make run. It will compile the kernel and start in Bochs. || Just create Kmain.c file as a black file for now we will edit that on next article||

Here we are watching the structure of the directory

| — bochsrc.txt
| — iso
| | — boot
| | — grub
| | — menu.lst
| | — stage2_eltorito
| — kmain.c
| — loader.s
| — Makefile

Error warning

in this article we just creating a makefile for compile c code after finishing this step you can check it by just “make run “ code

we you do that some time you will get some errors those are :

makefile:12: *** missing separator. stop.

when you encounter this Error it because of continuous 8 spaces you can’t type 8 spaces continuously on makefile in order to shootout this error you need to replace 8 spaces by tab

some time you will get

make: *** no rule to make target ‘kmain.c’, needed by ‘kernel.elf’. stop.

this error will happen because of your careless mistakes when you creating a kmain.c file please make sure you name that is simple letters

that's all

thanks for Studying OS development with my guideline , see you at next article

Abdullah M.R.M

--

--

M.R.M Abdullah
M.R.M Abdullah

Written by M.R.M Abdullah

Software Engineering undergraduate at University of Kelaniya.

No responses yet