Vous êtes sur la page 1sur 65

What is an Assembler?

An assembler is a program that translates symbolic code (assembly language) into executable object
code. This object code can be executed with a 80C51-compatible microcontroller. If you have ever
written a computer program directly in machine-recognizable form, such as binary or hexadecimal
code, you will appreciate the advantages of programming in symbolic assembly language.
Assembly language operation codes (mnemonics) are easily remembered (MOV for move instructions,
ADD for addition, and so on). You can also symbolically express addresses and values referenced in
the operand field of instructions. Because you assign these names, you can make them as meaningful
as the mnemonics for the instructions. For example, if your program must manipulate a date as data,
you can assign it the symbolic name DATE. If your program contains a set of instructions used as a
timing loop (executed repeatedly until a specific amount of time has passed), you can name the
instruction group TIMER_LOOP.
An assembly program has three parts:

Machine instructions - Code the machine can execute. Detailed discussion of the machine
instructions is in the hardware manuals of the 80C51 microcontroller.

Assembler directives - Define the program structure and symbols, and generate non
executable code (data, messages, and so on.).

Assembler controls - Set assembly modes and direct assembly flow.

Un ensamblador es un programa que traduce el cdigo simblico (lenguaje


ensamblador) en cdigo objeto ejecutable. Este cdigo objeto puede ser
ejecutado con un microcontrolador 80C51-compatible. Si alguna vez ha escrito
un programa de ordenador directamente en la mquina de forma reconocible, como
cdigo binario o hexadecimal, apreciarn las ventajas de la programacin en
lenguaje ensamblador simblico.
En el lenguaje Asembler los cdigos de las operacin (mnemotcnicos) son
fciles de recordar (MOV para las instrucciones de mover, ADD para la suma, y
as sucesivamente). Tambin puede expresar simblicamente las direcciones y
los valores de referencia en los operando en el campo de las instrucciones.
Debido a que asigne estos nombres, se puede hacer como significativa como los
mnemotcnicos para las instrucciones. Por ejemplo, si tu programa debe
manipular una fecha como datos, puede asignarle el nombre simblico FECHA. Si
el programa contiene un conjunto de instrucciones utilizado como un calendario
de bucle (ejecutado varias veces hasta una determinada cantidad de tiempo que
ha pasado), usted puede nombrar a la instruccin grupo TIMER_LOOP.
Un programa en asembler tiene tres partes:
Instrucciones de mquina - Cdigo de la mquina puede se
ejecutado. Detallada explicacin de instrucciones de mquina est
en los manuales hardware del 80C51 microcontrolador.
Assembler directivas - Definir la estructura de los programas y
los smbolos, y no generar cdigo ejecutable (datos, mensajes,
etc.).
Assembler controles Fija los modos del ensamblador.

Assembly Statements
Home Writing Assembly Programs Assembly Statements

Assembly program source files are made up of statements that may include controls,
assembler control statements, or 8051 MCU instructions (mnemonics). For example:
$TITLE(Demo Program #1)
CSEG
JMP
END

AT
$

0000h

This example program consists of four statements. $TITLE is a directive, CSEG and END
are control statements, and JMP is an 8051 MCU instruction.
Each line of an assembly program can contain only one directive, control
statement, or MCU instruction. Statements must be contained in exactly one line.
Multiline statements are not allowed.
Statements in x51 assembly programs are not column sensitive. Directives, control
statements, or MCU instructions may start in any column. Indentation used in the
examples in this manual is done for program clarity and is neither required nor expected by
the assembler. The only exception is that arguments and instruction operands must be
separated from directives, control statements, or MCU instructions by at least one space.
All x51 assembly programs must include the END control statement. This control
statement signals to the assembler that this is the end of the assembly program. Any MCU
instructions, directives, or control statements found after the END control statements are
ignored. The shortest valid assembly program contains only an END control statement.

Controls
Home Writing Assembly Programs Assembly Statements Controls

Assembler controls instruct the assembler how to process subsequent assembly language
instructions. Controls also provide a way for you to define program constants and reserve
space for variables.
Assembler controls direct the operation of the assembler when generating a listing file or
object file. Typically, controls do not impact the code that is generated by the assembler.
Controls can be specified on the command line or within an assembler source file.
The conditional assembly controls are the only assembler controls that will impact the code
that is assembled by the Ax51 assembler. The IF, ELSE, ENDIF, and ELSEIF assembler
control statements provide a powerful set of conditional operators that can be used to
include or exclude certain parts of your program from the assembly.

The topic Control Statements describes the available assembler controls in detail and
provides an example of each. Refer to this topic for more information about control
statements.

Instructions
Home Writing Assembly Programs Assembly Statements Instructions

Assembly language instructions specify the program code that is to be assembled by the
Ax51 assembler. The Ax51 assembler translates the assembly instructions in your program
into machine code and stores the resulting code in an object file.
Assembly instructions have the following general format:
label: mnemonic operand , operand /, operand ; comment

Where
label

symbol name that is assigned the address at which the instruction is located.

mnemonic is the ASCII text string that symbolically represents a machine language

instruction.
operand

is an argument that is required by the specified mnemonic.

comment

is an optional description or explanation of the instruction. A comment may


contain any text you wish. Comments are ignored by the assembler.

Refer to the x51 microcontrollers 8051 Instruction Set Manual, where they are listed by
mnemonic and by machine language opcode, for more information about assembler
instructions.

Comments
Home Writing Assembly Programs Comments

Comments are lines of text that you may include in your program to identify and explain the
program. Comments are ignored by the Ax51 assembler and are not required in order to
generate working programs.
You may include comments anywhere in your assembler program. Comments must be
preceded with a semicolon character (;). A comment can appear on a line by itself or can
appear at the end of an instruction. For example:
;This is a comment
NOP

;This is also a comment

When the assembler recognizes the semicolon character on a line, it ignores subsequent
text on that line. Anything that appears on a line to the right of a semicolon will be ignored
by the Ax51 assembler. Comments have no impact on object file generation or the code
contained therein.

Symbols and Symbol Names


Home Writing Assembly Programs Symbols and Symbol Names

A symbol is a name that you define to represent a value, text block, address, or register
name. You can also use symbols to represent numeric constants and expressions.
Symbol Names
Symbols are composed of up to 31 characters from the following list:
A Z, a z, 0 9, _, and ?
A symbol name can start with any of these characters except the digits 0 9.
Symbols can be defined in a number of ways. You may define a symbol to represent an
expression using the EQU or SET control statements:
NUMBER_FIVE
TRUE_FLAG
FALSE_FLAG

EQU
SET
SET

5
1
0

You may define a symbol to be a label in your assembly program:


LABEL1:

DJNZ

R0, LABEL1

You can define a symbol to refer to a variable location:


SERIAL_BUFFER

DATA

99h

Symbols are used throughout assembly programs. A symbolic name is much easier to
understand and remember than an address or numeric constant. The following topics
provide more information about how to use and define

Labels
Home Writing Assembly Programs Labels

A label defines a "place" (an address) in your program or data space. All rules that apply to
symbol names also apply to labels. When defined, a label must be the first text field in a
line. It may be preceded by tabs or spaces. A colon character (:) must immediately follow
the symbol name to identify it as a label. Only one label may be defined on a line. For
example:
LABEL1:
LABEL2:
NUMBER:
COPY:

DS

DB
MOV

27, 33, 'STRING', 0


R6, #12H

;label by itself
;label at a message
;label in a program

In the above example, LABEL1, LABEL2, NUMBER, and COPY are all labels.
When a label is defined, it receives the current value of the location counter of the currently
selected segment. Refer to Location Counter for more information about the location
counter.
You may use a label just like you would use a program offset within an instruction. Labels
can refer to program code, to variable space in internal or external data memory, or can
refer to constant data stored in the program or code space.
You may use a label to transfer program execution to a different location. The instruction
immediately following a label can be referenced by using the label. Your program can jump
to or make a call to the label. The code immediately following the label will be executed.
You may use labels to provide information to simulators and debuggers. A simulator or
debugger can provide the label symbols while debugging. This can help to simplify the
debugging process.
Labels may only be defined once. They may not be redefined.

Location Counter
Home Writing Assembly Programs Expressions and Operators Location Counter

The Ax51 assembler maintains a location counter for each segment which contains the
offset of the instruction or data being assembled. The location counter increments after
each line by the number of bytes of code or data in that line.
The location counter is initialized to 0 for each segment, but may be changed using the ORG
statement.
The dollar sign character ('$') may be used in an expression to obtain the current value of
the location counter. For example, the following uses the location counter to determine the
length of a string.
msg:
msg_len:

DB
EQU

'This is a message', 0
$-msg

You may use the location counter in instructions. For example, the following creates an
infinite loop:
JMP

; While (1)

Using Operands and Expressions


Home Writing Assembly Programs Using Operands and Expressions

Operands are arguments, or expressions, that are specified along with assembler directives
or instructions. Assembler directives require operands that are constants or symbols. For
example:
VVV

EQU
DS

3
10h

Assembler instructions support a wider variety of operands than do directives. Some


instructions require no operands and some may require up to 3 operands. Multiple operands
are separated by commas. For example:
MOV

R2, #0

The number of operands that are required and their types depend on the instruction or
directive that is specified. In the following table, the first four operands can also be
expressions. Instruction operands can be classified as one the following types:
Operand Type

Description

Immediate Data

Symbols or constants the are used as an numeric value.

Direct Bit Address

Symbols or constants that reference a bit address.

Program Addresses

Symbols or constants that reference a code address.

Direct Data Addresses

Symbols or constants that reference a data address.

Indirect Addresses

Indirect reference to a memory location, optionally with offset.

Special Assembler Symbol Register names.

Special Assembler Symbols


Home Writing Assembly Programs Special Assembler Symbols

The Ax51 assembler defines and reserves names of the x51 register set. These predefined
names are used in x51 programs to access the processor registers. Following, is a list of the
each of the 8051, 80C51MX, and 251 registers along with a brief description:
Register Description
A

Represents the 8051 Accumulator. It is used with many operations including


multiplication and division, moving data to and from external memory, Boolean
operations, etc.

DPTR

The DPTR register is a 16 bit data pointer used to address data in XDATA or
CODE memory.

PC

The PC register is the 16 bit program counter. It contains the address of the

next instruction to be executed.


C
AB
R0 R7

The Carry flag; indicates the status of operations that generate a carry bit. It is
also used by operations that require a borrow bit.
The A and B register pair used in MUL and DIV instructions.
The eight 8 bit general purpose 8051 registers in the currently active register
bank. A Maximum of four register banks are available.

AR0
AR7

Represent the absolute data addresses of R0 through R7 in the current register


bank. The absolute address for these registers changes depending on the
register bank that is currently selected. These symbols are only available when
the USING assembler statement is given. Refer to the USING assembler
statement for more information on selecting the register bank. These
representations are suppressed by the NOAREGS directive off the Cx51
compiler.

PR0,
PR1

The Universal Pointer Registers of the 80C51MX architecture. Universal Pointer


can access the complete 16MB address space of the 80C51MX. PR0 is
composed of registers R1, R2, and R3. PR1 is composed of registers R5, R6,
and R7.

EPTR

Additional extended data pointer register of the 80C51MX architecture. EPTR


may be used to access the complete memory space.

Immediate Data
Home Writing Assembly Programs Immediate Data

An immediate data operand is a numeric expression that is encoded as a part of the


machine language instruction. Immediate data values are used literally in an instruction to
change the contents of a register or memory location. The pound (or number) sign (#) must
precede any expression that is to be used as an immediate data operand. The following
shows some examples of how the immediate data is typically used:
MY_VAL

EQU

50H

; an equate symbol

MOV
MOV
MOV
ANL
XRL
MOV

A,IO_PORT2
A,#0E0h
DPTR,#0x8000
A,#128
A,#0FFh
R5,#MY_VAL

;
;
;
;
;
;

direct memory access to DATA


load 0xE0 into the accumulator
load 0x8000 into the data pointer
AND the accumulator with 128
XOR A with 0ffh
load R5 with the value of MY_VAL

Memory Access
Home Writing Assembly Programs Memory Access

A memory access reads or writes a value in to the various memory spaces of the A51
system.
Direct memory access encodes the memory address in the instruction that to reads or
writes the memory. With direct memory accesses, you may access variables in the memory
class DATA and BIT.

Indirect memory accesses uses the content of a register in the instruction that reads or
writes into the memory. With indirect address operands it is possible to access all memory
classes of the A51.
The examples in DATA and BIT show how to access the different memory classes of an A51
system.

DATA
Home Writing Assembly Programs Memory Access DATA

Memory locations in the memory class DATA can be addressed with both direct and indirect
memory accesses. Special Function Registers (SFR) of the A51 have addresses above 0x80
in the DATA memory class. SFR locations can only be addressed with direct memory
accesses. An indirect memory access to SFRs is not supported in the A51 microcontrollers.
Example for all 8051 variants
?DT?myvar

; define a SEGMENT of class DATA

VALUE:

SEGMENT DATA
RSEG
?DT?myvar
DS
1

IO_PORT2
VALUE2

DATA
DATA

0A0H
20H

; special function register


; absolute memory location

CODE
?PR?myprog
A,IO_PORT2
A,VALUE
VALUE2,A
R1,#VALUE
A,@R1

; define a segment for program code

?PR?myprog SEGMENT
RSEG
MOV
ADD
MOV
MOV
ADD

; reserve 1 BYTE in DATA space

; direct memory access to DATA

; load address of VALUE to R1


; indirect memory access to VALUE

BIT
Home Writing Assembly Programs Memory Access BIT

Memory locations in the memory class BIT are addressed with the bit instructions of the
8051. The Special Function Registers (SFR) that are located in bit-addressable memory
locations can be addressed with bit instructions. Bit-addressable SFR locations are: 80H,
88H, 90H, 98H, 0A0H, 0A8H, 0B0H, 0B8H, 0C0H, 0C8H, 0D0H, 0D8H, 0E0H, 0E8H, 0F0H,
and 0F8H.
Example for all 8051 variants
?BI?mybits SEGMENT
RSEG
FLAG:
DBIT
P1
DATA
GREEN_LED BIT

BIT
?BI?mybits
1
90H
P1.2

?PR?myprog SEGMENT CODE


RSEG
?PR?myprog
SETB
GREEN_LED

; define a SEGMENT of class BIT


; reserve 1 Bit in BIT space
; 8051 SFR PORT1
; GREEN LED on I/O PORT P1.2
; define a segment for program code
; P1.2 = 1

is_on:

JB
SETB
CLR
:
CLR
CLR

FLAG,is_on
FLAG
ACC.5

; direct memory access to DATA


; reset bit 5 in register A

FLAG
GREEN_LED

; P1.2 = 0

IDATA
Home Writing Assembly Programs Memory Access IDATA

Variables in this memory class are accessed via registers R0 or R1.


Example for all 8051 variants
?ID?myvars SEGMENT IDATA
RSEG
?EB?mybits
BUFFER:
DS
100

; define a SEGMENT of class IDATA

?PR?myprog SEGMENT
RSEG
MOV
MOV
INC
MOV

; define a segment for program code

CODE
?PR?myprog
R0,#BUFFER
A,@R0
R0
@R0,A

; reserve 100 Bytes

;
;
;
;

load the address in R0


read memory location buffer
increment memory address in R0
write memory location buffer+1

EDATA
Home Writing Assembly Programs Memory Access EDATA

The EDATA memory is only available in the NXP 80C51MX.


In the NXP 80C51MX, the EDATA memory can be accessed via EPTR or the Universal
Pointers PR0 and PR1. Universal Pointers can access any memory location in the 16MB
address space.
Example for NXP 80C51MX
?ED?my_seg SEGMENT EDATA
RSEG
?ED?my_seg
STRING:
DS
100

; define a SEGMENT of class EDATA

?PR?myprog SEGMENT CODE


RSEG
?PR?myprog
MOV
R1,#BYTE0 STRING
MOV
R2,#BYTE1 STRING
MOV
R3,#BYTE2 STRING
MOV
A,@PR0

; define a segment for program code

; reserve 100 Bytes

; load address of STRING in PR0

; load first byte of STRING in A

XDATA
Home Writing Assembly Programs Memory Access XDATA

The XDATA memory class can be accessed with the instruction MOVX via the register DPTR.
A single page of the XDATA memory can be also accessed via the registers R0, R1. At the C
Compiler level this memory type is called pdata and the segment prefix ?PD? is used. The
high address for this pdata page is typically set with the P2 register. But in new 8051
variants there are also dedicated special function registers that define the XDATA page
address.
Example for all 8051 variants
?XD?my_seg SEGMENT XDATA
RSEG
?ED?my_seg
XBUFFER:
DS
100

; define a SEGMENT of class XDATA

?PD?myvars SEGMENT XDATA INPAGE


RSEG
?PD?myvars
VAR1:
DS
1

; define a paged XDATA segment

?PR?myprog SEGMENT
RSEG
MOV
:
MOV
MOVX
MOV
MOVX

CODE
?PR?myprog
P2,#HIGH ?PD?myvars

; define a segment for program code

DPTR,#XBUFFER
A,@DPTR
R1,#VAR1
@R1,A

;
;
;
;

; reserve 100 Bytes

; reserve 1 byte

; load page address register


load address
access via DPTR
load address
access via R0 or R1

CODE
Home Writing Assembly Programs Memory Access CODE

CODE memory can be accessed with the instruction MOVC via the DPTR register.
Example for all 8051 variants
?CO?my_seg SEGMENT CODE
RSEG
?CO?my_seg
TABLE:
DB
1,2,4,8,0x10

; define a SEGMENT of class CODE

?PR?myprog SEGMENT
RSEG
MOV
MOV
MOVC

; define a segment for program code

CODE
?PR?myprog
DPTR,#TABLE
A,#3
A,@A+DPTR

; a table with constant values

; load address of table


; load offset into table
; access via MOVC instruction

Program Addresses
Home Writing Assembly Programs Memory Access Program Addresses

Program addresses are absolute or relocatable expressions with the memory class CODE or
ECODE. Typically, program addresses are used in jump and call instructions. For indirect
jumps or calls, it is required to load a program address in a register or a jump table. The
following jumps and calls are possible:

Program
Addresses

Description

SJMP JZ JNZ

Relative jumps include conditional jumps (CJNE, DJNZ, JB, JBC,


JC, ) and the unconditional SJMP instruction. The addressable
offset is 128 to +127 bytes from the first byte of the instruction
that follows the relative jump. When you use a relative jump in
your code, you must use an expression that evaluates to the code
address of the jump destination. The assembler does all the offset
computations. If the address is out of range, the assembler will
issue an error message.

ACALL AJMP

In-block jumps and calls permit access only within a 2KByte


block of program space. The low order 11 bits of the program
counter are replaced when the jump or call is executed. For Dallas
390 contiguous mode the block size is 512KB or 19 bits. If ACALL
or AJMP is the last instruction in a block, the high order bits of the
program counter change and the jump will be within the block
following the ACALL or AJMP.

LCALL LJMP

Long jumps and calls allow access to any address within a


64KByte segment of program space. The low order 16 bits of the
program counter are replaced when the jump or call is executed.
For Dallas 390 contiguous mode: the block size is 16MB or 24 bits.
On NXP 80C51MX: if LCALL or LJMP is the last instruction in a
64KByte segment, the high order bits of the program counter
change and the jump will into the segment following the LCALL or
LJMP.

ECALL EJMP

Extended jumps and calls allow access within the extended


program space of the NXP 80C51MX.

CALL JMP

Generic jumps and calls are two instruction mnemonics that do


not represent a specific opcode. JMP may assemble to SJMP,
AJMP, LJMP or EJMP. CALL may assemble to ACALL, LCALL or
ECALL. These generic mnemonics always evaluate to an
instruction, not necessarily the shortest, that will reach the
specified program address operand.

Example for all 8051 Variants

EXTRN CODE (my_function)


CSEG
JMP
?PR?myintr SEGMENT
RSEG
ext_int:
JB
INC
flag_OK:
CPL
RETI
?PR?myprog SEGMENT
RSEG

AT
3
ext_int

; an interrupt vector

CODE
?PR?myintr
FLAG,flag_OK
my_var
FLAG

; define a segment for program code

CODE INBLOCK
?PR?myprog

; a segment within a 2K block

func1:
loop:

CALL
CALL
MOV
JNZ
RET

sub_func
my_function
A,my_var
loop

sub_func:

CLR
MOV
CALL
DJNZ
RET

FLAG
R0,#20
my_function
R0,loop1

loop1:

; will generate ACALL


; external function -> LCALL

Example with EJMP, ECALL for NXP 80C51MX


EXTRN ECODE:FAR (my_farfunc)
Reset

EQU

ECODE 0FF0000H

; Reset location on 251

?PR?my_seg SEGMENT ECODE


RSEG
?PR?my_seg

; define a SEGMENT of class EDATA

func1

; far function called with ECALL


; generates LCALL
; generates ECALL

mylab:

func2

PROC
CALL
CALL
JNB
EJMP
ERET
ENDP

FAR
func2
my_farfunc
Flag,mylab
Reset

PROC
CALL
RET
ENDP

NEAR
my_farfunc

; generates ECALL

Expressions and Operators


Home Writing Assembly Programs Expressions and Operators

An operand may be a numeric constant, a symbolic name, a character string or an


expression.
Operators are used to combine and compare operands within your assembly program.
Operators are not assembly language instructions nor do they generate A51 assembly code.
They represent operations that are evaluated at assembly time. Therefore, operators can
only handle calculations of values that are known when the program is assembled.
An expression is a combination of numbers, character string, symbols, and operators that
evaluate to a single 32 bit binary number (for A51: 16-bit binary number). Expressions are
evaluated at assembly time and can, therefore, be used to calculate values that would
otherwise be difficult to determine beforehand.
The following topics describe operators and expressions and how they are used in A51
assembly programs.

Numbers
Home Writing Assembly Programs Expressions and Operators Numbers

Numbers may be specified in hexadecimal (base 16), decimal (base 10), octal (base 8), and
binary (base 2). The default representation is decimal. A decimal, octal, or binary number's
first character must always be a digit (0-9). Hexadecimal numbers whose first character is
not a digit (0-9) must be prefixed with a zero ('0').
The base of a number is specified by the last character in the number. A number that is
specified without an explicit base is interpreted as decimal number.
The following table illustrates various types of numbers:
Base

Suffix

Legal Characters

Hexadecimal

H,h

0-9, A-F, a-f

Decimal

D,d

0-9

1234 65590D 20d 123

O,o,Q,q

0-7

177O 7777o 25O 123o 177777O

B,b

0-1

1111B 10011111B 101010101B

Octal
Binary

Examples
1234H 99H 123H 0A0F0H 0FFH

Note

Hexadecimal numbers may be written using C notation. For example, 0x12AB.

Hexadecimal numbers must begin with a 0 if the first digit is A-F.

The dollar sign character ('$') may be used in numbers to make them more readable
(as long it is not the first or last character). For example:
1111$0000$1010$0011B
1$2$3$4

- is equivalent to - is equivalent to -

1111000010100011B
1234

Characters
Home Writing Assembly Programs Expressions and Operators Characters

ASCII characters may be used in an expression to generate numeric values. Expressions


may have up to two ASCII characters enclosed in single quotes ('). More than two
characters in an expression causes an error. Following are several examples:
Expression

Value

'A'

0041h

'AB'

4142h

'a'

0061h

'ab'

6162h

''
'ABC'

Error: Null string is invalid.


Error: More than 2 characters.

Characters may be used anywhere in your program as a immediate data operand. For
example:

LETTER_A

EQU

'A'

TEST:

MOV

@R0, #'F'

SUBB

A, #'0'

Character Strings
Home Writing Assembly Programs Expressions and Operators Character Strings

Character strings can be used in combination with the DB assembler statement to define
messages that are used in your Ax51 assembly program. Character strings must be
enclosed within single quotes ('). For example:
KEYMSG:

DB

'Press any key to continue.'

generates the hexadecimal data (50h, 72h, 65h, 73h, 73h, 20h, 6Eh, 75h, 65h, 2Eh)
starting at KEYMSG. You can mix string and numeric data on the same line. For
example:
EOLMSG:

DB

'End of line', 00h

appends the value 00h to the end of the string 'End of line'.
Two successive single quote characters can be used to insert a single quote into a string.
For example:
MSGTXT:

DB

'ISN''T A QUOTE REQUIRED HERE?'.

Location Counter
Home Writing Assembly Programs Expressions and Operators Location Counter

The Ax51 assembler maintains a location counter for each segment which contains the
offset of the instruction or data being assembled. The location counter increments after
each line by the number of bytes of code or data in that line.
The location counter is initialized to 0 for each segment, but may be changed using the
ORG statement.
The dollar sign character ('$') may be used in an expression to obtain the current value
of the location counter. For example, the following uses the location counter to
determine the length of a string.
msg:

DB

'This is a message', 0

msg_len:

EQU

$-msg

You may use the location counter in instructions. For example, the following creates an
infinite loop:
JMP

; While (1)

Operators
Home Writing Assembly Programs Expressions and Operators Operators

Operators may be unary (requiring one operand) or binary (requiring two operands). The
combination of an operator and its operand(s) is an expression. Parentheses may be used in
expressions with multiple operators to specify the order in which operators are evaluated. If
no parentheses are used in an expression, operator precedence determines the evaluation
order.
Following is the operator precedence table:
MBYTE
Level

Operators

()

NOT HIGH LOW


BYTE0 BYTE1 BYTE2 BYTE3
WORD0 WORD2
MBYTE

Unary + Unary -

* / MOD

+-

SHL SHR

AND OR XOR

EQ = NE <> LT < LTE <= GT > GTE >=

( ) Assembler Operator
Home Writing Assembly Programs Expressions and Operators Operators ( )

Syntax

None.

Example

None.

* Assembler Operator
Home Writing Assembly Programs Expressions and Operators Operators *

Syntax

operand * operand

Descriptio
n

The multiplication operator ('*') multiplies the specified absolute


operands. The result is absolute.

See Also

/, MOD

Example

MOV

R1,#3*4

MOV

R2,#99*45

+ Assembler Operator
Home Writing Assembly Programs Expressions and Operators Operators +

Syntax

+ operand
operand + operand

Descriptio
n

The plus sign ('+') is used as the unary plus operator and as the addition
operator.
The unary plus operator is used to assign a positive value to the specified
operand. This operator does effectively nothing to alter the value of the
operand.
The addition operator is used to add the values of two operands which
may be absolute or relocatable. The type of the resulting expression is
determined by the operand types as shown in the following table:
Operand Types

Resulting Expression Type

absolute + absolute

absolute

absolute + relocatable

relocatable

relocatable + absolute

relocatable

relocatable + relocatable
Note

Error: Not allowed.

As a general rule, relocatable expressions must evaluate to a


relocatable symbol plus or minus an optional constant value
(displacement).

See Also

Example

labx:

MOV

R1, #1234+5678

MOV

R2, labx+10h

- Assembler Operator
Home Writing Assembly Programs Expressions and Operators Operators -

Syntax

- operand
operand1 - operand2

Descriptio
n

The minus sign ('-') is used as the unary minus operator and as the
subtraction operator.
The unary minus operator is used to change the sign of the specified
operand. If the value of the operand is positive, the sign is changed to
negative. If the value is negative, it is changed to positive.
The subtraction operator is used to subtract the value of operand2 from
the value of operand1. The specified operands may be absolute or
relocatable. The type of the resulting expression is determined by the
operand types as shown in the following table:
Operand Types

Resulting Expression Type

absolute - absolute

absolute

absolute - relocatable

Error: Not allowed.

relocatable - absolute

relocatable

relocatable - relocatable
absolute1
1. Relocatable operands are allowed if both are defined in the same
parent section. External operands are not allowed.
Note

As a general rule, relocatable expressions must evaluate to a


relocatable symbol plus or minus an optional constant value

(displacement).
See Also

Example

labx:

MOV

R1, #5678-1234

laby:

MOV

R2, laby-labx

/ Assembler Operator
Home Writing Assembly Programs Expressions and Operators Operators /

Syntax

operand1 / operand2

Descriptio
n

The division operator ('/') divides the value of operand1 by the value of
operand2. Both operands must be absolute. The result is absolute.

See Also

*, MOD

Example

MOV

R1,#12/4

MOV

R2,#99/9

Control Statements
Home Control Statements

The Ax51 Assembler provides a number of control statements that permit you to define
symbol values, reserve and initialize storage, and control the placement of your code.
These statements should not be confused with processor instructions or with assembler
directives. They do not produce executable code and, with the exception of the DB, DD, and
DW statements, they have no direct effect on the contents of code memory. These controls
change the state of the assembler, define user symbols, and add information to the object
file.
Control statements may be divided into the following categories:

Address Control
Statement

Description

EVEN1

Forces the location counter to the next even address.

ORG

Sets the location counter to a specifies offset or address.

USING

Specifies which register bank to use.

Conditional Assembly
Statement

Description

IF

Begins an IF-ELSE-ENDIF block.

ELSEIF

Begins an alternate IF block.

ELSE

Begins an ELSE block.

ENDIF

Terminates an IF block.

Memory Initialization
Statement

Description

DB

Allocates memory for one or more defined byte values.

DD

Allocates memory for one or more defined double word values.

DW

Allocates memory for one or more defined word values.

Memory Reservation
Statement

Description

DBIT

Reserves space for one or more bits.

DS

Reserves space for one or more bytes.

DSB

DSD

Reserves space for one or more bytes.


Reserves space for one or more double words.

DSW1

Reserves space for one or more words.

Procedure Declaration
Statement

Description

ENDP1

Defines the end of a function.

LABEL
PROC

Assigns an address to a symbolic name.


Defines the beginning of a function.

Program Linkage
Statement

Description

EXTERN1

Defines external symbols.

EXTRN

Defines external symbols.

NAME

Specifies the name of the current module.

PUBLIC

Defines symbols which may be used in other modules.

Segment Control
Statement

Description

BSEG

Defines an absolute bit segment.

CSEG

Defines an absolute code segment.

DSEG

Defines an absolute data segment.

ISEG

Defines an absolute idata segment.

RSEG

Selects a relocatable segment.

SEGMENT

Defines a relocatable segment.

XSEG

Defines an absolute xdata segment.

Symbol Definition
Statement

Description

BIT

Defines an address in bit space.

CODE

Defines an address in code space.

DATA

Defines an address in data space.

EQU

Sets a permanent symbol value.

IDATA

Defines an address in idata space.

LIT

Assigns a text string to a symbol name.

SBIT

Defines a bit SFR.

SET

Sets or resets a symbol value.

SFR

Defines a byte SFR.

SFR16

Defines a word SFR.

XDATA

Defines an address in xdata space.

Miscellaneous
Statement

Description

__ERROR__

Generates an error message.

END

Signals the end of the assembly module.

Note
1. This directive is available in the AX51 Assembler only.

Symbols and Symbol Names


Home Writing Assembly Programs Symbols and Symbol Names

A symbol is a name that you define to represent a value, text block, address, or register
name. You can also use symbols to represent numeric constants and expressions.
Symbol Names
Symbols are composed of up to 31 characters from the following list:
A Z, a z, 0 9, _, and ?
A symbol name can start with any of these characters except the digits 0 9.
Symbols can be defined in a number of ways. You may define a symbol to represent an
expression using the EQU or SET control statements:
NUMBER_FIVE
TRUE_FLAG
FALSE_FLAG

EQU
SET
SET

5
1
0

You may define a symbol to be a label in your assembly program:


LABEL1:

DJNZ

R0, LABEL1

You can define a symbol to refer to a variable location:


SERIAL_BUFFER

DATA

99h

Symbols are used throughout assembly programs. A symbolic name is much easier to
understand and remember than an address or numeric constant. The following topics
provide more information about how to use and define symbols.

Labels
Labels are special cases of symbols. Labels are used only before statements that
have physical addresses associated with them.
Examples of such statements are assembly language instructions, data storage
directives (DB and DW), and data reservation directives (DS and DBIT). Labels must
follow all the rules of symbol creation with the additional requirement that they be
followed by a colon. The following are legal examples of label uses:
TABLE_OF_CONTROL_CONSTANTS:
DB 0,1,2,3,4,5 (Data storage)
MESSAGE: DB 'HELP' (Data storage)
VARIABLES: DS 10 (Data reservation)

BIT_VARIABLES: DBIT 16 (Data reservation)


START: MOV A,#23 (Assembly language instruction)

Directives
The Ax51 Assembler provides a number of directives you may use to control source file assembly.
Directives are composed of one or more letters or digits and, unless otherwise specified, may be
specified after the filename on the command line or within the source file when preceded by a dollar
sign ('$'). For example:

A51 SAMPLE.A51 SYMBOLS DEBUG


or

$SYMBOLS DEBUG
or

$SYMBOLS
$DEBUG
The source file to assemble is SAMPLE.A51 and SYMBOLS and DEBUG are the directives.
Assembler directives may be divided into two groups:

Primary directives are specified on the assembler command line or in first few lines of the
assembly source file. These directives control how the assembler behaves for the entire
module. If conflicting primary directives are specified on the command line and in the source
file, the command line directive takes precedence.

General directives may be specified on the command line or anywhere in the source file.
These directives control the immediate behaviour of the assembler and may be changed
during assembly.

Note

Syntax is the same for directives specified on the command line and those specified in the
source file.

Some directives may not be specified on the command line. If one of these directives is
specified on the command line, the assembler generates a fatal error and aborts.

The following table is an alphabetical list of the control directives available in the Ax51 Assembler.
Directive

Group

Description

CASE

Primar
y

Enables case-sensitive symbol names.

COND

Genera
l

Includes (in the listing file) conditional source lines skipped by the
preprocessor.

DATE

Primar
y

Specifies the date to use in the listing file header.

DEBUG

Primar
y

Includes debugging information in the object file.

DEFINE

Primar
y

Defines C preprocessor symbols on the command line.

EJECT1

Genera
l

Inserts a form feed character into the listing file.

ELSE

Genera
l

Assemble block if the condition of a previous IF is false.

ELSEIF

Genera
l

Assemble block if condition is true and a previous IF or ELSEIF is


false.

ENDIF

Genera
l

Ends an IF block.

ERRORPRINT

Primar
y

Specifies the file name for error messages.

GEN

Genera
l

Includes all macro expansions in the listing file.

IF

Genera
l

Assemble block if condition is true.

INCDIR

Primar
y

Sets additional include file paths.

INCLUDE2

Genera
l

Includes the contents of another file.

LIST

Genera
l

Includes the assembly source text in the listing file.

MACRO

Primar
y

Enables preprocessor expansion of standard macros.

MOD_CONT

Primar
y

Enables 24-bit contiguous addressing for Dallas Semiconductor


devices.

MOD_MX51

Primar
y

Enables instruction set extensions for the Philips 80C51MX


architecture.

MOD51

Primar
y

Enables code generation and defines SFRs for classic 8051 devices.

MPL

Primar
y

Enables preprocessor expansion of MPL macros.

NOAMAKE

Primar
y

Excludes build information from the object file.

NOCASE

Primar
y

Disables case-sensitive symbol names. All symbols are converted


to uppercase.

NOCOND

Primar
y

Excludes (from the listing file) conditional source lines skipped by


the preprocessor.

NODEBUG

Primar
y

Excludes debugging information from the object file.

NOERRORPRINT

Primar
y

Disables error messages output to the screen.

NOGEN

Genera
l

Excludes macro expansions from the listing file.

NOLINES

Primar
y

Excludes line number information from the generated object


module.

NOLIST

Genera
l

Excludes the assembly source text from the listing file.

NOMACRO

Primar
y

Disables preprocessor expansion of standard and MPL macros.

NOMOD51

Primar
y

Suppresses SFRs definitions for classic 8051 devices

NOMPL

Primar
y

Disables preprocessor expansion of MPL macros.

NOOBJECT

Primar
y

Disables object file generation.

NOPRINT

Primar
y

Disables listing file generation.

NOREGISTERBAN
K

Primar
y

Disables memory space reservation for register banks.

NOSYMBOLS

Primar
y

Excludes the symbol table from the listing file.

NOSYMLIST

Genera
l

Excludes subsequently defined symbols from the symbol table.

NOXREF

Primar
y

Excludes the cross-reference table from the listing file.

OBJECT

Primar
y

Specifies the name for the object file.

PAGELENGTH

Primar
y

Specifies the number of lines on a page in the listing file.

PAGEWIDTH

Primar
y

Specifies the number of characters on a line in the listing file.

PRINT

Primar
y

Specifies the name for the listing file.

REGISTERBANK

Primar
y

Reserves memory space for register banks.

REGUSE

Genera
l

Specifies registers modified by the specified function.

RESET

Genera
l

Sets symbols, which may be tested by IF or ELSEIF, to false.

RESTORE1

Genera
l

Restores settings for the LIST and GEN directives.

SAVE1

Genera
l

Saves settings for the LIST and GEN directives.

SET

Genera
l

Sets symbols, which may be tested by IF or ELSEIF, to true or a


specified value.

SYMBOLS

Primar
y

Includes the symbol table in the listing file.

SYMLIST

Genera
l

Includes subsequently defined symbols in the symbol table.

TITLE

Primar
y

Specifies the page header title in the listing file.

XREF

Primar
y

Includes the cross-reference table in the listing file.

Note
1.

These directives may not be specified on the command line. They may only be specified in the
source file.

2.

Primary directives may not be specified after an INCLUDE directive.

Control Statements
The Ax51 Assembler provides a number of control statements that permit you to define symbol
values, reserve and initialize storage, and control the placement of your code.
These statements should not be confused with processor instructions or with assembler directives.
They do not produce executable code and, with the exception of the DB, DD, and DW statements, they
have no direct effect on the contents of code memory. These controls change the state of the
assembler, define user symbols, and add information to the object file.
Control statements may be divided into the following categories:

Address Control
Statement

Description

ORG

Sets the location counter to a specifies offset or address.

USING

Specifies which register bank to use.

Conditional Assembly
Statement

Description

IF

Begins an IF-ELSE-ENDIF block.

ELSEIF

Begins an alternate IF block.

ELSE

Begins an ELSE block.

ENDIF

Terminates an IF block.

Memory Initialization
Statement

Description

DB

Allocates memory for one or more defined byte values.

DW

Allocates memory for one or more defined word values.

Memory Reservation
Statement

Description

DBIT

Reserves space for one or more bits.

DS

Reserves space for one or more bytes.

Program Linkage
Statement

Description

EXTRN

Defines external symbols.

NAME

Specifies the name of the current module.

PUBLIC

Defines symbols which may be used in other modules.

Segment Control
Statement

Description

BSEG

Defines an absolute bit segment.

CSEG

Defines an absolute code segment.

DSEG

Defines an absolute data segment.

ISEG

Defines an absolute idata segment.

RSEG

Selects a relocatable segment.

SEGMENT

Defines a relocatable segment.

XSEG

Defines an absolute xdata segment.

Symbol Definition
Statement

Description

BIT

Defines an address in bit space.

CODE

Defines an address in code space.

DATA

Defines an address in data space.

EQU

Sets a permanent symbol value.

IDATA

Defines an address in idata space.

SBIT

Defines a bit SFR.

SET

Sets or resets a symbol value.

SFR

Defines a byte SFR.

SFR16

Defines a word SFR.

XDATA

Defines an address in xdata space.

Miscellaneous
Statement

Description

__ERROR__

Generates an error message.

END

Signals the end of the assembly module.

ORG Assembler Statement


Syntax
Descriptio
n

ORG expression

The ORG statement alters the location counter for the current segment and sets a
new origin for subsequent statements. The expression must be a simple
relocatable expression with no forward references. Only absolute addresses or
symbol values in the current segment may be used. The dollar-sign character ('$'),
which represents the current value of the location counter, may be used in the
expression.
When the ORG statement is encountered, the assembler calculates the value of the
expression and changes the location counter.

If the ORG statement appears in an absolute segment, the location counter


is assigned the absolute address value specified. The location counter may
not be set to an address below the base address of the segment.

If the ORG statement appears in a relocatable segment, the location


counter is assigned the offset of the specified expression. For example, if
the relocatable segment starts at address 1000h and if the ORG
expression has a value of 1234h, the absolute address of the next
statement is 2234h (1000h + 1234h).

The ORG statement changes the location counter, which may create a gap, but
does not create a new segment.
Note

It is possible to use the ORG statement to change the location counter and
overwrite (or overlay) existing code or data. This is supported because of
legacy programs that used this technique to define multiple variables at the
same physical address. No warning is generated if the ORG statement is
used this way.

See Also

SEGMENT

Example

ORG
ORG
ORG
ORG

100h
RESTART
EXIT1
($ + 15) AND 0FFF0h

USING Assembler Statement


Syntax
Descriptio
n

USING expression

The USING statement specifies the register bank (0-3) expression to use for
encoding the >AR0-AR7 registers. The register bank selected is noted in the
object file and the memory area is reserved by the linker.
Some 8051 instructions (like PUSH and POP) allow only absolute addresses to be
used. The assembler replaces absolute registers (AR0-AR7) with the physical
address of the register in the current register bank. So, while the instruction PUSH
R0 is not valid, PUSH AR0 is valid. However, the assembler must know which
register bank is used so that the correct physical address is calculated. This is the
purpose for the USING statement.
The USING statement does not generate any code to switch the current register
bank. The assembler program must select the correct register bank. For example,
the following code selects register bank 2:

PUSH
MOV
.
.
.
POP

PSW
PSW, #(2 SHL 3)

; save the current register bank


; set register bank 2

PSW

; restore saved register bank

The physical address is calculated as follows:


(register bank 8) + register
Note

Example

Exercise caution when using the EQU statement to define a symbol for an
absolute register (AR0-AR7). When using EQU, the symbol value is
calculated at the time it is defined (not when it is used). If the register
bank is subsequently changed with the USING statement, the defined
symbol will have the incorrect address and the code generated is likely to
fail.

USING
PUSH

3
AR2

; select register bank 3


; push R2 in bank 3 (address 1Ah)

USING
PUSH

1
AR7

; select register bank 1


; push R7 in bank 1 (address 1Fh)

DB Assembler Statement
Syntax
Descriptio
n

label: DB expression , expression ...

The DB statement initializes memory with one or more byte values. label is a
symbol that is assigned the current memory address. expression is a byte value
that is stored in memory. Each expression may be a symbol, a string, or an
expression.
The DB statement may be specified only within a code or const segment. An error
is generated if it is used in a different segment.

See Also

DBIT, DD, DS, DSB, DSD, DSW, DW

Example

MSG:
TAB:

DB
DB

'Press A Key To Continue', 0


2, 3, 5, 7, 11, 13, 17, 19, ';'

DBIT Assembler Statement


Syntax
Descriptio
n

label: DBIT expression

The DBIT statement reserves the specified number of bits in a bit or ebit segment.
label is a symbol that is assigned the current memory address. expression is the
number of bits to reserve.
This statement reserves space in the current memory space and increments the
location counter by the number of bits reserved.

See Also

DS, DSB, DSD, DSW

Example

A_FLAG:
B_FLAG:

DBIT
DBIT

1
1

DS Assembler Statement
Syntax
Descriptio
n

label: DS expression

The DS statement reserves the specified number of bytes in the current memory
space. label is a symbol that is assigned the current memory address. expression is
the number of bytes to reserve.
This statement reserves space and increments the location counter by the number
of bytes reserved.
Note

See Also

DBIT, DSB, DSD, DSW

Example

GAP:
TIME:

DS
DS
DS

(($ + 15) AND 0FFF0h) - $


10
8

; 16-byte alignment

EXTRN Assembler Statement


Syntax

EXTRN class (symbol , symbol ... )

Description
The EXTRN statement (which may appear anywhere in the assembler source file)
specifies symbols that the current source file uses but which are defined in other
object modules. The module where the symbols are defined must export them using
a PUBLIC statement.
Valid classes are:
class

Description

BIT

A symbol located in BIT memory space.

CODE

A symbol located in CODE space.

DATA

A symbol located in DATA space.

IDATA

A symbol located in IDATA memory.

XDATA

A symbol located in XDATA memory.

NUMBER

A symbol located in any memory space.

The linker resolves all external symbols and verifies that the classes and types
match. Symbols whose class is NUMBER match any memory class.

See Also

EXTERN, PUBLIC

Example

EXTRN
EXTRN
EXTRN

CODE (main)
NUMBER (tabsize)
DATA (counter)

DW Assembler Statement
Syntax
Descriptio
n

label: DW expression , expression ...

The DW statement initializes memory with one or more word (2-byte) values. label
is a symbol that is assigned the current memory address. expression is a word
value that is stored in memory. Each expression may be a symbol, a string, or an
expression.
The DW statement may be specified only within a code or const segment. An error
is generated if it is used in a different segment.

See Also

DB, DBIT, DD, DS, DSB, DSD, DSW

Example

TABLE:
HERE:
CTAB:

DW
DW
DW
DW

TABLE, TABLE+10, HERE


0
CASE0, CASE1, CASE2, CASE3
$

BSEG Assembler Statement


Syntax

BSEG AT address

Descriptio
n

The BSEG statement selects an absolute segment within BIT space.


If the optional address is included, the assembler starts the absolute segment from
that address. The valid address range is 20h-2Fh.
If the optional address is omitted, the assembler starts the absolute segment from
address 0 (if no prior absolute BIT segment was defined). If an absolute BIT segment
was previously defined, the assembler continues from the end of that segment.
Note

The start address must be an absolute expression.

The AX51 Assembler converts BSEG statements into the following:

?BI?modulename?n SEGMENT OFFS address


Where
modulename

is the name of the source file.

is a sequential number.

address

is the address specified in the BSEG statement.

See Also

BIT, CSEG, DSEG, ISEG, XSEG

Example

BSEG AT 10 ; absolute BIT segment at 0x20+10 bits =


0x21.2
DEC_FLAG: DBIT 1
; absolute bit with the name DEC_FLAG
INC_FLAG: DBIT 1
; absolute bit with the name INC_FLAG

CSEG Assembler Statement


Syntax

CSEG AT address

Descriptio
n
The CSEG statement selects an absolute segment within CODE space.
If the optional address is included, the assembler starts the absolute segment from that
address. The valid address range is 0000h-0FFFFh.
If the optional address is omitted, the assembler starts the absolute segment from
address 0 (if no prior absolute CODE segment was defined). If an absolute CODE
segment was previously defined, the assembler continues from the end of that
segment.
Note

The start address must be an absolute expression.

The AX51 Assembler converts CSEG statements into the following:

?CO?modulename?n SEGMENT OFFS address


Where
modulename

is the name of the source file.

is a sequential number.

address

is the address specified in the CSEG statement.

See Also

BSEG, CODE, DSEG, ISEG, XSEG

Example

CSEG
AT 0003h
VECT_0: LJMP
ISR_0
location

; absolute code segement at address 0x3


; jump instruction for the interrupt vector

CSEG
0x100
CRight: DB
location

AT 0x100

; absolute code segment at address

CSET
0x1000
Parity_TAB:
DB
information
DB
DB

AT 1000H

; absolute code segment at address

00H

; table with the name Parity_TAB


; fixed encoding for parity

"(C) MyCompany" ; copyright string at fixed

01H
01H

DB

00H

DSEG Assembler Statement


Syntax

DSEG AT address

Descriptio
n

The DSEG statement selects an absolute segment within DATA space.


If the optional address is included, the assembler starts the absolute segment from that
address. The valid address range is 00h-0FFh (data memory resides from 00h-7Fh and
SFRs reside from 80h-0FFh).
If the optional address is omitted, the assembler starts the absolute segment from
address 0 (if no prior absolute DATA segment was defined). If an absolute DATA
segment was previously defined, the assembler continues from the end of that
segment.
Note

The start address must be an absolute expression.

The AX51 Assembler converts DSEG statements into the following:

?DT?modulename?n SEGMENT OFFS address


Where

Example

modulename

is the name of the source file.

is a sequential number.

address

is the address specified in the DSEG statement.

DSEG
TMP_A: DS
TMP_A
TEM_B: DS
TMP_B

AT 0x40
2
4

; absolute DATA segment at 40H


; absolute data word variable with name

; absolute data dword (32-bit) variable with name

ISEG Assembler Statement


Syntax

ISEG AT address

Descriptio
n

The ISEG statement selects an absolute segment within IDATA space.


If the optional address is included, the assembler starts the absolute segment from
that address. The valid address range is 00h-0FFh.
If the optional address is omitted, the assembler starts the absolute segment from
address 0 (if no prior absolute IDATA segment was defined). If an absolute IDATA
segment was previously defined, the assembler continues from the end of that
segment.
Note

The start address must be an absolute expression.

The AX51 Assembler converts ISEG statements into the following:

?ID?modulename?n SEGMENT OFFS address


Where
modulename

is the name of the source file.

is a sequential number.

address

is the address specified in the ISEG statement.

See Also

BSEG, CSEG, DSEG, IDATA, XSEG

Example

ISEG AT 0xC0
TMP_IA:
DS
2
with name TMP_IA
TEM_IB:
DS
4
variable with name TMP_IB

; absolute IDATA segment at 0C0H


; absolute idata word variable
; absolute idata dword (32-bit)

SEGMENT Assembler Statement


Syntax

segname SEGMENT class relocation alignment

Descripti The SEGMENT statement declares a generic segment along with memory class and
on
segment location options.
Where
segnam specifies the symbol name to assign to the segment. This symbol is references
e
by subsequent RSEG statements. It may also be used in expressions to
represent the base or starting address of the combined segment.
class

is the memory classification for the segment. The class specifies the memory
space where the segment is located.

relocati
on

determines what relocation options are available to the linker for this segment.

alignme determines what address alignment options are available to the linker for this
nt
segment.
The name of each segment in a source module must be unique. The linker combines
segments that have the same type.

Class
The segment class specifies the memory space for the segment and is used by the linker
to access all segments that belong to that class. The following table lists the basic
classes.
class

Description

BIT

The BIT address space (from 20h-2Fh).

CODE

The CODE address space (from 0000h-0FFFFh).

DATA

The DATA address space (from 00h-7Fh).

IDATA

The IDATA address space (from 00h-0FFh).

XDATA

The XDATA address space (from 0000h-0FFFFh).

Following are a few examples of segments defined using the basic classes.

seg1
seg3

SEGMENT
SEGMENT

XDATA
DATA

Relocation
The relocation type specifies how the linker may relocate the segment. The following
table lists the valid kinds of relocation.
relocation

Description

BITADDRESS
ABLE

specifies that the segment is located in bit-addressable memory.


This relocation type is allowed only for segments in the DATA class
whose length does not exceed 16 bytes.

INBLOCK

specifies that the segment must be located in a single 2048-byte


block. This relocation type may be specified only for segments in the
CODE class. This is typically used for segments with routines that
use ACALL and AJMP instructions.

INPAGE

specifies that the segment must be located in a single 256-byte


page.

OVERLAYABL
E

specifies that the segment may be overlaid with and share the
memory of other segments. Segments declared with the
OVERLAYABLE relocation type may be overlaid with each other.
OVERLAYABLE segments must be declared using the naming
conventions of the C51.

Alignment
The alignment is optional and specifies how the linker allocates the segment. The
following table lists the valid alignment types.
1

alignm
ent

Description

BIT1

specifies bit alignment for the segment. This is the default for all segments
with the class BIT.

BYTE

specifies byte alignment for the segment. This is the default for all
segments, except for segments with the class BIT.

PAGE

specifies 256-byte page alignment for the segment. The segment start
must be on a 256-byte page boundary.

See Also

BSEG, CSEG, DSEG, ISEG, RSEG, XSEG

Example

; a segment with the name ?ID?IDS and the memory class IDATA.
?ID?IDS SEGMENT IDATA
; a segment with the name ?XD?ABC in the memory class XDATA. The
; alignment PAGE forces to segment to start on a 256 byte page
boundary.
?XD?ABS SEGMENT XDATA PAGE

RSEG Assembler Statement


Syntax
Descripti
on

RSEG segment

The RSEG statement selects a relocatable segment that was previously declared using
the SEGMENT statement.

See Also

SEGMENT

Example

MYPROG

SEGMENT CODE
RSEG
MYPROG

; Declare the segment


; Select the segment

2.7. Bit Addressing


The period (.) has special meaning to the Cross Assembler when
used in a symbol. It is used to explicitly specify a bit in a
bit-addressable symbol. For example, it you wanted to specify
the most significant bit in the Accumulator, you could write
ACC.7, where ACC was previously defined as the Accumulator
address. The same bit can also be selected using the physical
address of the byte it's in. For example, the Accumulator's
physical address is 224. The most significant bit of the
Accumulator can be selected by specifying 224.7. If the symbol
ON was defined to be equal to the value 7, you could also specify
the same bit by either ACC.ON or 224.ON.
2.8. ASCII Literals
Printable characters from the ASCII character set can be used
directly as an immediate operand, or they can used to define
symbols or store ASCII bytes in Program Memory. Such use of the
ASCII character set is called ASCII literals. ASCII literals are
identified by the apostrophe (') delimiter. The apostrophe
itself can be used as an ASCII literal. In this case, use two
apostrophes in a row. Below are examples of using ASCII
literals.
MOV A,#'m' ;Load A with 06DH (ASCII m)
QUOTE EQU '''' ;QUOTE defined as 27H (ASCII single quote)
DB '8051' ;Store in Program Memory
2.9. Comments

Comments are user defined character strings that are not


processed by the Cross Assembler. A comment begins with a
semicolon ( ; ) and ends at the carriage return/line feed pair
that terminates the line. A comment can appear anywhere in a
line, but it has to be the last field. The following are
examples of comment lines:
; Begin initialization routine here
$TITLE(8051 Program Vers. 1.0) ;Place version number here
TEN EQU 10 ;Constant
; Comment can begin anywhere in a line
MOV A,Serial_Port_Buffer ; Get character
2.10. The Location Counter
The Cross Assembler keeps a location counter for each of the five
segments (code, internal data, external data, indirect internal
data and bit data). Each location counter is initialized to zero
and can be modified using Assembler Directives described in
Chapter 5.
The dollar sign ($) can be used to specify the current value of
the location counter of the active segment. The following are
examples of how this can be used:
JNB FLAG,$ ;Jump on self until flag is reset
CPYRGHT: DB 'Copyright, 1983'
CPYRGHT_LENGTH
EQU $-CPYRGHT-1 ;Calculate length of copyright message
2.12. Numbers and Operators
The Cross Assembler accepts numbers in any one of four radices:

binary, octal, decimal and hexadecimal. To specify a number in a


specific radix, the number must use the correct digits for the
particular radix and immediately following the number with its
radix designator. Decimal is the default radix and the use of
its designator is optional. An hexadecimal number that would
begin with a letter digit must be preceded by a 0 (zero) to
distinguish it from a symbol. The internal representation of
numbers is 16-bits, which limits the maximum number possible.
Table 2-4 summarizes the radices available.
MAXIMUM LEGAL
RADIX DESIGNATOR LEGAL DIGITS NUMBER
----------- ---------- ------------ ----------------Binary B 0,1 1111111111111111B
Octal O,Q 0,1,2,3,4,5, 177777O
6,7 177777Q
Decimal D,(default) 0,1,2,3,4,5, 65535D
6,7,8,9 65535
Hexadecimal H 0,1,2,3,4,5, 0FFFFH
6,7,8,9,A,B,
C,D,E,F
Table 2-4: Cross Assembler Radices
No spaces or tabs are allowed between the number and the radix
designator. The letter digits and radix designators can be in
upper or lower case. The following examples list the decimal
number 2957 in each of the available radices:
101110001101B (Binary)

5615o or 5615Q (Octal)


2957 or 2957D (Decimal)
0B8DH, 0b8dh (Hexadecimal)
When using radices with explicit bit symbols, the radix
designator follows the byte portion of the address as shown in
the following examples:
0E0H.7 Bit seven of hexadecimal address 0E0
200Q.ON Bit ON of octal address 200
The Cross Assembler also allows assembly time evaluation of
arithmetic expressions up to thirty-two levels of embedded
parentheses. All calculations use integer numbers and are done
in sixteen bit precision.
OPERATOR SYMBOL OPERATION
--------------- -----------------------+ Addition
Unary positive
- Subtraction
Unary negation (2's complement)
* Multiplication
/ Integer division (no remainder)
MOD Modulus (remainder of integer division)
SHR Shift right
SHL Shift left
NOT Logical negation (1's complement)
AND Logical and
OR Inclusive or

XOR Exclusive or
LOW Low order 8-bits
HIGH High order 8-bits
EQ, = Relational equal
NE, <> Relational not equal
GT, > Relational greater than
GE, >= Relational greater than or equal
LT, < Relational less than
LE, <= Relational less than or equal
( ) Parenthetical statement
Table 2-5: Assembly Time Operations
The relational operators test the specified values and return
either a True or False. False is represented by a zero value,
True is represented by a non zero value (the True condition
actually returns a 16-bit value with every bit set; i.e.,
0FFFFH). The relational operators are used primarily with the
Conditional Assembly capability of the Cross Assembler.
Table 2-5 lists the operations available while Table 2-6 lists
the operations precedence in descending order. Operations with
higher precedence are done first. Operations with equal
precedence are evaluated from left to right.
OPERATION PRECEDENCE
--------- ---------(,) HIGHEST
HIGH,LOW
*,/,MOD,SHR,SHL

+,EQ,LT,GT,LE,GE,NE,=,<,>,<=,>=,<>
NOT
AND
OR,XOR LOWEST
Table 2-6: Operators Precedence
The following are examples of all the available operations and
their result:
HIGH(0AADDH) will return a result of 0AAH
LOW(0AADDH) will return a result of 0DDH
7*4 will return a result of 28
7/4 will return a result of 1
7 MOD 4 will return a result of 3
1000B SHR 2 will return a result of 0010B
1010B SHL 2 will return a result of 101000B
10+5 will return a result of 15
+72 will return a result of 72
25-17 will return a result of 8
-1 will return a result of 1111111111111111B
NOT 1 will return a result of 1111111111111110B
7 EQ 4, 7 = 4 will return a result of 0
7 LT 4, 7 < 4 will return a result of 0
7 GT 4, 7 > 4 will return a result of 0FFFFH
7 LE 4, 7 <= 4 will return a result of 0
7 GE 4, 7 >= 4 will return a result of 0FFFFH
7 NE 4, 7 <> 4 will return a result of 0FFFFH

1101B AND 0101B will return a result of 0101B


1101B OR 0101B will return a result of 1101B
1101B XOR 0101B will return a result of 1000B

Classic 8051
Home Architecture Overview Memory Classes and Layout Classic 8051

The following table shows the memory classes used for programming the classic 8051
architecture. These memory classes are available when you are using the A51 macro
assembler and the BL51 linker/locater.
Memory
Class

Address Range

Description

DATA

D:00 D:7F

Direct addressable on chip RAM.

BIT

D:20 D:2F

bit addressable RAM; accessed bit instructions.

IDATA

I:00 I:FF

Indirect addressable on chip RAM; can be


accessed with @R0 or @R1.

XDATA

X:0000 X:FFFF

64 KB RAM (read/write access). Accessed with


MOVX instruction.

CODE

C:0000 C:FFFF

64 KB ROM (only read access possible). Used for


executable code or constants.

BANK 0
BANK 31

B0:0000 B0:FFFF
B31:0000
B31:FFFF

Code Banks for expanding the program code space


to 32 x 64KB ROM.

Note

The memory prefix D: I: X: C: B0: .. B31: cannot be used at Ax51 assembler or


BL51 linker/locater level. The memory prefixes are only listed for better understanding.
Several Debugging tools, for example the Vision Debugger, are using memory prefixes
to identify the memory class of the address.

Classic 8051 Memory Layout


Home Architecture Overview Memory Classes and Layout Classic 8051 Memory Layout

The classic 8051 memory layout, shown in the following figure, is familiar to 8051 users the
world over.
The classic 8051 architecture includes the following memory classes.

BIT, DATA, and IDATA


The BIT, DATA, and IDATA memory classes comprise the on-chip memory of the 8051
architecture and are shown in the following figure.

CODE and CONST


The CODE and CONST memory classes are typically stored in ROM that may be either offchip or on-chip. The CODE memory class is used for the actual program code while the
CONST memory class is used for constant variables declared in code space.

In addition, up to 32 code banks may be used for code banking applications. While these are
technically located in code space, they are assigned their own address space.

XDATA and PDATA


The XDATA memory class is typically stored in RAM that may be off-chip or on-chip (on
newer devices). It contains external program data (named because it was off-chip on the
original 8051 devices) including variables declared with the xdata and pdata memory
types.
The PDATA memory class is a 256-byte page within the XDATA
memory class.

Extended 8051 Variants


Home Architecture Overview Memory Classes and Layout Extended 8051 Variants

Several new variants of the 8051 extend the code and/or xdata space of the classic 8051
with address extension registers.
The following table shows the memory classes used for programming the extended 8051
devices. These memory classes are available for classic 8051 devices when you are using
memory banking with the LX51 linker/locater. In addition to the code banking known from
the BL51 linker/locater, the LX51 linker/locator supports also data banking for xdata and
code areas with standard 8051 devices.
Memory
Class

Address Range

Description

DATA

D:00 D:7F

Direct addressable on chip RAM.

BIT

D:20 D:2F

bit addressable RAM; accessed bit instructions.

IDATA

I:00 I:FF

Indirect addressable on chip RAM; can be


accessed with @R0 or @R1.

XDATA

X:0000 X:FFFF

64 KB RAM (read/write access). Accessed with


MOVX instruction.

HDATA

X:0000 X:FFFFFF

16 MB RAM (read/write access). Accessed with


MOVX instruction and extended DPTR.

CODE

C:0000 C:FFFF

64 KB ROM (only read access possible). Used for


executable code or constants.

ECODE

C:0000 C:FFFFFF

16 MB ROM (only read access possible). Used for


constants. In some modes of the Dallas 390
architecture also program execution is possible. .

BANK 0
BANK 31

B0:0000 B0:FFFF
B31:0000
B31:FFFF

Code Banks for expanding the program code space


to 32 x 64KB ROM.

Note

The memory prefixes D: I: X: C: B0: .. B31: cannot be used at Ax51 assembler


level. The memory prefix is only listed for better understanding. The Lx51 linker/locater
and several Debugging tools, for example the Vision2 Debugger, are using memory
prefixes to identify the memory class of the address.

If you are using the Dallas 390 contiguous mode the address space for CODE can be
C:0000 - C:0xFFFFFF.

Extended 8051 Memory Layout


Home Architecture Overview Memory Classes and Layout Extended 8051 Memory Layout

The extended 8051 8051 architecture includes the following memory classes.

BIT, DATA, and IDATA


The BIT, DATA, and IDATA memory classes comprise the on-chip memory of the 8051
architecture and are shown in the following figure.

CODE, ECODE, and Memory Banks


The CODE and ECODE memory classes are typically stored in ROM that may be either offchip or on-chip. The CODE memory class may be used for program code and refers to the
first 64K (or Bank 0) of this read-only memory area.
The ECODE memory class is used for constant variables declared in code space above the

first 64K of ROM.


In several variants the DPTR register is expanded to a 24-bit register with an DPX SFR. For
example, the Dallas/Maxim 390 provides new operating modes where this addressing is
enabled. You may even use the HCONST and HDATA memory classes with classic 8051
devices by using the memory banking available in LX51.
Bank 0 through Bank 63 allow your program to expand up to 4 Mbytes when using
memory banking.

XDATA and HDATA


The XDATA memory class refers to the first 64K of RAM which may be off-chip or on-chip.
The HDATA memory class is used when RAM memory requirements exceed 64K bytes. This
memory class allows you to define up to 16 Mbytes
of variable data.

B. Reserved Symbols
Home Appendix B. Reserved Symbols

The Ax51 assembler uses predefined or reserved symbols that may not be redefined in your
program. Reserved symbol names include instruction mnemonics, directives, operators, and
register names. The following lists the reserved symbol names that are found in all Ax51
variants:

DPTR

JNE

R1

AB

DS

JNZ

R2

ACALL

DSEG

JSG

R3

ADD

DW

JSGE

R4

ADDC

ELSE

JSL

R5

AJMP

ELSEIF

JSLE

R6

AND

END

JZ

R7

ANL

ENDIF

LCALL

REPT

AR0

ENDM

LE

RET

AR1

ENDP

LJMP

RETIv

AR2

EQ

LOCAL

RL

AR3

EQU

LOW

RLC

AR4

EXITM

LT

RR

AR5

EXTRN

MACRO

RRC

AR6

GE

MOD

RSEG

AR7

GT

MOV

SEG

BIT

HIGH

MOVC

SEGMENT

IDATA

MOVX

SET

BITADDRESSAB

LE

IF

MUL

SETB

BLOCK

NAME

BSEG

INBLOCK

SHL

INC

NE

SHR

NOP

CALL

INPAGE

SJMP

INSEG

NOT

SUB

CJNE

NUL

CLR

IRP

SUBB

IRPC

NUMBER

SWAP

CMP

OR

CODE

ISEG

UNIT

JB

ORG

USING

CPL

ORL

CSEG

JBC

XCH

JC

OVERLAYABLE

XCHD

DA

PAGE

DATA

JE

XDATA

JG

PC

XOR

DB

POP

DBIT

JLE

XRL

JMP

PUBLIC

XSEG

DEC

JNB

PUSH

DIV

JNC

R0

DJNZ

The A51 assembler defines the following additional reserved symbols which are special
function registers (SFR) of the classic 8051 MCU. These SFR definitions may be disabled
using the NOMOD51 control statement. The predefined SFR symbols are reserved symbols
and may not be redefined in your program.

AC

INT0

RB8

TF0

ACCv

INT1

RD

TF1

IT0

REN

TH0

CY

IT1

RI

TH1

DPH

OV

RS0

TI

DPL

RS1

TL0

EA

P0

RXD

TL1

ES

P1

SBUF

TMOD

ET0

P2

SCON

TO

ET1

P3

SM0

TR0

EX0

PS

SM1

TR1

EX1

PSW

SM2

TXD

F0

PT0

SP

WR

IE

PT1

T1

IE0

PX0

TB8

IE1

PX1

TCON

The AX51 assembler defines the following additional reserved symbols which comprise the
additional instructions and registers of the Philips 80C51MX architecture.

AT

DSD

ERET

NEAR

BYTE

DSW

EVEN

OFFS

BYTE0

DWORD

EXTERN

PR0

BYTE1

ECALL

FAR

PR1

BYTE2

ECODE

HCONST

PROC

BYTE3

EDATA

HDATA

WORD

CONST

EJMP

LABEL

WORD0

DD

EMOV

LIT

WORD2

DSB

EPTR

MBYTE

A51: ADDRESSING BITS AND BYTES


Information in this support solution applies to:

A51 Version 5.50

QUESTION
How do I address a bit-addressable byte as both bits and a byte in assembly?

ANSWER
Here is the code that shows the code for addressing bit addressable internal memory as bits
and bytes
NAMETEST
?PR?main?TESTSEGMENTCODE
?BA?TESTSEGMENTDATABITADDRESSABLE
EXTRNCODE(?C_STARTUP)
PUBLICbit0
PUBLICbyte
PUBLICmain
RSEG?BA?TEST
byte:DS1
bit0EQU(byte+0).0
RSEG?PR?main?TEST
USING0
main:
SETBbit0
?C0001:
SJMP?C0001
RET
END

MORE INFORMATION
To add other bits just duplicate the EQU to represent the remaining locations.

SEE ALSO

A51: BIT-ADDRESSABLE DATA VARIABLES


A51: ERROR A17 (INVALID BYTE BASE IN BIT ADDRESS EXPRESSION)

Vous aimerez peut-être aussi