Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some part of an os has to be written in assembly? [duplicate]

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?

like image 562
Niklas Rosencrantz Avatar asked Aug 11 '13 23:08

Niklas Rosencrantz


People also ask

Can you write an OS without assembly?

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.

Why do we need an assembler as a part of a programming tool?

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.

Does assembly languages depend on OS?

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.

What is assembly operating system?

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.


1 Answers

The generic answer is for one of three reasons:

  1. 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.

  2. 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.

  3. 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.

like image 61
Mats Petersson Avatar answered Oct 03 '22 11:10

Mats Petersson