ChatGPT解决这个技术问题 Extra ChatGPT

Is it worthwile to learn assembly language? [closed]

Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 11 years ago. Improve this question

Is it still worthwhile to learn ASM?

I know a little of it, but I haven't really used it or learned it properly because everything I learn to do in assembler I can do in 1/10th the time with some language like C or C++. So, should I really learn and use ASM? Will it do me any good professionally? Will it increase my resourcefulness? In short, would it make me a better programmer?

Note: I am talking about low-level assembly like FASM or NASM and not something like HLA (High-Level Assembler).

Note, that assembly language tied to a processor/controller.
You mean it isn't cross-platform?!!
Ok, so there is a different assembly language for every processor???!!!!!!!!!!!!!!!!!!!!!!!!!
A different assembly language for each family of processors. 386 code runs on any later x86 processor including Intel and AMD's offerings -- but some feature sets are proprietry extensions. The language is the same for these extra instructions but the resulting binary won't be portable.

s
spraff

I learned from Kip Irvine's book. If you ignore the (fair) criticisms of his (irrelevant) libraries, I can recommend it as a good introduction to the language itself -- although for the really interesting stuff you have to hunt out obsessives on the net.

I think it's useful to understand what happens at the lower levels. As you research assembler you will learn about cpu pipelining, branch prediction, cache alignment, SIMD, instruction reordering and so on. Knowledge of these will help you write better high-level code.

Furthermore, the conventional wisdom is to not try to hand-optimise assembly most of the time but let the compiler worry about it. When you see some examples of the twisted things that compilers generate, you will better understand why the conventional wisdom holds.

Example: LFSRs run fast with the rotate-with-carry instruction, for specific cases like this it's just as easy to write the assembler version as it is to discover whether or not the compiler is smart enough to figure it out. Sometimes you just know something that the compiler doesn't.

It also increases you understanding of security issues -- write-or-execute, stack overruns, etc.

Some concurrency issues only become apparent when you are aware of what is happening at the per-instruction level.

It can be useful sometimes when debugging if you don't have the complete source code.

There's the curiousity value. How are virtual functions implemented anyway? Ever try to write DirectX or COM programs in assembler? How do large structures get returned, does the calling function offer a space for them or vice-versa?

Then there are special assembly languages for graphics hardware, although shader languages went high-level a few years ago, anything which lets you think about a problem a different way is good.


Well written answer +1. Learning and using are two different things, learning it is a good idea, when/if to use it comes with experience.
To give an example, Linus Torvalds uses a disassembler to debug his code so I think he has good knowledge of x86 assembly.
J
James Martinez

I find it interesting that so many people jump to say that yes, you need/should learn assembly. To me the question is how much assembly do you need to know? I don't think you have to know assembly like a programming language, that is I don't believe that everyone should be able to write a program in assembly, but on the other hand, being able to read it and understand what it actually means (which might require more knowledge of the architecture than the assembler) is enough.

I for sure cannot write assembly (i.e. write any non trivial piece of code in assembly), but I can read it and that together with knowledge of the actual hardware architecture, and the calling conventions that are being used is enough to analyze performance, and identify what piece of C++ code was the source of that assembly.


You mean i should just learn the basic push, pop, mov and invoke commands?
I agree, the need for knowing assembly depends a lot on what programming branch you are working with. Web/desktop/database? Forget assembler. Computer games and graphics? It could be somewhat handy. OS development or real-time embedded systems? It is a must. And so on.
You're misrepresenting the other answers -- nobody is saying you need/should learn assembly; just that it is useful or worthwhile, as per the question. Your answer is still relevant though.
@burningprodigy: My believe is that you should have some basic knowledge of how the instructions are in the assembler that you use (i.e. what is the order of arguments and the like), and some very basic understanding of a few commands, what a load or a store is. Then if you need to look at some piece of assembly you can use a reference to understand each one of the instructions, and in doing so you will start learning the basics that you need.
I agree. Especially only knowing assembly will gain you little at best about assessing efficiency on modern CPUs. If that's the goal a little basic knowledge of assembly with a fair share of the basics of computer architectures is way more useful. Most times I need assembly is looking through dumps of optimized programs, or for fun (ie security stuff, writing an mini kernel,..). But I can't remember the last time I changed some design decision because of assembly knowledge.
C
Community

Yes - the primary reason to learn assembly for C and C++ developers is it helps understanding what's going on under the hood of C and C++ code. It's not that you will actually write code in assembly, but you will be able to look at code disassembly to assess its efficiency and you will understand how different C and C++ features work much better.


n
nbro

It's worthwhile to learn lots of different languages, from lots of different paradigms. Learning Java, C++, C#, and Python doesn't count, since they are all instances of the same paradigm.

As assembly is at the root (well, close to the root) of all languages, I for one say that it is worthwhile to learn assembly.

Then again, it's worthwhile to learn a functional programming language, logic programming, scripting languages, math-based languages. You only have so much time, so you do have to pick and choose.


And then, when you finally have learned them all and think you have a glimpse of what is going on, you discover J.
n
nbro

Knowing ASM is also useful when debugging, as sometimes all you have is "ASM dump of the error".


G
GJ.

Depend of which programming level you wish to reach. If you need to work with debuggers then YES. If you need to know how compilers works then YES. Any assembler/debugger is CPU dependent, so there is a lot of work, just check x86 family how big and old is it.


May be you meant CPU family dependent?
N
Necrolis

Do you have any use for it in what you plan to do? is it going to aid you in any way in what you currently do or plan to do? those are the two questions you should ask yourself, the answer to those is the answer to your question.

In a more general sense, yes, I'd say in my opinion is well worth learning asm (something like x86 or arm), how well it serves you depends on what you programming and how your debugging it.