Sie sind auf Seite 1von 31

Assembler Operation

 Numerous assemblers and utilities (simulator, editors, IDE


etc) are available for the application development for the
8051 microcontroller.
 However, we will only focus on ASM51, which is Intel‟s
original MCS-51™ family assembler.
 ASM51 is known as a cross assembler as it assembles
machine code for 8051 on host computer with CPU other
than the 8051.
 Since the host computer‟s CPU is not an 8051, the
assembled program may not be executed directly.
 To execute the program, hardware emulation or software
simulation of the target CPU is required.

2007 twhaw
1
continue …
Operation of ASM51 1. Find the values of labels and symbols
Start and placed them in a symbol table.

ASM files 2. A “location counter” is used to calculate


the address of each instruction.
1st pass 3. As the file is scanned, the counter is
incremented by the length of each
instruction (Starting from 0000H).

nd 4. If a label is found, it is placed in the


2 pass
symbol table along with the current value
OBJ files, of the counter.
LST files 5. Symbols are also placed in the symbol
End table along with their values.
2007 twhaw
2
continue ...
Operation of ASM51
Start
6. Actual code conversion takes place.
ASM files
7. The object (.obj) and listing files (.lst)
st
1 pass are created.

8. The object file contains the binary


codes of the machine language program.
2nd pass

OBJ files, 9. The listing files contains ASCII text


LST files codes for both the source program and the
hexadecimal bytes of the machine code.
End
2007 twhaw
3
Assembly Language Program Format
 An assembly language programs may consist of:
;This is a sample program Comments: enhance the readability
$TITLE(***Sample***) of assembly programs by
$DATE() annotating the purpose and
PARA EQU 22H operation of instruction sequence.
ORG 0000H
Assembler controls:
MAIN: MOV IE, #82H
set the format of the listing and
MOV TMOD, #12H
object files by regulating the
MOV TH0, #-71
action of ASM51
SETB TR0
. Assembler directives: instructions
. to the assembler program that
. define program structure, symbols,
END data, constants etc

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

 A symbol (or label) must begin with a letter, a question mark,


or underscore (_); must be followed by letters, digits, “?” or
“_” and can contain up to 31 characters

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

 The assembler will determine the correct code address and


insert into the instruction the correct 8-bit offset, 11-bit
page address or 16-bit long address, depending on the
addressing mode.

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.

 Note that a numeric digit must be the first character for


hexadecimal constants in order to differentiate them from
symbols [use “0A5H” instead of “A5H”]

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)

 For eg., the following 2 instructions are the same:


MOV A, #10 + 10H
MOV A, #1AH

 The following 2 instructions are also the same:


MOV A, #25 MOD 7
MOV A, #4

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)

 The operation is applied on the corresponding bits in each


operand.
 For eg., the following 2 instruction are the same:
MOV A, #‘9’ AND 0FH ; ASCII code of ‘9’ -> 39H
MOV A, #9

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

 For eg., the following two instructions are the same:


MOV P1, #0FFH XOR (1 SHL 4)
; (0000 00010001 0000) 1111 11111110 1111
MOV P1, #0EFH ; clear bit 4 of Port 1
 The following two instructions are also the same:
MOV A, #HIGH 1234H
MOV A, #12H
2007 twhaw
18
continue ...
Relational Operators
 When a relational operator is used between two operands,
the results is either false (00H) or true (FFH).
 The operators are:
Operator Description
EQ = equal
NE <> not equal
LT < less than
LE <= less than or equal to
GT > greater than
GE >= greater than or equal to
 In the following examples, all relational tests are true:
MOV A, #5 = 5
MOV A, #5 NE 4 The assembled instructions
MOV A, #‘X’ LT ‘Z’ are all equal to:
MOV A, #‘X’ >= ‘X’ MOV A, #0FFH
MOV A, #100 GE 50
2007 twhaw
19
Assembly Directives
 Assembler directives are instructions to the assembler
program.
 They are not part of the assembly language instructions
which are translated into machine codes.

ORG (Set original)


 The format for ORG directive is
ORG expression
 This directive is used to define the starting address of the
program or to define the starting address of different
sections of the program.
 For e.g.,
ORG 0100H ; set the starting address of this section to 0100H
.
.
.

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

 The EQU directive assigns a numeric value to a specified


symbol name.
 For e.g.,
N27 EQU 27 ; set N27 to the value 27
CR EQU 0DH ; set CR to 0DH

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
...

Data segment starts at


30H

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

A Sample List File

2007 twhaw
28
MCS-51 MACRO ASSEMBLER CHAP3_1

DOS 7.10 (038-N) MCS-51 MACRO ASSEMBLER, V2.2


OBJECT MODULE PLACED IN CHAP3_1.OBJ
ASSEMBLER INVOKED BY: C:\ASM51\ASM51.EXE CHAP3_1.ASM

LOC OBJ LINE SOURCE

0064 1 CONST EQU 100


0000 2 ORG 0000H
3
4 ; this is a comment line
5
0000 3099FD 6 JNB TI, $
0003 3099FD 7 HERE: JNB TI, HERE ; same as above
8
0006 4464 9 ORL A, #CONST
0008 4464 10 ORL A, #100 ; same as above
11
000A E599 12 MOV A, 99H
000C E599 13 MOV A, SBUF ; same as above
14
000E D297 15 SETB 97H
0010 D297 16 SETB P1.7 ; same as above
17
0012 900502 18 MOV DPTR, #04FFH + 3
0015 900502 19 MOV DPTR, #0502H ; same as above
20
21 END
2007 twhaw
29
MCS-51 MACRO ASSEMBLER CHAP3_2

DOS 7.10 (038-N) MCS-51 MACRO ASSEMBLER, V2.2


OBJECT MODULE PLACED IN CHAP3_2.OBJ
ASSEMBLER INVOKED BY: C:\ASM51\ASM51.EXE CHAP3_2.ASM

LOC OBJ LINE SOURCE

0100 1 ORG 0100H


0100 904142 2 MOV DPTR, #'AB'
0103 904142 3 MOV DPTR, #4142H ; same as above
4
0106 741A 5 MOV A, #10 + 10H
0108 741A 6 MOV A, #1AH ; same as above
7
010A 7404 8 MOV A, #25 MOD 7
010C 7404 9 MOV A, #4 ; same as above
10
010E 7409 11 MOV A, #'9' AND 0FH
0110 7409 12 MOV A, #9 ; same as above
13
0112 7410 14 MOV A, #8 SHL 1
0114 7410 15 MOV A, #10H ; same as above
16
17 END

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

LOC OBJ LINE SOURCE

0000 1 ORG 0000H


2
0000 7412 3 MOV A, #HIGH 1234H
0002 7412 4 MOV A, #12H ; same as above
5
0004 74FF 6 MOV A, #5 = 5
0006 74FF 7 MOV A, #5 NE 4
0008 74FF 8 MOV A, #'X' LT 'Z'
000A 74FF 9 MOV A, #'X' >= 'X'
000C 74FF 10 MOV A, #100 GE 50
000E 74FF 11 MOV A, #0FFH ; same as above
12
0200 13 ORG 0200H
0200 00 14 SQUARE: DB 0, 1, 4, 9, 16, 25
0201 01
0202 04
0203 09
0204 10
0205 19
0206 0041 15 MYDATA: DW 'A', 1234H, 10
0208 1234
020A 000A
16
2007 twhaw
17 END 31

Das könnte Ihnen auch gefallen