Beruflich Dokumente
Kultur Dokumente
2007 twhaw
1
continue …
Operation of ASM51 1. Find the values of labels and symbols
Start and placed them in a symbol table.
Assembly instructions:
the mnemonics of executable instruction
2007 twhaw
4
continue ...
The lines containing assembly instructions and directives
must be written following specific rules.
Each line is divided into “fields” separated by space or tab
, with the general form of:
[Label:] mnemonic [operand] [,operand] […] [;comment]
Some assemblers require the label field to begin on the left
in Column 1 (leftmost), and subsequent fields to be
separated by space or tab.
Label Field
A label represents the address of the instruction (or data)
that follows.
Labels are mostly used as in the operand field of branch or
jump instruction to specify the destination.
2007 twhaw
5
continue ...
Labels are actually one type of symbol, but they terminate
with a colon (:)
Symbols are assigned values or attributes, using assembler
directives such as EQU.
Symbols may represent an address or data, and they do not
terminate with a colon.
Example
PAR EQU 500 ; PAR is a symbol which represents the value 500
START: MOVA, #0FFH ; START is a label which represents the address of
; the MOV instruction
2007 twhaw
6
continue ...
Mnemonic Field
Instruction mnemonics or assembler directives go into
mnemonic field, which follows the label field.
Operand Field
This field contains the address or data used by the
instruction.
A label may be used to represent the address of the data, or
a symbol may be used to represent a data constant.
Some instructions have no operand (eg., RET), while
others may have multiple operands separated by commas.
2007 twhaw
7
continue ...
Comment Field
Annotations to clarify the program go into comment filed
at the end of each line.
Comment must begin with a semicolon (;).
Entire line can be a comment line by preceding it with a
semicolon.
; The following instruction move 20H to accumulator
MOV A, #20H ; move 20H to accumulator
2007 twhaw
8
continue ...
Special Assembler Symbols
Special assembler symbols may be used for register-
specific modes.
These include A, R0 through R7, DPTR, PC, C and AB.
In addition, a dollar sign ($) can be used to refer to the
current value of the location counter, e.g.:
JNB TI, $
HERE: JNB TI, HERE ; same as above
Indirect Address
The commercial “at” sign (@) indicates indirect
addressing and may only be used with R0, R1, DPTR OR
PC, depending on the instruction.
ADD A, @R0
MOVX @DPTR, A
2007 twhaw
9
continue ...
Immediate Data
As for immediate data, they are preceded by a hash sign, #:
CONST EQU 100
MOV A, #0FEH
ORL A, #CONST
Direct Address
Direct address may be specified in the operand field.
For some of the SFR addresses, predefined symbol may be
used:
MOV A, 99H
MOV A, SBUF ; same as above
2007 twhaw
10
continue ...
Bit Address
Bit location can be specified in 3 ways:
i. Giving the actual bit address.
ii. Using the dot operator between the byte address and
the bit position.
iii. Using a predefined assembler symbol.
Example 1:
SETB 97H ; specifying bit address
SETB P1.7 ; using dot operator (same as above)
Example 2
JNB TI, $ ; using predefined symbol
JNB 99H, $ ; specifying bit address (same as above)
2007 twhaw
11
continue ...
Code Address
Code address is used in the operand field for jump
instructions.
The code address is usually given in the form of a label.
Example:
HERE: .
.
.
SJMP HERE
2007 twhaw
12
Assemble-time Expression Evaluation
Values and data in the operand field may be expressed in 3
ways:
i. Explicitly (e.g., 0EFH).
ii. Using a predefined symbol.
iii. Using an expression (e.g., 2 + 3).
When expression is used, the assembler calculates the
actual value and insert it into the instruction.
Example:
MOV DPTR, #04FFH + 3
MOV DPTR, #0502H ; same as above
2007 twhaw
13
continue ...
Number Base
This number base can be changed by specifying the
number follow by:
i. “B” for binary.
ii. “O” or “Q” for octal.
iii. “D” or nothing for decimal (default).
iv. “H” for hexadecimal.
2007 twhaw
14
continue ...
Number Base
String using one or two characters may be used as
operands in expressions.
The ASCII codes are converted to binary equivalent by the
assembler.
Character constants are enclosed in a single quote („‟).
Example:
MOV DPTR, #‘AB’
MOV DPTR, #4142H ; same as above
2007 twhaw
15
continue ...
Arithmetic Operators
The operators supported by the assembler are:
Operator Description
+ addition
- subtraction
* multiplication
/ division
MOD modulo (remainder after division)
2007 twhaw
16
continue ...
Logical Operators
The logical operators are:
Operator Description
OR logical OR
AND logical AND
XOR logical XOR
NOT logical NOT (complement)
2007 twhaw
17
continue ...
Special Operators
The special operators are:
Operator Description
SHR shift right
SHL shift left
HIGH high-byte
LOW low-byte
() evaluate first
2007 twhaw
20
continue ...
END
The END directive should be the last statement in the
source file.
EQU
The format for this directive is
symbol EQU expression
2007 twhaw
21
continue ...
DB (Define Byte)
The format for this directive is:
[Label:] DB expression [, expression] […] [;comment]
Each data item is assigned to one memory location in the
ROM (1 byte).
If a label is used, it is assigned the address of the first byte.
For e.g.,
ORG 0100H
SQUARE: DB 0, 1, 4, 9, 16, 25
; Corresponding memory assignment in the ROM:
Address Contents
0100 00 0
0101 01 1
0102 04 4
0103 09 9
0104 10 16
0105 19 25 22
2007 twhaw
continue ...
DW (Define Word)
The format for this directive is:
[Label:] DW expression [, expression] […] [;comment]
This directive is the same as DB directive except that two
memory locations (2 bytes) are assigned for each data item.
For e.g.,
ORG 0100H
MYDATA: DW ‘A’, 1234H, 10
; Corresponding memory assignment in the ROM:
Address Contents
0100 00
‘A’
0101 41
0102 12
1234
0103 34
0104 00
10
0105 0A
2007 twhaw
23
continue ...
Segments
ASM51 defines the memory spaces in the 8051 into
different segments:
i. Code space [CSEG].
ii. Internal data space (00H - 7FH) [DSEG].
iii. External data space [XSEG].
iv. Entire internal space addressable by indirect addressing
[ISEG].
v. Bit addressable space (20H - 2FH) [BSEG].
The default segment is the absolute code segment, which
start at 0000H in the code memory.
A segment can be either absolute or relocatable.
For absolute segments, the starting addresses of the
segments are specified (each segment has an absolute
starting address, e.g., CSEG starts at 0000H).
2007 twhaw
24
continue ...
For relocatable segments, each segment is given symbolic
name and the addresses for these segments can be defined
later (relocatable).
Relocatable segments are used in “modular programming”,
where smaller modules (usually subroutine) are combined
to form a larger program.
A program known as linker or relocator is used to combine
the different modules into one absolute object module that
can be executed.
If each module uses absolute segments, then it would be
difficult to combine the modules together as there might be
conflict between the addresses of the modules.
By using relocatable segments, the same segments (with
the same symbolic name) from different modules can be
combined together using a “linker”.
2007 twhaw
25
continue ...
For e.g., the code segments from different modules are
combined and an absolute starting address is assigned to
the final combined code segment.
A program using absolute segments is shown below:
Using absolute segments mC
Program ROM
Code segment starts at
code segment (0000H) 0000H
...
assembler
data segment (30H)
RAM
...
2007 twhaw
26
continue ...
For modular programs,
Using relocatable segments
Module 1
code segment
mC
...
ROM
assembler
data segment Code segment of
module 1
...
Code segment of
module 2
Module 2 Code segment of
Obj 1 module 3
code segment
...
Obj 2
assembler Linker
data segment
...
Obj 3 RAM
Data segment of
Module 3 module 1
Data segment of
code segment
module 2
...
Data segment of
assembler module 3
data segment
...
2007 twhaw
27
Appendix
2007 twhaw
28
MCS-51 MACRO ASSEMBLER CHAP3_1
2007 twhaw
30
DOS 7.10 (038-N) MCS-51 MACRO ASSEMBLER, V2.2
OBJECT MODULE PLACED IN CHAP3_3.OBJ
ASSEMBLER INVOKED BY: C:\ASM51\ASM51.EXE CHAP3_3.ASM