The scheduler of my mini os is written in assembly and I wonder why. I found out that the instruction eret
can't be generated by the C compiler, is this somthing that can be generalized to other platforms than Nios and also x86 and/or MIPS architechture? Since I believe that part of os is always written in assembly and I'm searching for why a systems programmer must know assembly to write an operating system. Is is the case that there are builtin limitations of the C compiler that can't generate certain assembly instructions like the eret
that returns the program to what is was doing after an interrupt?
For instance, the PL/M51 allows to write an OS without any assembly for an intel 8051 processor. To sum up, the answer is: assembly isn't mandatory, you just need a language with enough expressive power to describe the tasks you OS shall perform.
It helps in understanding the programming language to machine code. In computers, there is an assembler that helps in converting the assembly code into machine code executable. Assembly language is designed to understand the instruction and provide it to machine language for further processing.
Assembly languages don't depend on an OS, but on the CPU's instruction set. Only if you call API functions (like for example a Windows API function from inline assembler code in MSVC), you get an OS dependency.
Assembly language is the closest computer language to communicate with computers. It is the machine language (1s and 0s) that a CPU uses to operate in an easy to remember and understand format. Only assembly language can take the full advantages of the processor architecture, and it is hardware dependent.
The generic answer is for one of three reasons:
Because that particular type of code can't be written in C. I think eret
is a "return from exception" instruction, so there is no C equivalent to this (because hardware exceptions such as page faults, divide by zero or similar are not C/C++ style exceptions). Another example may be saving the registers onto the stack when task-switching, and saving the stack pointer into the task-control block. The C code can't do that, because there is no direct access to the stack pointer.
Because the compiler won't produce as good code as someone clever writing assembler. Some specialized operations can be hard to write in C - the compiler may not generate very good code, or the code gets very convoluted to achieve something that is simple in assembler.
The startup of C code needs to be written in assembler, because a C program needs certain things set up before you can run actual C code. For example configuring the stack-pointer, and some other registers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With