Why do we even need assembler when we have compiler?

放肆的年华 提交于 2020-01-04 07:09:10

问题


If compiler converts high-level language to machine code, why do we even need assembler? Are there any assembly level language and we can't use compiler for that?


回答1:


why do we even need assembler?

Many people don't need to know assembly language.

It exists so we can talk about / analyze machine code, and write/debug compilers more easily.

Compilers have to be written by humans. As @old_timer points out, when designing a new CPU architecture, you always give names to the opcodes and registers so you can talk about the design with other humans, and publish readable manuals.

Or for OS development, some special privileged instructions can't be generated by compilers. And you can't write a context-switch function that saves registers in pure C.

CPUs run machine-code, not high-level languages directly, so computer security / exploits, and any serious performance analysis / tuning require looking at the instructions the CPU is running. Mnemonic names for the opcodes are very helpful in thinking and writing about them. mov r32, imm32 is much easier to remember and more expressive than B8+rd imm32 (the range of opcodes for that mnemonic).


I regularly use asm for easily creating the machine code I want to test for microbenchmarks. A compiler has to create efficient machine code, not just correct machine code, so it's common for humans to play around with asm to see exactly what's fast and what's not on various CPUs.

See http://agner.org/optimize/, and other performance links in the x86 tag wiki.

e.g. see Can x86's MOV really be "free"? Why can't I reproduce this at all? and Micro fusion and addressing modes for examples of micro-benchmarking to learn something about what's fast.

See C++ code for testing the Collatz conjecture faster than hand-written assembly - why? for more about writing asm by hand that's faster than what I could hand-hold gcc or clang into emitting, even by adjusting the C source to look more like the asm I came up with.

(And obviously I had to know asm to be able to look at the compiler's asm output and see how to do better. Compilers are far from perfect. Sometimes very far. Missed-optimization bugs are common. To think of new optimizations and suggest that compilers look for them, it's a lot easier to think in terms of asm instructions than machine code.)

Wrong-code compiler bugs also sometimes happen, and verifying them basically requires looking at the compiler output.


Stack Overflow has several questions like "what's faster: a++ or ++a?", and the answer completely depends on exactly how it compiles into asm, not on source-level syntax differences. To understand why some kinds of source differences affect performance, you have to understand how code compiles to asm.

e.g. Adding a redundant assignment speeds up code when compiled without optimization. (People often fail to realize that compiling with/without optimization isn't just a linear speedup, and that it's basically pointless to benchmark un-optimized code. un-optimized code has different bottlenecks... This is obvious if you look at the asm.)




回答2:


Quoting from @TylerAndFriends's answer on Why do we need assembly language? on cs.SE (a duplicate of this):

Assembly language was created as an exact shorthand for machine level coding, so that you wouldn't have to count 0s and 1s all day. It works the same as machine level code: with instructions and operands.

Though it's true, you probably won't find yourself writing your next customer's app in assembly, there is still much to gain from learning assembly.

Today, assembly language is used primarily for direct hardware manipulation, access to specialized processor instructions, or to address critical performance issues. Typical uses are device drivers, low-level embedded systems, and real-time systems.

Assembly language is as close to the processor as you can get as a programmer so a well designed algorithm is blazing -- assembly is great for speed optimization. It's all about performance and efficiency. Assembly language gives you complete control over the system's resources. Much like an assembly line, you write code to push single values into registers, deal with memory addresses directly to retrieve values or pointers. (source: codeproject.com)




回答3:


Some more examples:

  • Interacting with the interrupt handler to implement atomic opaerations such Linux's atomic operations on ARMv5 and earlier.
  • Call a system call only if the signal handler has not been called in QEMU linux-user.
  • Initalising a computer to a state that a compiler can be used, for example configuring the memory controller.
  • Entry and exit to/from interrupt handlers and system calls.


来源:https://stackoverflow.com/questions/51780158/why-do-we-even-need-assembler-when-we-have-compiler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!