Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am doing a research paper on programming, and I need to somehow explain assembly... I've never learned the language, but I understand what it is used for and kinda what it looks like...

MOV A,47
ADD A,B
HALT

And from what I understand, it is used for writing some compilers, small bits of code that need to be really fast, and other tasks... But this is kind of a question for people who have used/studied assembly and understand it fairly well. And no, I'm not asking for you to type up this segment of the research paper for me. That would be selfish. I just need a brief explanation, clarification on the subject (I'm just unsure if what I know is true or not) and a pointer in the right direction.

For a research paper (I need to explain this VERY simply...) what is purpose of using Assembly in today's society? Of course it was important back in the day but now with all the high level languages, when is this low level language used?

share|improve this question
1  
There are lots of reasons to use assembly in today's society. How about the situations where there are no other choice? In some closed systems, assembly is the only language you can use, because of the limited resources. –  Ramhound Feb 10 '12 at 16:09
1  
If your looking for past uses, the original RollerCoaster Tycoon was written in Assembly. –  Richard Feb 10 '12 at 16:19
    
@Richard That astounds me everytime I hear it. –  Kris Harper Feb 11 '12 at 1:21
2  
Just because something is "assembly" doesn't mean it's written like a plate of spaghetti =) –  Patrick Hughes Feb 11 '12 at 2:43
    
ZSnes was also written in assembler. That's probably the reason it hasn't been updated in five years. –  user16764 Feb 14 '12 at 5:19

10 Answers 10

The assembler is usually used when you need some heavy optimization. Take for example here, where the guy optimized the processing 6 times.

However take a note that modern compilers are very good in optimization, and unless you really knows what you are doing, you will not beat the compiler.

share|improve this answer
3  
"However take a note that modern compilers are very good in optimization" it isn't like compilers just pull the assembly out of thin air. There has to be somebody who knew that assembly first. –  Jetti Feb 10 '12 at 14:16
4  
" and unless you really knows what you are doing, you will not beat the compiler." +1 –  Gabe Feb 10 '12 at 14:18
1  
@Gabe on some compilers, yes. But that isn't necessarily true for all platforms. –  Jetti Feb 10 '12 at 15:49
    
@Jetti The compiler's optimization depends on the compiler (even different versions do differently). As for the 1st thing (about compiler writers) - you are right : they do have to know it in details. My answer shows an example how some code was optimized using assembler code. –  BЈовић Feb 10 '12 at 16:20

It's essential for implementing compilers, operating systems, and highly optimized code, especially on embedded systems.

It also can be used for debugging as viewing the compiler's output is a helpful thing.

Additionally, many architectures provide special instructions that can be reliably accessed with assembly, again used for optimization.

share|improve this answer

I'm of two minds about the use of assembly today. First, it's handy for low-level, highly optimized systems (usually embedded systems.) It is also useful for optimizing some parts of a system (like an operating system or a network stack, for example) that are have heavy performance demands placed upon them. It also can be used to create compilers for other languages, but I think most are now done in C.

Secondly, though, I think it has an educational use. It's like learning Latin. Although there isn't a country in which Latin is spoken, it's still widely taught because of its use in science, medicine, and is the antecedent of many of the the more modern languages.

Assembly is like that. Learning exactly how the bits flow around the computer gives a programmer a deeper understanding of the functioning of the machine, and that understanding may give him insight in writing higher-level languages. Assembly is as close to the machine (e.g. "close to the metal") as you can get.

share|improve this answer
    
"It also can be used to create compilers for other languages, but I think most are now done in C." do you mean as the source language? –  Jetti Feb 10 '12 at 15:50
    
Yes, I mean that very few people would sit down and hammer out a compiler in assembly if a C compiler exists for the hardware. There are always exceptions, however. –  Andrew Neely Feb 10 '12 at 17:22

While VJovic makes a good point, there are still a few situations where you will need to learn an assembly language.

1.) If you are writing a compiler and want to compile to native code. Although, you can translate to a language with a native compiler (such as C) and then use a C compiler to go from there.

2.) You are writing an operating system. While most operating systems are written in C (or another high level language) there are still parts that are written in assembly

3.) Embedded and/or limited device programming. Sometimes you need to get into the nitty gritty because your hardware doesn't have a C compiler (though this is becoming more and more rare)

share|improve this answer
1  
Sometimes the C compiler for your hardware isn't very good (not unusual for new or niche microcontrollers). In those cases you often take the assembly listing produced by the compiler (or use a disassembler if the compiler's really bad [or new]) and use that as the basis for your assembly version of the code. –  TMN Feb 10 '12 at 15:24
    
@TMN I was going to say something along those lines as well but wasn't sure how common that really is. –  Jetti Feb 10 '12 at 15:27
    
IIRC, the first Itanium compilers were pretty poor at optimizing, so even companies like Intel and HP do it. Some companies also used to provide basic compilers for free, but charge for their "professional" optimizing compilers, not sure how prevalent that is today. –  TMN Feb 10 '12 at 16:25

Assembly in and of itself consists of commands that can run directly on a machine. It's pretty much as low as you can go down the language/abstraction stack before you run into physics.

Higher level languages are built on top of assembly: C gets compiled into assembly commands. The compiler necessarily is written in assembly. Other interpreters/compilers (and their associated languages/frameworks) are built in a similar fashion up higher levels of abstraction.

The value of these abstractions is that they allow various kinds of nitty-gritty and difficult specifics to be transparent, freeing up developers to think and write at higher and higher levels. Imagine trying to write any modern app if you had to write it entirely in assembly, It may be possible, but maintainability, development time, and performance would very likely suffer.

However, Abstractions are Leaky. At a high-level, the only reasons to write assembly are that the required abstraction doesn't exist, or that the existing abstractions are leaky in a way that you need them to not be. More specifically, an embedded system may have very strict and stringent requirements (say...program size), and skilled assembly programmer may be able to beat a C compiler. The compiler's less than optimal performance in this regard(lines of assembly in the compiled output), is a leak in the abstraction.

When you're dealing with any tool used to abstract away complexity, you should only dive down the stack when you really need to.

The most common optimizations I know of are for program-size, and performance. And of course, the compiler itself must be at the assembly level.

share|improve this answer

There are also a few products out there that were never refactored to use a new language, I used to work on pharamcy software that was still written in a Series/1 macro assembler language.

There was so much business and clinical knowledge locked into the code that management never wanted to do a complete re-write, along with the constantly changing regulations they believed it was easier to emulate the environment that the code ran on and keep on making additions.

The code base was constantly evolving, but a couple of times a year I would work on code that was written before I was born (I'm in my early 30's).

I imagine that this senario is rare, but I doubt that's the only company that is keeping old assembly code alive.

share|improve this answer

Perhaps the biggest use of assembly is the need to be able to read it:

  • to check to see what your optimizing compiler has come up with
  • debugging cases where you don't have debug symbols built in

Even if the great majority of your work doesn't need knowledge of assembly, it greatly widens the scope of what is possible. It is also necessary for reverse engineering and the like. Finally, in order to really understand the strengths and weaknesses of your platform -- to have a baseline grasp of what is difficult and what is easy -- an understanding of your processing architecture includes understanding its machine code.

There are also still cases (aside from OS bootstrapping and compiler writing) where writing assembly is the best solution -- usually specialized/embedded processors like microcontrollers and DSP's. Not everything has robust system tools ab initio: if you can't do it yourself, you become a hostage to your toolset.

share|improve this answer

It's an oversimplification but Assembly is as close as you can get to the machine. As compilers and hardware performance advance this is less important but situations still exist. Some examples:

  • Graphics - these are machine specific. Getting 100% of what the device will give you requires assembly.

  • Optimizing speed - if it really needs to be fast and you are smarter than the compiler. (historically a big reason - less so today)

  • Legacy code - once upon a time memory and processing speed were more expensive than human time. People wrote assembler to optimize limited resources. Believe it or not some of this code is still around.

  • Embedded devices - by definition very close to the machine.

Good luck on the paper prohect As always, please cite sources you use from here. Why not share what you come up with?

share|improve this answer

The most wide use is as an intermediate step during the compile process, often hidden by the outer compiler command line or gui code. The compiler produces assembly code then an assembler is called to turn that into some flavor of binary (object or an executable form). Not uncommon for the compiler command line or gui shell to call the linker so that the user only sees source code go in and an executable come out.

Not all compilers do this, tcc for example has the "machine code" hard coded in the compiler.

Having an intermediate ascii assembly language step separates the compiler development from the assembler development. Compiler writers can directly see the output of their work, they dont have to disassemble (which on some platforms is problematic) to figure out what their compiler produced. the assembler program/tool has to be there anyway, so might as well leverage it instead of re-invent it in the compiler.

bootloaders and startup and shutdown code that leads and follows compiled code often needs to be written in assembly language because hand written assembly doesnt have to conform to calling standards, where compiler generated assembly does often conform to a calling standard. Someone has to setup the stack and zero .bss memory and those sorts of tasks before the compiled code can operate using its calling convention.

Sometimes assembly is used for hand optimizing something the compiler and or language was not able to not produce fast enough code for the problem being solved.

Microcontrollers, both because some use cpus that do not have enough of a market to produce a great compiler, and because they are very resource limited to the point that having to double or triple the clock speed or amount of ram/rom to use compiled code is not worth it. You will find a larger percentage of this market uses asm compared to normal applications and operating systems.

Hopefully learning for educational purposes is a reason to use asm. Once you learn even one, esp if you learn a few different instruction sets, you both gain an appreciation for what the compiler and language is doing, and often as a result of this education you write better (performance and reliability) high level language code.

share|improve this answer

Since you said "the" language and "this" language I wanted to make sure you understood that Assembly is not a single language. It has similar structure, but is different for every different kind of processor. That's why it's most often used to access features specific to a processor.

Assembly is pretty much directly translated into machine code, which is the ones and zeroes that the processor directly operates on. MOV A, 47 on a hypothetical processor might translate into 010110 01 00101111 in machine code, where 010110 means MOV, 01 means the A register, and 00101111 is the value to move. This one-to-one correspondence makes it very easy to "disassemble," or translate machine code back into assembly code.

It's the only code you can't hide from its users, which makes knowing it a necessity for nefarious purposes like cracking software or exploiting vulnerabilities, or defending against such attacks. Because one high-level statement might result in dozens of assembly instructions, it's also handy to be able to read assembly during debugging, as it can help narrow down the problem.

However, its most common modern usage is as a smug ego boost when you want to look down upon ignorant colleagues :-)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.