Documente Academic
Documente Profesional
Documente Cultură
See the terminology section below for information regarding inconsistent use of the terms assembly and assembler.
1 Key concepts
1.1 Assembler
An assembler is a program which creates object code
by translating combinations of mnemonics and syntax
for operations and addressing modes into their numerical equivalents. This representation typically includes an
operation code ("opcode") as well as other control bits.[1]
The assembler also calculates constant expressions and
resolves symbolic names for memory locations and other
entities.[2] The use of symbolic references is a key feature
of assemblers, saving tedious calculations and manual address updates after program modications. Most assemblers also include macro facilities for performing textual
substitution e.g., to generate common short sequences
of instructions as inline, instead of called subroutines.
Some assemblers may also be able to perform some simple types of instruction set-specic optimizations. One
concrete example of this may be the ubiquitous x86 assemblers from various vendors. Most of them are able
to perform jump-instruction replacements (long jumps
replaced by short or relative jumps) in any number of
passes, on request. Others may even do simple rearrangement or insertion of instructions, such as some assemblers
for RISC architectures that can help optimize a sensible
instruction scheduling to exploit the CPU pipeline as efciently as possible.
An assembly language is a low-level programming language for a computer, or other programmable device, in
which there is a very strong (generally one-to-one) correspondence between the language and the architectures
machine code instructions. Each assembly language is
specic to a particular computer architecture, in contrast to most high-level programming languages, which
are generally portable across multiple architectures, but Like early programming languages such as Fortran,
require interpreting or compiling.
Algol, Cobol and Lisp, assemblers have been available
Assembly language is converted into executable machine since the 1950s and the rst generations of text based
code by a utility program referred to as an assembler; the computer interfaces. However, assemblers came rst as
conversion process is referred to as assembly, or assem- they are far simpler to write than compilers for high-level
languages. This is because each mnemonic along with the
bling the code.
1
1 KEY CONCEPTS
addressing modes and operands of an instruction translates rather directly into the numeric representations of
that particular instruction, without much context or analysis. There have also been several classes of translators and semi automatic code generators with properties
similar to both assembly and high level languages, with
speedcode as perhaps one of the better known examples.
There may be several assemblers with dierent syntax
for a particular CPU or instruction set architecture. For
instance, an instruction to add memory data to a register in a x86-family processor might be add eax,[ebx], in
original Intel syntax, whereas this would be written addl
(%ebx),%eax in the AT&T syntax used by the GNU Assembler. Despite dierent appearances, dierent syntactic forms generally generate the same numeric machine
code, see further below. A single assembler may also
have dierent modes in order to support variations in syntactic forms as well as their exact semantic interpretations
(such as FASM-syntax, TASM-syntax, ideal mode etc., in
the special case of x86 assembly programming).
1.1.1
Number of passes
the program load if the assembler directly produces executable code) faster.[3]
1.1.2 High-level assemblers
More sophisticated high-level assemblers provide language abstractions such as:
Advanced control structures
High-level procedure/function declarations and invocations
High-level abstract data types, including structures/records, unions, classes, and sets
Sophisticated macro processing (although available
on ordinary assemblers since the late 1950s for
IBM 700 series and since the 1960s for IBM/360,
amongst other machines)
Object-oriented programming features such as
classes, objects, abstraction, polymorphism, and
inheritance[4]
3
the semicolon. This is much easier to read and to remem- so dier; the corresponding assembly languages reect
ber.
these dierences.
MOV AL, 61h ; Load AL with 97 decimal (61 hex)
Opcode mnemonics
Data denitions
Assembly directives
2.1.1 Opcode mnemonics and extended mnemonics
Instructions (statements) in assembly language are generally very simple, unlike those in high-level languages.
Generally, a mnemonic is a symbolic name for a single executable machine language instruction (an opcode),
and there is at least one opcode mnemonic dened for
each machine language instruction. Each instruction typically consists of an operation or opcode plus zero or more
operands. Most instructions refer to a single value, or a
pair of values. Operands can be immediate (value coded
in the instruction itself), registers specied in the instruction or implied, or the addresses of data located elsewhere
in storage. This is determined by the underlying processor architecture: the assembler merely reects how this
architecture works. Extended mnemonics are often used
to specify a combination of an opcode with a specic
operand, e.g., the System/360 assemblers use B as an extended mnemonic for BC with a mask of 15 and NOP
(NO OPeration do nothing for one step) for BC with
a mask of 0.
2 LANGUAGE DESIGN
xchg ax,ax instruction as nop. Similarly, IBM assemblers for System/360 and System/370 use the extended
mnemonics NOP and NOPR for BC and BCR with zero
masks. For the SPARC architecture, these are known as
synthetic instructions.[8]
2.2 Macros
2.1.2
Data directives
Assembly directives
Many assemblers support predened macros, and others support programmer-dened (and repeatedly redenable) macros involving sequences of text lines in
which variables and constants are embedded. This sequence of text lines may include opcodes or directives.
Once a macro has been dened its name may be used
in place of a mnemonic. When the assembler processes
such a statement, it replaces the statement with the text
lines associated with that macro, then processes them
as if they existed in the source code le (including, in
some assemblers, expansion of any macros existing in
the replacement text). Macros in this sense date to IBM
autocoders of the 1950s.
This denition of macro is slightly dierent from the
use of the term in other contexts, like the C programming
language. C macros created through the #dene directive are typically just one line, or a few lines at most. Assembler macro instructions can be lengthy programs by
themselves, executed by interpretation by the assembler
during assembly.
2.3
organization using assembly language that has been heavily extended using such a macro suite can be considered
to be working in a higher-level language, since such programmers are not working with a computers lowest-level
conceptual elements.
Macros were used to customize large scale software systems for specic customers in the mainframe era and
were also used by customer personnel to satisfy their employers needs by making specic versions of manufacturer operating systems. This was done, for example,
by systems programmers working with IBM's Conversational Monitor System / Virtual Machine (VM/CMS)
and with IBMs real time transaction processing addons, Customer Information Control System CICS, and
ACP/TPF, the airline/nancial system that began in the A curious design was A-natural, a stream-oriented as1970s and still runs many large computer reservations sembler for 8080/Z80 processors from Whitesmiths Ltd.
systems (CRS) and credit card systems today.
(developers of the Unix-like Idris operating system, and
what
was reported to be the rst commercial C compiler).
It was also possible to use solely the macro processing
The
language
was classied as an assembler, because
abilities of an assembler to generate code written in comit
worked
with
raw machine elements such as opcodes,
pletely dierent languages, for example, to generate a
registers,
and
memory
references; but it incorporated an
version of a program in COBOL using a pure macro asexpression
syntax
to
indicate
execution order. Parenthesembler program containing lines of COBOL code inside
ses
and
other
special
symbols,
along with block-oriented
assembly time operators instructing the assembler to genstructured
programming
constructs,
controlled the seerate arbitrary code.
quence of the generated instructions. A-natural was built
This was because, as was realized in the 1960s, the con- as the object language of a C compiler, rather than for
cept of macro processing is independent of the concept hand-coding, but its logical syntax won some fans.
of assembly, the former being in modern terms more
word processing, text processing, than generating object There has been little apparent demand for more sophistidecline of large-scale assemcode. The concept of macro processing appeared, and cated assemblers since the[12]
bly
language
development.
In spite of that, they are still
appears, in the C programming language, which supports
being
developed
and
applied
in
cases where resource conpreprocessor instructions to set variables, and make
straints
or
peculiarities
in
the
target
systems architecture
conditional tests on their values. Note that unlike cerprevent
the
eective
use
of
higher-level
languages.[13]
tain previous macro processors inside assemblers, the C
preprocessor was not Turing-complete because it lacked
the ability to either loop or go to, the latter allowing
programs to loop.
3 Use of assembly language
Despite the power of macro processing, it fell into disuse in many high level languages (major exceptions being 3.1 Historical perspective
C/C++ and PL/I) while remaining a perennial for assemblers.
Assembly languages date to the introduction of the
Macro parameter substitution is strictly by name: at stored-program computer. The Electronic Delay Storage
macro processing time, the value of a parameter is tex- Automatic Calculator (EDSAC) (1949) had an assembler
tually substituted for its name. The most famous class of called initial orders featuring one-letter mnemonics.[14]
bugs resulting was the use of a parameter that itself was an Nathaniel Rochester wrote an assembler for an IBM 701
expression and not a simple name when the macro writer (1954). SOAP (Symbolic Optimal Assembly Program)
expected a name. In the macro: foo: macro a load a*b the (1955) was an assembly language for the IBM 650 comintention was that the caller would provide the name of a puter written by Stan Poley.[15]
variable, and the global variable or constant b would be
Assembly languages eliminated much of the errorused to multiply a. If foo is called with the parameter
prone and time-consuming rst-generation programming
a-c, the macro expansion of load a-c*b occurs. To avoid
needed with the earliest computers, freeing programmers
any possible ambiguity, users of macro processors can
from tedium such as remembering numeric codes and calparenthesize formal parameters inside macro denitions,
culating addresses. They were once widely used for all
or callers can parenthesize the input parameters.[10]
sorts of programming. However, by the 1980s (1990s on
microcomputers), their use had largely been supplanted
by higher-level languages, in the search for improved
programming productivity. Today assembly language is
6
still used for direct hardware manipulation, access to specialized processor instructions, or to address critical performance issues. Typical uses are device drivers, lowlevel embedded systems, and real-time systems.
Historically, numerous programs have been written entirely in assembly language. Operating systems were entirely written in assembly language until the introduction of the Burroughs MCP (1961), which was written
in Executive Systems Problem Oriented Language (ESPOL), an Algol dialect. Many commercial applications
were written in assembly language as well, including a
large amount of the IBM mainframe software written by
large corporations. COBOL, FORTRAN and some PL/I
eventually displaced much of this work, although a number of large organizations retained assembly-language application infrastructures well into the 1990s.
Most early microcomputers relied on hand-coded assembly language, including most operating systems and large
applications. This was because these systems had severe
resource constraints, imposed idiosyncratic memory and
display architectures, and provided limited, buggy system services. Perhaps more important was the lack of
rst-class high-level language compilers suitable for microcomputer use. A psychological factor may have also
played a role: the rst generation of microcomputer pro- There are some situations in which developers might
grammers retained a hobbyist, wires and pliers attitude. choose to use assembly language:
In a more commercial context, the biggest reasons for us A stand-alone executable of compact size is required
ing assembly language were minimal bloat (size), minithat must execute without recourse to the run-time
mal overhead, greater speed, and reliability.
components or libraries associated with a high-level
Typical examples of large assembly language programs
language; this is perhaps the most common situafrom this time are IBM PC DOS operating systems and
tion. For example, rmware for telephones, autoearly applications such as the spreadsheet program Lotus
mobile fuel and ignition systems, air-conditioning
1-2-3. Even into the 1990s, most console video games
control systems, security systems, and sensors.
were written in assembly, including most games for the
Mega Drive/Genesis and the Super Nintendo Entertain Code that must interact directly with the hardware,
ment System. According to some industry insiders, the
for example in device drivers and interrupt handlers.
assembly language was the best computer language to use
Programs that need to use processor-specic into get the best performance out of the Sega Saturn, a constructions not implemented in a compiler. A comsole that was notoriously challenging to develop and pro[16]
mon example is the bitwise rotation instruction at
gram games for.
The arcade game NBA Jam (1993)
the core of many encryption algorithms.
is another example.
Assembly language has long been the primary development language for many popular home computers of the
1980s and 1990s (such as the Sinclair ZX Spectrum,
Commodore 64, Commodore Amiga, and Atari ST). This
was in large part because interpreted BASIC dialects on
these systems oered insucient execution speed, as
well as insucient facilities to take full advantage of the
available hardware on these systems. Some systems even
have an integrated development environment (IDE) with
highly advanced debugging and macro facilities.
The Assembler for the VIC-20 was written by Don French
and published by French Silk. At 1,639 bytes in length,
its author believes it is the smallest symbolic assembler
ever written. The assembler supported the usual symbolic
addressing and the denition of character strings or hex
Programs that create vectorized functions for programs in higher-level languages such as C. In the
higher-level language this is sometimes aided by
compiler intrinsic functions which map directly to
SIMD mnemonics, but nevertheless result in a oneto-one assembly conversion specic for the given
vector processor.
Programs requiring extreme optimization, for example an inner loop in a processor-intensive algorithm. Game programmers take advantage of the
abilities of hardware features in systems, enabling
games to run faster. Also large scientic simulations
require highly optimized algorithms, e.g. linear algebra with BLAS[19][25] or discrete cosine transformation (e.g. SIMD assembly version from x264[26] )
3.3
Typical applications
7
programmers today regularly work with assembly language as a tool, the underlying concepts remain very important. Such fundamental topics as binary arithmetic,
memory allocation, stack processing, character set encoding, interrupt processing, and compiler design would be
hard to study in detail without a grasp of how a computer
operates at the hardware level. Since a computers behavior is fundamentally dened by its instruction set, the
logical way to learn such concepts is to study an assembly
language. Most modern computers have similar instruction sets. Therefore, studying a single assembly language
is sucient to learn: I) the basic concepts; II) to recognize
situations where the use of assembly language might be
appropriate; and III) to see how ecient executable code
can be created from high-level languages. [28] This is analogous to children needing to learn the basic arithmetic
operations (e.g., long division), although calculators are
widely used for all except the most trivial calculations.
Related terminology
Assembly language or assembler language is
commonly called assembly, assembler, ASM, or
symbolic machine code. A generation of IBM
mainframe programmers called it ALC for Assembly Language Code or BAL[29] for Basic Assembly
Language. Calling the language assembler might
be considered potentially confusing and ambiguous,
since this is also the name of the utility program that
translates assembly language statements into machine code. However, this usage has been common among professionals and in the literature for
decades.[30] Similarly, some early computers called
their assembler their assembly program.[31]
6 Further details
For any given personal computer, mainframe, embedded
system, and game console, both past and present, at least
one possibly dozens of assemblers have been written.
For some examples, see the list of assemblers.
9
1. ;-----------------------------------------------------------
8 See also
2. ; zstr_count:
Compiler
Disassembler
6.
Microassembler
Comparison of assemblers
9 References
[3] Beck, Leland L. (1996). 2. System Software: An Introduction to Systems Programming. Addison Wesley.
21.
The rst column (from the left) is simply the line number
in the listing and is otherwise meaningless. The second
[10] Macros (C/C++), MSDN Library for Visual Studio
column is the relative address, in hex, of where the code
2008. Microsoft Corp. Retrieved 2010-06-22.
will be placed in memory. The third column is the actual
compiled code. For instance, B9 is the x86 opcode for [11] Concept 14 Macros. MVS Software. Retrieved May
25, 2009.
the MOV ECX instruction; FFFFFFFF is the value 1 in
twos-complement binary form.
[12] Answers.com. assembly language: Denition and Much
10
11
git.videolan.org.
EXTERNAL LINKS
10 Further reading
Yurichev, Dennis, An Introduction To Reverse
Engineering for Beginners. Online book: http:
//yurichev.com/writings/RE_for_beginners-en.pdf
ASM Community Book An online book full of helpful ASM info, tutorials and code examples by the
ASM Community
Jonathan Bartlett: Programming from the Ground
Up. Bartlett Publishing, 2004. ISBN 0-97528384-7
Also available online as PDF
Robert Britton: MIPS Assembly Language Programming. Prentice Hall, 2003. ISBN 0-13-142044-5
Paul Carter: PC Assembly Language. Free ebook,
2001.
Website
Je Duntemann: Assembly Language Step-by-Step.
Wiley, 2000. ISBN 0-471-37523-3
Randall Hyde: The Art of Assembly Language. No
Starch Press, 2003. ISBN 1-886411-97-2
Draft versions available online as PDF and HTML
Peter Norton, John Socha, Peter Nortons Assembly
Language Book for the IBM PC, Brady Books, NY:
1986.
Michael Singer, PDP-11. Assembler Language Programming and Machine Organization, John Wiley &
Sons, NY: 1980.
[27] 68K Programming in Fargo II. Archived from the original on 2 July 2008. Retrieved 2008-07-03.
Dominic Sweetman: See MIPS Run. Morgan Kaufmann Publishers, 1999. ISBN 1-55860-410-3
John Waldron: Introduction to RISC Assembly Language Programming. Addison Wesley, 1998. ISBN
0-201-39828-1
[29] Technically BAL was only the assembler for BPS; the others were macro assemblers.
[30] Stroustrup, Bjarne (1986). The C++ Programming Language. Addison-Wesley. ISBN 0-201-12078-X. C++ was
primarily designed so that the author and his friends would
not have to program in assembler, C, or various modern
high-level languages. [use of the term assembler to mean
assembly language]
[31] Saxon, James; Plette, William (1962). Programming the
IBM 1401. Prentice-Hall. LoC 62-20615. [use of the
term assembly program]
[32] Microsoft Corporation. MASM: Directives & PseudoOpcodes. Retrieved March 19, 2011.
[33] (John Daintith, ed.) A Dictionary of Computing: metaassembler
[34] Randall Hyde. Which Assembler is the Best?". Archived
from the original on 18 October 2007. Retrieved 200710-19.
11 External links
Unix Assembly Language Programming
Linux Assembly
PPR: Learning Assembly Language
NASM The Netwide Assembler (a popular assembly language)
Assembly Language Programming Examples
Authoring Windows Applications In Assembly Language
Iczelions Win32 Assembly Tutorial at the Wayback
Machine
Assembly Optimization Tips by Mark Larson
11
12
12.1
12.2
Images
12
12
12.3
Content license