Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Assembly language
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Language design== ===Basic elements=== There is a large degree of diversity in the way the authors of assemblers categorize statements and in the nomenclature that they use. In particular, some describe anything other than a machine mnemonic or extended mnemonic as a pseudo-operation (pseudo-op). A typical assembly language consists of 3 types of instruction statements that are used to define program operations: * [[Opcode]] mnemonics * Data definitions * Assembly directives ===={{anchor|Mnemonics}}Opcode mnemonics and extended mnemonics==== Instructions (statements) in assembly language are generally very simple, unlike those in [[high-level programming language|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 defined for each machine language instruction. Each instruction typically consists of an ''operation'' or ''opcode'' plus zero or more ''[[operand]]s''. Most instructions refer to a single value or a pair of values. Operands can be immediate (value coded in the instruction itself), registers specified 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 reflects how this architecture works. ''Extended mnemonics'' are often used to specify a combination of an opcode with a specific operand, e.g., the System/360 assemblers use {{code|B}} as an extended mnemonic for {{code|BC}} with a mask of 15 and {{code|NOP}} ("NO OPeration" β do nothing for one step) for {{code|BC}} with a mask of 0. ''Extended mnemonics'' are often used to support specialized uses of instructions, often for purposes not obvious from the instruction name. For example, many CPU's do not have an explicit NOP instruction, but do have instructions that can be used for the purpose. In 8086 CPUs the instruction {{code|2=asm|xchg ax,ax}} is used for {{code|nop}}, with {{code|nop}} being a pseudo-opcode to encode the instruction {{code|2=asm|xchg ax,ax}}. Some disassemblers recognize this and will decode the {{code|2=asm|xchg ax,ax}} instruction as {{code|nop}}. Similarly, IBM assemblers for [[IBM System/360|System/360]] and [[IBM System/370|System/370]] use the extended mnemonics {{code|NOP}} and {{code|NOPR}} for {{code|BC}} and {{code|BCR}} with zero masks. For the SPARC architecture, these are known as ''synthetic instructions''.<ref name="SPARC_1992"/> Some assemblers also support simple built-in macro-instructions that generate two or more machine instructions. For instance, with some Z80 assemblers the instruction {{code|ld hl,bc}} is recognized to generate {{code|ld l,c}} followed by {{code|ld h,b}}.<ref name="Moxham_1996"/> These are sometimes known as ''pseudo-opcodes''. Mnemonics are arbitrary symbols; in 1985 the [[Institute of Electrical and Electronics Engineers|IEEE]] published Standard 694 for a uniform set of mnemonics to be used by all assemblers.<ref>{{cite book |title=IEEE Std 694-1985: IEEE Standard for Microprocessor Assembly Language |publisher=IEEE Computer Society |date=1985 |isbn=0-7381-2752-3 |oclc=1415906564 }}</ref> The standard has since been withdrawn. ====Data directives==== There are instructions used to define data elements to hold data and variables. They define the type of data, the length and the [[data structure alignment|alignment]] of data. These instructions can also define whether the data is available to outside programs (programs assembled separately) or only to the program in which the data section is defined. Some assemblers classify these as pseudo-ops. ====Assembly directives==== Assembly directives, also called pseudo-opcodes, pseudo-operations or pseudo-ops, are commands given to an assembler "directing it to perform operations other than assembling instructions".<ref name="Salomon_1992"/> Directives affect how the assembler operates and "may affect the object code, the symbol table, the listing file, and the values of internal assembler parameters". Sometimes the term ''pseudo-opcode'' is reserved for directives that generate object code, such as those that generate data.<ref name="Hyde_MASM"/> The names of pseudo-ops often start with a dot to distinguish them from machine instructions. Pseudo-ops can make the assembly of the program dependent on parameters input by a programmer, so that one program can be assembled in different ways, perhaps for different applications. Or, a pseudo-op can be used to manipulate presentation of a program to make it easier to read and maintain. Another common use of pseudo-ops is to reserve storage areas for run-time data and optionally initialize their contents to known values. Symbolic assemblers let programmers associate arbitrary names (''[[label (computer science)|label]]s'' or ''symbols'') with memory locations and various constants. Usually, every constant and variable is given a name so instructions can reference those locations by name, thus promoting [[self-documenting code]]. In executable code, the name of each subroutine is associated with its entry point, so any calls to a subroutine can use its name. Inside subroutines, [[GOTO]] destinations are given labels. Some assemblers support ''local symbols'' which are often lexically distinct from normal symbols (e.g., the use of "10$" as a GOTO destination). Some assemblers, such as [[Netwide Assembler|NASM]], provide flexible symbol management, letting programmers manage different [[namespace]]s, automatically calculate offsets within [[data structure]]s, and assign labels that refer to literal values or the result of simple computations performed by the assembler. Labels can also be used to initialize constants and variables with relocatable addresses. Assembly languages, like most other computer languages, allow comments to be added to program [[source code]] that will be ignored during assembly. Judicious commenting is essential in assembly language programs, as the meaning and purpose of a sequence of binary machine instructions can be difficult to determine. The "raw" (uncommented) assembly language generated by compilers or disassemblers is quite difficult to read when changes must be made. ===Macros=== Many assemblers support ''predefined macros'', and others support ''programmer-defined'' (and repeatedly re-definable) macros involving sequences of text lines in which variables and constants are embedded. The macro definition is most commonly<ref group="nb" name="NB4"/> a mixture of assembler statements, e.g., directives, symbolic machine instructions, and templates for assembler statements. This sequence of text lines may include opcodes or directives. Once a macro has been defined 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 file (including, in some assemblers, expansion of any macros existing in the replacement text). Macros in this sense date to IBM [[autocoder]]s of the 1950s.<ref name="Autocoder"/> '''Macro assemblers''' typically have directives to, e.g., define macros, define variables, set variables to the result of an arithmetic, logical or string expression, iterate, conditionally generate code. Some of those directives may be restricted to use within a macro definition, e.g., '''MEXIT''' in [[HLASM]], while others may be permitted within open code (outside macro definitions), e.g., '''AIF''' and '''COPY''' in HLASM. In assembly language, the term "macro" represents a more comprehensive concept than it does in some other contexts, such as the [[pre-processor]] in the [[C (programming language)|C programming language]], where its #define directive typically is used to create short single line macros. Assembler macro instructions, like macros in [[PL/I]] and some other languages, can be lengthy "programs" by themselves, executed by interpretation by the assembler during assembly. Since macros can have 'short' names but expand to several or indeed many lines of code, they can be used to make assembly language programs appear to be far shorter, requiring fewer lines of source code, as with higher level languages. They can also be used to add higher levels of structure to assembly programs, optionally introduce embedded debugging code via parameters and other similar features. Macro assemblers often allow macros to take [[parameter (computer programming)|parameter]]s. Some assemblers include quite sophisticated macro languages, incorporating such high-level language elements as optional parameters, symbolic variables, conditionals, string manipulation, and arithmetic operations, all usable during the execution of a given macro, and allowing macros to save context or exchange information. Thus a macro might generate numerous assembly language instructions or data definitions, based on the macro arguments. This could be used to generate record-style data structures or "[[loop unrolling|unrolled]]" loops, for example, or could generate entire algorithms based on complex parameters. For instance, a "sort" macro could accept the specification of a complex sort key and generate code crafted for that specific key, not needing the run-time tests that would be required for a general procedure interpreting the specification. An 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 computer's lowest-level conceptual elements. Underlining this point, macros were used to implement an early [[virtual machine]] in [[SNOBOL4]] (1967), which was written in the SNOBOL Implementation Language (SIL), an assembly language for a virtual machine. The target machine would translate this to its native code using a [[macro assembler]].<ref name="Griswold_1972"/> This allowed a high degree of portability for the time. Macros were used to customize large scale software systems for specific customers in the mainframe era and were also used by customer personnel to satisfy their employers' needs by making specific versions of manufacturer operating systems. This was done, for example, by systems programmers working with [[IBM]]'s Conversational Monitor System / Virtual Machine ([[VM (operating system)|VM/CMS]]) and with IBM's "real time transaction processing" add-ons, Customer Information Control System [[CICS]], and [[IBM Airline Control Program|ACP]]/[[Transaction Processing Facility|TPF]], the airline/financial system that began in the 1970s and still runs many large [[computer reservation system]]s (CRS) and credit card systems today. It is also possible to use solely the macro processing abilities of an assembler to generate code written in completely different languages, for example, to generate a version of a program in [[COBOL]] using a pure macro assembler program containing lines of COBOL code inside assembly time operators instructing the assembler to generate arbitrary code. IBM [[OS/360 and successors|OS/360]] uses macros to perform [[system generation]]. The user specifies options by coding a series of assembler macros. Assembling these macros generates a [[job stream]] to build the system, including [[job control language]] and [[Support programs for OS/360 and successors|utility]] control statements. This is because, as was realized in the 1960s, the concept of "macro processing" is independent of the concept of "assembly", the former being in modern terms more word processing, text processing, than generating object code. The concept of macro processing appeared, and appears, in the C programming language, which supports "preprocessor instructions" to set variables, and make conditional tests on their values. Unlike certain previous macro processors inside assemblers, the C preprocessor is not [[Turing completeness|Turing-complete]] because it lacks the ability to either loop or "go to", the latter allowing programs to loop. Despite the power of macro processing, it fell into disuse in many high level languages (major exceptions being [[C (programming language)|C]], [[C++]] and PL/I) while remaining a perennial for assemblers. Macro parameter substitution is strictly by name: at macro processing time, the value of a parameter is textually substituted for its name. The most famous class of bugs resulting was the use of a parameter that itself was an expression and not a simple name when the macro writer expected a name. In the macro: foo: macro a load a*b the intention was that the caller would provide the name of a variable, and the "global" variable or constant b would be used to multiply "a". If foo is called with the parameter <code>a-c</code>, the macro expansion of <code>load a-c*b</code> occurs. To avoid any possible ambiguity, users of macro processors can parenthesize formal parameters inside macro definitions, or callers can parenthesize the input parameters.<ref name="Microsoft_2012_Macros"/> ===Support for structured programming=== <!-- With rare exceptions, macros are not part of the assembler but reside in the source code or in macro libraries. --> Packages of macros have been written providing [[structured programming]] elements to encode execution flow. The earliest example of this approach was in the [[Concept-14 macro set]],<ref name="Kessler_1970"/> originally proposed by [[Harlan Mills]] (March 1970), and implemented by Marvin Kessler at IBM's Federal Systems Division, which provided IF/ELSE/ENDIF and similar control flow blocks for OS/360 assembler programs. This was a way to reduce or eliminate the use of [[GOTO]] operations in assembly code, one of the main factors causing [[spaghetti code]] in assembly language. This approach was widely accepted in the early 1980s (the latter days of large-scale assembly language use). IBM's High Level Assembler Toolkit<ref>{{cite web | title = High Level Assembler Toolkit Feature Increases Programmer Productivity | id = A95-1432 | date = December 12, 1995 | url = https://www.ibm.com/common/ssi/ShowDoc.wss?docURL=/common/ssi/rep_ca/2/649/ENUSA95-1432/index.html&request_locale=en | work = Announcement Letters | publisher = [[IBM]] | archive-url = https://web.archive.org/web/20230307045942/https://www.ibm.com/common/ssi/ShowDoc.wss?docURL=/common/ssi/rep_ca/2/649/ENUSA95-1432/index.html&request_locale=en | archive-date = March 7, 2023 | url-status = dead }} </ref> includes such a macro package. Another design was [[A-Natural]],<ref>{{Cite book |last=Whitesmiths Ltd |url=http://archive.org/details/a-natural-manual |title=A-Natural Language Reference Manual |date=1980-07-15}}</ref> a "stream-oriented" assembler for 8080/[[Zilog Z80|Z80]] processors from [[Whitesmiths|Whitesmiths Ltd.]] (developers of the [[Unix]]-like [[Idris (operating system)|Idris]] operating system, and what was reported to be the first commercial [[C (programming language)|C]] [[compiler]]). The language was classified as an assembler because it worked with raw machine elements such as [[opcode]]s, [[processor register|registers]], and memory references; but it incorporated an expression syntax to indicate execution order. Parentheses and other special symbols, along with block-oriented structured programming constructs, controlled the sequence of the generated instructions. A-natural was built as the object language of a C compiler, rather than for hand-coding, but its logical syntax won some fans. There has been little apparent demand for more sophisticated assemblers since the decline of large-scale assembly language development.<ref name="assembly-language?cat=technology"/> In spite of that, they are still being developed and applied in cases where resource constraints or peculiarities in the target system's architecture prevent the effective use of higher-level languages.<ref name="Provinciano_2005"/> Assemblers with a strong macro engine allow structured programming via macros, such as the switch macro provided with the Masm32 package (this code is a complete program): <syntaxhighlight lang="nasm"> include \masm32\include\masm32rt.inc ; use the Masm32 library .code demomain: REPEAT 20 switch rv(nrandom, 9) ; generate a number between 0 and 8 mov ecx, 7 case 0 print "case 0" case ecx ; in contrast to most other programming languages, print "case 7" ; the Masm32 switch allows "variable cases" case 1 .. 3 .if eax==1 print "case 1" .elseif eax==2 print "case 2" .else print "cases 1 to 3: other" .endif case 4, 6, 8 print "cases 4, 6 or 8" default mov ebx, 19 ; print 20 stars .Repeat print "*" dec ebx .Until Sign? ; loop until the sign flag is set endsw print chr$(13, 10) ENDM exit end demomain </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)