[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

 

Chapter 2.

 

Assembly Language Syntax



This chapter describes the syntax of assembly language statements.

This chapter includes the following topics:


Assembly Language Statements Description

Three types of assembly language statements exist:

The type of the assembly language statement differs depending on whether the operation performed by the statement is a machine instruction, a macro call, or an assembler directive.

Instruction, directive, and macro names are case insensitive. For example, MOV, Mov, and mov all name the same instruction.

When creating assembly language statements, you must be aware of the following information:

The maximum length of a statement or an expanded macro is 1000 characters. A statement must reside on a single line. However, you can concatenate two or more lines by typing a backslash (\) character at the end of the line. Each line of the source file can contain only one statement unless the assembler is running in GNU mode, which allows multiple statements to reside on one line, separated by semicolons.

For information on where to find machine instructions for a particular chip, see the processor-specific chapters of this manual. For more information on assembler directives, see "Using Directives". For more information on macros, see "Using Macros".


Assembly Language Statement Syntax

Listing 2.1 shows the syntax of an assembly language statement.

Statement syntax:


statement ::= [ symbol ] operation [ operand ] [ ,operand ]... [ comment ]
operation ::= machine_instruction | assembler_directive | macro_call

operand ::= symbol | constant | expression | register_name


Table 2.1 provides information related to the syntax shown in Listing 2.1.

Syntax-related information:

 

Syntax Element
Description
symbol  
A symbol is a combination of characters that represents a value. For more information, see "Symbols".  
machine_instruction  
A machine instruction for your target processor. For information on where to find machine instructions for a particular processor, see the processor-specific chapters of this manual.  
assembler_directive  
Assembler directives are special instructions that tell the assembler how to process other assembly language statements. For example, certain assembler directives tell the assembler where the beginning and end of a macro is. For more information on assembler directives, see "Using Directives".  
macro_call  
A call to a previously specified macro. For information on macro-related assembler directives, see "Macro Directives". For more information on macros, see "Using Macros".  
constant  
A defined value such as a string of characters or a numeric value. For more information, see "Constants".  
expression  
A mathematical expression. For more information, see "Expressions".  
register_name  
The name of a register; these names are processor-specific. For information on related processor-specific documentation, see the processor-specific chapters of this manual.  
comment  
A comment is text that the assembler ignores. You can use comments to document your code. For more information, see "Comments".  


Symbols

A symbol is a group of characters that represents a value, such as an address, numeric constant, string constant, or character constant. The length of a symbol name is unlimited.

The syntax of a symbol follows:


  symbol ::= label | equate


NOTE

For the complete syntax of an assembly language statement, see Listing 2.1.


In general, a symbol has file-wide scope. File-wide scope means that you can access the symbol anywhere within the file where you defined the symbol and only within that file. However, symbols sometimes have a different scope. For more information, see "Local labels".

This section discusses the following topics:


Labels

A label is a symbol that represents an address. The assembler provides local labels and non-local labels. Whether a label is local or non-local determines its scope.

The syntax of a label follows:


  label ::= local_label [ : ] | non-local_label[ : ]


NOTE

For the complete syntax of an assembly language statement, see Listing 2.1.


By default, a label ends with a colon (:) and can begin in any column. However, if you are porting existing code that does not follow this convention, clear the Labels must end with ':' checkbox on the Assembler settings panel. After you clear the checkbox, a label must either begin in column 1 or end with a colon (:).


NOTE

For more information, see "Common Assembler Settings".


This section contains the following topics:


Non-local labels

A non-local label is a symbol that represents an address and has file-wide scope.

The first character of a non-local label must be one of the following:

The subsequent characters of a non-local label can be either a character from the preceding list or one of the following:

A numeral between zero and nine (0-9) A dollar sign ($)

Local labels

A local label is a symbol that represents an address and has local scope. Local scope means that the scope of the label extends forward and backward within the file until the point where the assembler encounters a non-local label.

The first character of a local label must be an at-sign (@). The subsequent characters of a local label must be one of the following:

NOTE: You cannot export local labels. In addition, local labels do not appear in debugging tables.


Within an expanded macro, the scope of local labels works differently:

Listing 2.2 shows the scope of local labels in macros.

The scope of local labels in a macro:


MAKEPOS   .MACRO
     cmp     eax, 1
     jne     @SKIP
     neg     eax
@SKIP:  ;Scope of this label is within the macro
    .ENDM
START:
     mov     eax, COUNT
     cmp     eax, 1
     jne     @SKIP
     MAKEPOS
@SKIP:    ;Scope of this label is START to END
          ;excluding lines arising from
          ;macro expansion
      add    eax, 1
END:  ret


In Listing 2.2, the @SKIP label defined in the macro does not conflict with the @SKIP label defined in the main body of code.


Relocatable labels

The assembler assumes a flat 32-bit memory space. You can specify the relocation of a 32-bit label with the expressions shown in Table 2.2.


NOTE

Some expressions are not allowed in all assemblers.


Relocatable label expressions:

 

This
Represents this
label
The offset from the address of the label to the base of its section, relocated by the section base address. It also is the PC-relative target of a branch or call. It is a 32-bit address.  
label@l
The low 16-bits of the relocated address of the symbol.  
label@h
The high 16-bits of the relocated address of the symbol. You can OR this with label@l to produce the full 32-bit relocated address.  
label@ha
The adjusted high 16-bits of the relocated address of the symbol. You can add this to label@l to produce the full 32-bit relocated address.  
label@sdax
For labels in a small data section, the offset from the base of the small data section to the label. This syntax is not allowed for labels in other sections.  
label@got
For chips with a global offset table, the offset from the base of the global offset table to the 32-bit entry for label.  


Equates

An equate is a symbol that represents any value. You can create an equate with a .equ or .set directive.


NOTE

For more information, see "equ" and "set".


This section contains the following topics:


Equate names

The first character of an must be one of the following:

The subsequent characters of an equate can be either a character from the preceding list or one of the following:

A numeral between zero and nine (0-9) A dollar sign ($)

Forward Equates

The assembler allows forward equates. This means that you can refer to an equate in a file before it is defined. When an assembler encounters an expression it cannot resolve because the expression references a symbol whose value is not known, the assembler retains the expression and marks it as unresolved. After the assembler reads the entire file, it reevaluates unresolved expressions and, if necessary, repeatedly reevaluates them until it resolves them all or it cannot resolve them any further. If the assembler cannot resolve an expression, it raises an error.

However, the assembler must be able to immediately resolve any expression whose value affects the location counter.


NOTE

Note that if the assembler can make a reasonable assumption about the location counter, the expression is allowed. For example, in a forward branch instruction for a 68K processor, you can specify a default assumption of 8, 16, or 32 bits.


Thus, the code in Listing 2.3 is valid.

Valid forward equate:


              .long  alloc_size
alloc_size    .set   rec_size + 4
                     ; a valid forward equate on next line
rec_size      .set   table_start-table_end
   ;...
table_start:
   ; ...
table_end:


However, the code in Listing 2.4 is not valid. The assembler cannot immediately resolve the expression in the .space directive. Consequently, the effect on the location counter is unknown.

Invalid forward equate:


                  ;invalid forward equate on next line
rec_size   .set   table_start-table_end
           .space rec_size
   ; ...
table_start:
   ; ...
table_end:



Case-sensitive identifiers

The Case-sensitive identifiers checkbox on the Assembler settings panel lets you choose whether symbols are case-sensitive. If you click the checkbox, symbols are case sensitive, so SYM1, sym1, and Sym1 are three different symbols, for example. If you clear the checkbox, symbols are not case-sensitive, so SYM1, sym1, and Sym1 are the same symbol, for example. By default, this option is on.


Constants

The assembler recognizes three kinds of constants:


Integer Constants

Table 2.3 lists the preferred notation for integer constants.

Preferred integer constant notation:

 

For numbers of this type

Use
Decimal  
A string of decimal digits, such as 12345678.  
Hexadecimal  
A dollar sign ($) followed by a string of hexadecimal digits, such as $deadbeef.  
Binary  
A percent sign (%) followed by a string of binary digits, such as %01010001.  

To help you port existing code, the assembler also supports the notation in Table 2.4.

Alternate integer constant notation:

 

For numbers of this type

Use
Hexadecimal
0x followed by a string of hexadecimal digits, such as 0xdeadbeef.  
Hexadecimal
0 followed by a string of hexadecimal digits, such as 0deadbeef, and ending with an h, such as 0deadbeefh.  
Decimal
A string of decimal digits followed by d, such as 12345678d.  
Binary
A string of binary digits followed by a b, such as 01010001b.  


NOTE

The assembler stores and manipulates integer constants using 32-bit signed arithmetic.



Floating-Point Constants

You can specify floating point constants in either hexadecimal or decimal format. A floating point constant in decimal format must contain either a decimal point or an exponent, e.g. 1E-10 or 1.0.

You can use floating point constants only in data generation directives like .float and .double, or in floating point instructions. You cannot use them in expressions.


Character Constants

Enclose a character constant in single quotes unless the character constant includes a single quote. In that case, enclose the character constant in double quotes.


NOTE

A character constant cannot include both single and double quotes.


The maximum width of a character constant is 4 characters, depending on the context. For example, the following items are character constants:

A character constant can contain any of the escape sequences shown in Table 2.5.

Escape sequences:

 

Sequence
Description
\b
Backspace  
\n
Line feed (ASCII character 10)  
\r
Return (ASCII character 13)  
\t
Tab  
\"
Double quote  
\\
Backslash  
\nnn
Octal value of \nnn  

A character constant is zero-extended to 32 bits during computation. You can use a character constant anywhere you can use an integer constant.


Expressions

The assembler evaluates expressions using 32-bit signed arithmetic and does not check for arithmetic overflow.

Since there is no common set of operators in the existing assemblers for different processors, the assembler uses an expression syntax similar to the one for the C language. Expressions use the C language arithmetic rules for such things as parentheses and associativity, and they use the same operators.


NOTE

To refer to the program counter in an expression, use a period (.), dollar sign ($), or asterisk (*).


The assembler supports the binary operators listed in Table 2.6.

Binary operators:

 

Operator
Description
+
add  
-
subtract  
*
multiply  
/
divide  
%
modulo  
||
logical OR  
&&
logical AND  
|
bitwise OR  
&
bitwise AND  
^
bitwise XOR  
<<
shift left  
>>
shift right (zeros are shifted into high order bits)  
==
equal to  
!=
not equal to  
<=
less than or equal to  
>=
greater than or equal to  
<
less than  
>
greater than  

The assembler supports the unary operators listed in Table 2.7.

Unary operators:

 

Operator
Description
+
unary plus  
-
unary minus  
~
unary bitwise complement  

The assembler also supports the operations listed in Table 2.8.

Alternate operators:

 

Operator
Description
<>
not equal to  
//
modulo  
!
logical OR  
!!
logical XOR  

The operators have the following precedence, with the highest priority first:

1. unary + - ~

2. * / %

3. binary + -

4. << >>

5. < <= > >=

6. == !=

7. &

8. ^

9. |

10. &&

11. ||


Comments

Comments are text that the assembler ignores. You can use them to document your code.

There are several ways you can specify comments:


NOTE

The asterisk (*) must be the first character of the line for it to specify a comment. The asterisk has other meanings when it occurs elsewhere in a line.



NOTE

The assembler distinguishes between a comment that begins with a pound sign (#) and a preprocessor directive that begins with a pound sign.


The three immediately preceding comment methods are helpful for porting existing code.


Data Alignment

By default, the assembler aligns all data on a natural boundary for the data size and for the target processor family. You can turn off alignment with the alignment argument to the .option directive, described in "option."

The assembler does not align data automatically in the .debug section. For more information on the .debug section, see "Debugging Directives."

 

 


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: July 20, 2000