My question is why nowadays some operating system event handling is still written in assembly language instead of a higher level language such as C, when the kernel itself is written mostly in C?
The language abstracts away access to CPU registers, and an OS when handling events has to save context, so it needs access to the registers at the point of the event, thus breaking the C spec.
C is an abstraction from the machine code that runs on the machine (although much closer than most other languages).
For those things machine code statements that cannot be expressed in C, and maybe for the extra optimization not provided by the C compiler assembly is used, mostly in the form of inline assembler.
In the kernel source code tree this is stored under
<arch> is a specific architecture name. It is actually only a small part of the complete kernel source.
You cannot do this in C :)
lgdt[xxxx] mov eax, cr0 or al, 0x01 mov cr0, eax
Am trying to get into x86 protected mode. Obviously I can still do this in C by "emitting" raw machine codes, but still in case I require to access precise offets - i am out of luck mostly.
The second example is BootLoader. On x86 systems, it is required that the traditional boot code be exactly 512 bytes long and the last two bytes be 0xAA and 0x55 (or 55 AA exactly) respectively... Ensuring such a thing with C compilers are a nightmare and assembler does the job in a fantastic way.
There are many more such cases where Assembly is not just preferrable - but is the only means.
asm is slimmer and generally a lot faster than C junked up with libraries etc, and the OS is handling a LOT of events ALL of the time. You want slim and fast for this function.