What is the difference between writing in assembly vs converting high level language into assembly using compiler?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/376842

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
scroll top