Академический Документы
Профессиональный Документы
Культура Документы
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.).
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
comment
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
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.
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
DJNZ
R0, LABEL1
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
;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.
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
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
Program Addresses
Indirect Addresses
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
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
PR0,
PR1
EPTR
Immediate Data
Home Writing Assembly Programs Immediate Data
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
;
;
;
;
;
;
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
VALUE:
SEGMENT DATA
RSEG
?DT?myvar
DS
1
IO_PORT2
VALUE2
DATA
DATA
0A0H
20H
?PR?myprog SEGMENT
RSEG
MOV
ADD
MOV
MOV
ADD
CODE
?PR?myprog
A,IO_PORT2
A,VALUE
VALUE2,A
R1,#VALUE
A,@R1
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
RSEG
SETB
JB
SETB
CLR
:
is_on:
CLR
CLR
CODE
?PR?myprog
GREEN_LED
FLAG,is_on
FLAG
ACC.5
FLAG
GREEN_LED
; P1.2 = 1
; direct memory access to DATA
; reset bit 5 in register A
; P1.2 = 0
IDATA
Home Writing Assembly Programs Memory Access IDATA
?PR?myprog SEGMENT
RSEG
MOV
MOV
INC
CODE
?PR?myprog
R0,#BUFFER
A,@R0
R0
MOV
@R0,A
EDATA
Home Writing Assembly Programs Memory Access EDATA
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
?PR?myprog SEGMENT
RSEG
MOV
:
MOV
MOVX
MOV
MOVX
CODE
?PR?myprog
P2,#HIGH ?PD?myvars
DPTR,#XBUFFER
A,@DPTR
R1,#VAR1
@R1,A
;
;
;
;
; reserve 1 byte
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
?PR?myprog SEGMENT
RSEG
MOV
MOV
MOVC
CODE
?PR?myprog
DPTR,#TABLE
A,#3
A,@A+DPTR
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
ACALL AJMP
LCALL LJMP
ECALL EJMP
CALL JMP
AT
3
ext_int
; an interrupt vector
?PR?myintr SEGMENT
RSEG
ext_int:
JB
INC
flag_OK:
CPL
RETI
?PR?myprog SEGMENT
RSEG
func1:
CALL
loop:
CALL
MOV
JNZ
RET
CODE
?PR?myintr
FLAG,flag_OK
my_var
FLAG
CODE INBLOCK
?PR?myprog
sub_func
my_function
A,my_var
loop
sub_func:
FLAG
R0,#20
my_function
R0,loop1
loop1:
CLR
MOV
CALL
DJNZ
RET
EQU
ECODE 0FF0000H
func1
mylab:
PROC
CALL
CALL
JNB
EJMP
ERET
FAR
func2
my_farfunc
Flag,mylab
Reset
ENDP
func2
PROC
CALL
RET
ENDP
NEAR
my_farfunc
; generates ECALL
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
Decimal
D,d
0-9
O,o,Q,q
0-7
B,b
0-1
Octal
Binary
Examples
1234H 99H 123H 0A0F0H 0FFH
Note
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
Value
'A'
0041h
'AB'
4142h
'a'
0061h
'ab'
6162h
''
'ABC'
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
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
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
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
()
Unary + Unary -
* / MOD
+-
SHL SHR
AND OR XOR
( ) 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
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
absolute + absolute
absolute
absolute + relocatable
relocatable
relocatable + absolute
relocatable
relocatable + relocatable
Note
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
absolute - absolute
absolute
absolute - relocatable
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
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
ORG
USING
Conditional Assembly
Statement
Description
IF
ELSEIF
ELSE
ENDIF
Terminates an IF block.
Memory Initialization
Statement
Description
DB
DD
DW
Memory Reservation
Statement
Description
DBIT
DS
DSB
DSD
DSW1
Procedure Declaration
Statement
Description
ENDP1
LABEL
PROC
Program Linkage
Statement
Description
EXTERN1
EXTRN
NAME
PUBLIC
Segment Control
Statement
Description
BSEG
CSEG
DSEG
ISEG
RSEG
SEGMENT
XSEG
Symbol Definition
Statement
Description
BIT
CODE
DATA
EQU
IDATA
LIT
SBIT
SET
SFR
SFR16
XDATA
Miscellaneous
Statement
Description
__ERROR__
END
Note
1. This directive is available in the AX51 Assembler only.
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
DJNZ
R0, LABEL1
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)
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:
$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
COND
Genera
l
Includes (in the listing file) conditional source lines skipped by the
preprocessor.
DATE
Primar
y
DEBUG
Primar
y
DEFINE
Primar
y
EJECT1
Genera
l
ELSE
Genera
l
ELSEIF
Genera
l
ENDIF
Genera
l
Ends an IF block.
ERRORPRINT
Primar
y
GEN
Genera
l
IF
Genera
l
INCDIR
Primar
y
INCLUDE2
Genera
l
LIST
Genera
l
MACRO
Primar
y
MOD_CONT
Primar
y
MOD_MX51
Primar
y
MOD51
Primar
y
Enables code generation and defines SFRs for classic 8051 devices.
MPL
Primar
y
NOAMAKE
Primar
y
NOCASE
Primar
y
NOCOND
Primar
y
NODEBUG
Primar
y
NOERRORPRINT
Primar
y
NOGEN
Genera
l
NOLINES
Primar
y
NOLIST
Genera
l
NOMACRO
Primar
y
NOMOD51
Primar
y
NOMPL
Primar
y
NOOBJECT
Primar
y
NOPRINT
Primar
y
NOREGISTERBAN
K
Primar
y
NOSYMBOLS
Primar
y
NOSYMLIST
Genera
l
NOXREF
Primar
y
OBJECT
Primar
y
PAGELENGTH
Primar
y
PAGEWIDTH
Primar
y
Primar
y
REGISTERBANK
Primar
y
REGUSE
Genera
l
RESET
Genera
l
RESTORE1
Genera
l
SAVE1
Genera
l
SET
Genera
l
SYMBOLS
Primar
y
SYMLIST
Genera
l
TITLE
Primar
y
XREF
Primar
y
Note
1.
These directives may not be specified on the command line. They may only be specified in the
source file.
2.
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
USING
Conditional Assembly
Statement
Description
IF
ELSEIF
ELSE
ENDIF
Terminates an IF block.
Memory Initialization
Statement
Description
DB
DW
Memory Reservation
Statement
Description
DBIT
DS
Program Linkage
Statement
Description
EXTRN
NAME
PUBLIC
Segment Control
Statement
Description
BSEG
CSEG
DSEG
ISEG
RSEG
SEGMENT
XSEG
Symbol Definition
Statement
Description
BIT
CODE
DATA
EQU
IDATA
SBIT
SET
SFR
SFR16
XDATA
Miscellaneous
Statement
Description
__ERROR__
END
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.
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 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)
PSW
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
USING
PUSH
1
AR7
DB Assembler Statement
Syntax
Descriptio
n
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
Example
MSG:
TAB:
DB
DB
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
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
Example
GAP:
TIME:
DS
DS
DS
; 16-byte alignment
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
CODE
DATA
IDATA
XDATA
NUMBER
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
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
Example
TABLE:
HERE:
CTAB:
DW
DW
DW
DW
BSEG AT address
Descriptio
n
is a sequential number.
address
See Also
Example
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
is a sequential number.
address
See Also
Example
CSEG
AT 0003h
VECT_0: LJMP
ISR_0
location
CSEG
0x100
CRight: DB
location
AT 0x100
CSET
0x1000
Parity_TAB:
DB
information
DB
DB
AT 1000H
00H
01H
01H
DB
00H
DSEG AT address
Descriptio
n
Example
modulename
is a sequential number.
address
DSEG
TMP_A: DS
TMP_A
TEM_B: DS
TMP_B
AT 0x40
2
4
ISEG AT address
Descriptio
n
is a sequential number.
address
See Also
Example
ISEG AT 0xC0
TMP_IA:
DS
2
with name TMP_IA
TEM_IB:
DS
4
variable with name TMP_IB
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
CODE
DATA
IDATA
XDATA
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
INBLOCK
INPAGE
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
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 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
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