Assembly Language Syntax
This chapter describes the basic lexical elements of assembly language programming, and explains how those elements combine to form complete assembly language expressions.
This chapter goes on to explain how sequences of expressions are put together to form the statements that make up an assembly language program.
Elements of Assembly Language
This section describes the basic building blocks of an assembly language program—these are characters, symbols, labels, and constants.
Characters
The following characters are used in assembly language programs:
Alphanumeric characters—A through Z, a through z, and 0 through 9
Other printable ASCII characters (such as
#
, $, :, ., +, -, *, /, !, and |)Nonprinting ASCII characters (such as space, tab, return, and newline)
Some of these characters have special meanings, which are described in Expression Syntax and in Assembly Language Statements.
Identifiers
An identifier (also known as a symbol) can be used for several purposes:
As the label for an assembler statement (see Labels)
As a location tag for data
As the symbolic name of a constant
Each identifier consists of a sequence of alphanumeric characters (which may include other printable ASCII characters such as .
, _
, and $
). The first character must not be numeric. Identifiers may be of any length, and all characters are significant. The case of letters is significant—for example, the identifier var
is different from the identifier Var
.
It is also possible to define an identifier by enclosing multiple identifiers within a pair of double quotation marks. For example:
"Object +new:": |
.long "Object +new:" |
Labels
A label is written as an identifier immediately followed by a colon (:
). The label represents the current value of the current location counter; it can be used in assembler instructions as an operand.
Numeric Labels
Local numeric labels allow compilers and programmers to use names temporarily. A numeric label consists of a digit (between 0 and 9) followed by a colon. These 10 local symbol names can be reused any number of times throughout the program. As with alphanumeric labels, a numeric label assigns the current value of the location counter to the symbol.
Although multiple numeric labels with the same digit may be used within the same program, only the next definition and the most recent previous definition of a label can be referenced:
To refer to the most recent previous definition of a local numeric label, write digit
b
, (using the same digit as when you defined the label).To refer to the next definition of a numeric label, write digit
f
.
The Scope of a Label
The scope of a label is the distance over which it is visible to (and referenceable by) other parts of the program. Normally, a label that tags a location or data is visible only within the current assembly unit.
The .globl
directive (described in .globl) may be used to make a label external. In this case, the symbol is visible to other assembly units at link time.
Constants
Four types of constants are available: Numeric, character, string, and floating point. All constants are interpreted as absolute quantities when they appear in an expression.
Numeric Constants
A numeric constant is a token that starts with a digit. Numeric constants can be decimal, hexadecimal, or octal. The following restrictions apply:
Decimal constants contain only digits between 0 and 9, and normally aren’t longer than 32 bits—having a value between
-2,147,483,648
and2,147,483,647
(values that don’t fit in 32 bits are bignums, which are legal but which should fit within the designated format). Decimal constants cannot contain leading zeros or commas.Hexadecimal constants start with
0x
(or0X
), followed by between one and eight decimal or hexadecimal digits (0
through9
,a
throughf
, andA
throughF
). Values that don’t fit in 32 bits are bignums.Octal constants start with
0
, followed by from one to eleven octal digits (0 through 7). Values that don’t fit in 32 bits are bignums.
Character Constants
A single-character constant consists of a single quotation mark ('
) followed by any ASCII character. The constant’s value is the code for the given character.
String Constants
A string constant is a sequence of zero or more ASCII characters surrounded by quotation marks (for example, "a string"
).
Floating-Point Constants
The general lexical form of a floating-point number is:
0flt_char[{+–}]dec...[.][dec...][exp_char[{+–}][dec...]] |
where:
Item | Description |
---|---|
flt_char | A required type specification character (see the following table). |
[{+-}] | The optional occurrence of either |
dec... | A required sequence of one or more decimal digits. |
[.] | A single optional period. |
[dec...] | An optional sequence of one or more decimal digits. |
[exp_char] | An optional exponent delimiter character (see the following table). |
The type specification character, flt_char, specifies the type and representation of the constructed number; the set of legal type specification characters with the processor architecture, as shown here:
Architecture | flt_char | exp_char |
---|---|---|
ppc | {dDfF} | {eE} |
i386 | {fFdDxX} | {eE} |
When floating-point constants are used as arguments to the .single
and .double
directives, the type specification character isn’t actually used in determining the type of the number. For convenience, r
or R
can be used consistently to specify all types of floating-point numbers.
Collectively, all floating-point numbers, together with quad and octal scalars, are called bignums. When as
requires a bignum, a 32-bit scalar quantity may also be used.
Floating-point constants are internally represented as flonums in a machine-independent, precision-independent floating-point format (for accurate cross-assembly).
Assembly Location Counter
A single period (.
), usually referred to as “dot,” is used to represent the current location counter. There is no way to explicitly reference any other location counters besides the current location counter.
Even if it occurs in the operand field of a statement, dot refers to the address of the first byte of that statement; the value of dot isn’t updated until the next machine instruction or assembler directive.
Expression Syntax
Expressions are combinations of operand terms (which can be numeric constants or symbolic identifiers ) and operators. This section lists the available operators, and describes the rules for combining these operators with operands in order to produce legal expressions.
Operators
Identifiers and numeric constants can be combined, through the use of operators, to form expressions. Each operator operates on 64-bit values. If the value of a term occupies less than 64 bits, it is sign-extended to a 64-bit value.
The assembler provides both unary and binary operators. A unary operator precedes its operand; a binary operator follows its first operand, and precedes its second operand. For example:
!var | unary expression |
var+5 | binary expression |
The assembler recognizes the following unary operators:
Operator | Description |
---|---|
| Unary minus: The result is the two’s complement of the operand. |
| One’s complement: The result is the one’s complement of the operand. |
| Logical negation: The result is zero if the operand is nonzero, and |
The assembler recognizes the following binary operators:
Operator | Description |
---|---|
| Addition: The result is the arithmetic addition of the two operands. |
| Subtraction: The result is the arithmetic subtraction of the two operands. |
| Multiplication: The result is the arithmetic multiplication of the two operands. |
| Division: The result is the arithmetic division of the two operands; this is integer division, which truncates towards zero. |
| Modulus: The result is the remainder that’s produced when the first operand is divided by the second (this operator applies only to integral operands). |
| Right shift: The result is the value of the first operand shifted to the right, where the second operand specifies the number of bit positions by which the first operand is to be shifted (this operator applies only to integral operands). This is always an arithmetic shift since all operators operate on signed operands. |
| Left shift: The result is the value of the first operand shifted to the left, where the second operand specifies the number of bit positions by which the first operand is to be shifted (this operator applies only to integral operands). |
| Bitwise AND: The result is the bitwise AND function of the two operands (this operator applies only to integral operands). |
| Bitwise exclusive OR: The result is the bitwise exclusive OR function of the two operands (this operator applies only to integral operands). |
| Bitwise inclusive OR: The result is the bitwise inclusive OR function of the two operands (this operator applies only to integral operands). |
| Less than: The result is |
| Greater than: The result is |
| Less than or equal: The result is |
| Greater than or equal: The result is |
| Equal: The result is |
| Not equal (same as |
Terms
A term is a part of an expression; it may be:
An identifier.
A numeric constant (its 32-bit value is used). The assembly location counter (
.
), for example, is a valid numeric constant.An expression or term enclosed in parentheses. Any quantity enclosed in parentheses is evaluated before the rest of the expression. This can be used to alter the normal evaluation of expressions—for example, to differentiate between
x * y + z
andx * (y + z)
or to apply a unary operator to an entire expression—for example,–(x * y + z)
.A term preceded by a unary operator (for example,
~var
). Multiple unary operators may be used in a term (for example,!~var
).
Expressions
Expressions are combinations of terms joined together by binary operators. An expression is always evaluated to a 32-bit value, but in some situations a different value is used:
If the operand requires a 1-byte value (a
.byte
directive, for example), the low-order 8 bits of the expression are used.If the operand requires a 16-bit value (a
.short
directive or amovem
instruction, for example), the low-order 16 bits of the expression are used.
All expressions are evaluated using the same operator precedence rules that are used by the C programming language.
When an expression is evaluated, its value is absolute, relocatable, or external, as described below.
Absolute Expressions
An expression is absolute if its value is fixed. The following are examples of absolute expressions:
An expression whose terms are constants
An identifier whose value is a constant via a direct assignment statement
Values to which the
.set
directive is applied
Relocatable Expressions
An expression (or term) is relocatable if its value is fixed relative to a base address but has an offset value when it is linked or loaded into memory. For example, all labels of a program defined in relocatable sections are relocatable.
Expressions that contain relocatable terms must add or subtract only constants to their value. For example, assuming the identifiers var
and dat
are defined in a relocatable section of the program, the following examples demonstrate the use of relocatable expressions:
Expression | Description |
---|---|
| Simple relocatable term. Its value is an offset from the base address of the current control section. |
| Simple relocatable expression. Since the value of |
| Not relocatable. Multiplying a relocatable term by a constant invalidates the relocatable status of the expression. |
| Not relocatable. The expression can’t be linked by adding |
| Relocatable expression if both |
External Expressions
An expression is external (or global) if it contains an external identifier not defined in the current program. In general, the same restrictions on expressions containing relocatable identifiers apply to expressions containing external identifiers. An exception is that the expression var–dat
is incorrect when both var
and dat
are external identifiers (that is, you cannot subtract two external relocatable expressions). Also, you cannot multiply or divide any relocatable expression.
Copyright © 2003, 2009 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2009-01-07