2

So i heard that in modern operating systems and device drivers, some parts of them are still written in assembly for better memory optimisation and speed

but do developers actually write it in assembly or they just use a tool to convert their high level code into assembly?

considering that compilers even have code optimisation phase, isn't it better to use compilers? do i need to the master assembly language in order to write a good device driver or Operating system?

7

High level languages like C do not expose all functionality of the CPU's instruction set. Sometimes the compiler is smart enough to use those features without being told to, but sometimes hoping for the best is not an option – you need to write a bit of assembly.

There are three typical ways to do this:

  • The compiler might offer intrinsics, pseudo-functions that the compiler will translate to an assembly instruction. The resulting code looks like C, but you need some knowledge of the instruction set to use them properly.
  • inline assembly
  • separate assembly files that are linked later

In most cases these assembly snippets are motivated by having access to special assembly instructions (e.g. syscall, or instructions related to interrupts). This kind of code is typically found in kernels or in standard libraries. In some cases assembly code is used for extremely performance-sensitive code paths, where it is not OK to depend on compiler optimizations. E.g. some language runtimes use assembly snippets for critical functionality like method dispatch. In particular for crypto code, it might also be desirable to prevent any compiler optimizations as they could make the implementation vulnerable to timing attacks.

  • So overall if i want to write a good and efficient device driver or operating system, i should write some parts of it in assembly instead of converting a high level code to assembly correct? and modern O.S developers(windows, iOs, etc) do the same thing right? – John P Aug 14 '18 at 13:14
  • 2
    @JohnP Modern kernels are almost entirely in C (NT also uses C++). According to Github, Linux is 1.4% assembly. The only part that has to be written in assembly is parts of the bootloader (see also the OSDev Wiki) – x86 compatible processors start in a silly 16-bit “real mode”, and you need to set specific CPU registers to enable modern features. – amon Aug 14 '18 at 13:31
  • @amon: you are probably correct, but 1.4% seems to be a lot for a system with >20 milllion lines of code (good answer, upvoted). – Doc Brown Aug 14 '18 at 15:31
  • @amon so where do people write in assembly, other than bootloader, in modern operating systems? device drivers? – John P Aug 14 '18 at 16:23
  • 2
    @John, I wrote device drivers for several years, and never once had the occasion to personally write assembly, although I had to debug in assembly a handful of times per year. There is not very much assembly code in a kernel, and it changes much more infrequently than other code, mostly when a new generation of processor comes out. – Karl Bielefeldt Aug 14 '18 at 16:50

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.