17

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?

  • 5
    "Mostly in c" -- and guess what the rest is? ;) – goldilocks Feb 24 '15 at 13:23
  • @goldilocks well it is in assembly. But why, while other parts are in c ? – MAKZ Feb 24 '15 at 13:25
  • 4
    I'm no expert on it but there are some low level hardware related things that can't be done in C; these are usually architecture specific. "Inline ASM" is often used in C code for that purpose, so e.g. foobar() will be defined, using inline assembly, one way on one platform and another way on some other. This keeps the use of asm to a minimum, but it can't be completely avoided. – goldilocks Feb 24 '15 at 13:29
  • How do you set the Global Descriptor Table Pointer register in C? – immibis Feb 25 '15 at 0:04
24

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.

  • This is actually the main reason. Some embedded C compilers do have extensions that allow them to address registers (usually via pre-declared global constants/variables). They can do it because they only target one architecture. But general-purpose C compilers target too many different architectures to make such extensions reasonable. Thus they usually just implement an asm embedding mechanism (besides, that would make them non-standard) – slebetman Feb 25 '15 at 0:17
18

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/<arch> and include/asm-<arch> where <arch> is a specific architecture name. It is actually only a small part of the complete kernel source.

6

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.

-5

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.

  • 2
    Optimizing C compiler technology has become pretty good. It's a myth that asm would be generally a lot faster than C. In any case that's not the reason that low-level operating system operations use asm. It mostly has to do with operations that can't be expressed in C like memory barriers, and register dances for non-C calling conventions, etc... – Celada Feb 25 '15 at 1:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.