Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why assembly language is still needed if we have high level languages offering sophisticated tools? [closed]

At university I have taken the compulsory course in Computer Architecture and Assembly Language Programming. I have found assembly language quite time consuming and hard to take grasp on. I still cannot understand the reason why assembly language is taught. I asked same question from my instructor and he smiled and said you will get to know in future. But I have a nature that I can't wait for so long for the answer once a question teases my mind. So I want to ask why do we still require assembly language if there are very powerful, fast and efficient programming languages and on top of that such languages offer wide variety of tools that make programming easy. So why?

like image 931
John Tota Avatar asked Jul 04 '12 05:07

John Tota


2 Answers

One fairly good reason is all programming languages are an abstraction of how a computer works. In theory, for high level languages, the abstraction means that the programmer never has to worry about the details of the computer.

But the abstraction is always leaky, e.g. most (if not all) high-level languages don't automatically optimise very well for the CPU caches (e.g. cache-coherency and false sharing) and efficient multithreading is always tricky. This means that if performance is critical, the programmer might have to write their code differently.

The best way of getting the performance is by understanding how the machine works so that the programmer can do large architectural transformations that a compiler can't easily. As an example, making sure that threads process data in multiples of 64 bytes (to reduce false sharing on cache lines), or doing an array-of-structs ⇔ struct-of-arrays transformation.


Assembly (and C to some extent) is a really good way of learning these performance details, since it makes it really easy to understand exactly what the computer is doing because you control exactly what happens at each step.

(The leaky abstraction point also applies to accessing specific/custom hardware components.)

like image 97
huon Avatar answered Sep 20 '22 08:09

huon


Machine code is still needed for key tasks, e.g.

  • Hardware specific code, such as device drivers
  • Embedded devices, where size of the code is important
  • Low level code where performance is critical etc.

Often, assembly language can be mixed into higher level languages, such as C, where assembly can be added inline to the C code.

That said, it is still a good idea to get a grasp of the underlying hardware architecture even if you will be programming in a high level language, as it will give you insight into how stacks, heaps, pointers etc work.

like image 36
StuartLC Avatar answered Sep 23 '22 08:09

StuartLC