MacOS environment-handwriting operating system-45-C language development application

C language application development

1 Introduction

I skipped a few sections earlier. One is about a rather detailed content and the other is about the link to the original teacher’s article that is broken.

I'm not very good at taking notes, so I skipped it. Some bugs were fixed and some features were added.

It’s not that important, so I’ll ignore it.

After our operating system adds a kernel interface export mechanism

Already ready to run applications as a platform

But one shortcoming of current application development is that

We can only use assembly language to develop applications. It is too tiring to develop programs in assembly language.

It would be great if C language could be used

For example, when we want to develop an application that outputs a character on the console

It would be nice if the code could be written like this in C language (app.c):

void api_putchar(int c);

void main() {
    
    
    api_putchar('C');
    return;
}

If the above code can be compiled into a binary file and can be loaded and executed by the system

Then developing applications on our operating systems doesn’t have to be as painful as it used to be

What we will do in this section is to study how to use C language to develop applications that run on our system

2.Code

In fact, the steps we use to develop the kernel in C language can be transferred to developing applications.

The basic logic we actually follow when developing the kernel is this

Use assembly to develop the underlying interface and then use C language to call the interface exported from the assembly language to implement business logic.

Then compile the C code into a binary file

Then use the objconv tool to decompile C language into assembly language

The next step is to combine the original code developed in assembly with the decompiled assembly code into one

Finally, an assembly compiler is used to compile the integrated assembly code into a unified binary executable file.

We use C language to develop applications and follow the above logic

First we export the kernel API interface in assembly language

Use C language to call the interface exported by assembly language to complete the writing of business logic

Then compile the C language code into binary and use objconv to disassemble it

Finally, the two assembly language codes are combined into one and compiled into an executable binary file.

Based on the above logical steps

The first step we need to do is to export api_putcharthe interface for function calls in assembly language

The implementation code is as followsapi_call.asm

[SECTION .s32]
BITS 32
call main
retf

api_putchar:
  mov edx, 1
  mov al, [esp + 4]
  int 02Dh
  ret

%include "app.asm"

Let’s look at the api_putchar part first

We mentioned earlier that all APIs in the kernel correspond to a number. The API number that outputs one character to the console is 1.

When you want to call the corresponding api, just put the corresponding number into the register edx

At the same time, submit the parameters to the specified register and finally call the 2D interrupt.

at the front of the code

We first execute the statement call main

In other words, the main function is called directly. This means that when developing a program in C language, the main entry function must be main.

All program codes developed in C language are compiled into binary files and then disassembled into assembler

The disassembled program file is app.asm

Integrate the code of app.asm into api_call.asm through the include directive

Finally, use the assembly compiler nasm to compile api_call.asm. The resulting binary file is an application developed in C language.

3. Compile and run

After compiling and running (note that the makefile is updated)

After running

Switch focus and enter hlt to run the program

The following results were printed

Insert image description here

Guess you like

Origin blog.csdn.net/w776341482/article/details/128676915