Professional Documents
Culture Documents
Lecture 5
8051 Assembly Language Programming (1)
Vocational Training Council, Hong Kong.
Week 5
In this Lecture
8051 programming model
Assembly language syntax
Operation codes and operands
Machine instructions
How 8051 interprets binary data
Example of Assembly language program
8051 Instruction Set
Instruction time calculation
Data movement instructions
Vocational Training Council, Hong Kong.
Week 5
Introduction
You are now learning to be a programmer
(a person who writes programs)
You should know the internal structure of
the 8051 to do good programming
Programming model how programmers
see the 8051
In the model, programmers can access the
registers and accumulators with programs
Week 5
7F
Week 5
Week 5
Week 5
Operand ()
Apart from Opcode, an instruction also includes
object) to act on.
The object is called an operand.
Operand is optional. Instructions can have one,
two or no operands.
e.g. MOV R0, #12h --- R0 and #12h are two
operands
INC R1 --- R1 is the only one operand
NOP --- no operand follows
Vocational Training Council, Hong Kong.
Week 5
Week 5
Week 5
10
11
SOURCE
COUNT EQU 25H
ORG
0H
MOV
R5, COUNT
MOV
R7, #34H
MOV
A, #0
ADD
A, R5
ADD
A, R7
ADD
A, #12H
NOP
HERE:
SJMP HERE
ORG
20H
DATA1:
DB 39H
DATA2:
DB "America
END
ATTRIBUTES
A
0020H
A
0021H
A
000BH
A
;COUNT = 25H
;start (origin) at location 0
;load 25H into R5
;load 34H into R7
;load 0 into A
;add contents of R5 to A, now A = A + R5
;add contents of R7 to A, now A = A + R7
;add to A value 12H, now A = A + 12H
;no operation
;stay in this loop
;
;
;end of assembly source file
Week 5
12
Pseudo-instructions/Directives
Beside mnemonics, directives are used to define variables and
memory locations where the machine codes are stored. These
directives are interpreted by assembler during the conversion of
the assembly language program into machine codes.
- ORG (origin)
indicates the beginning of the address of the instructions.
The number that comes after ORG can be either hex or
decimal.
- END
indicates to the assembler the end of the source assembly
instructions.
Vocational Training Council, Hong Kong.
Week 5
13
Pseudo-instructions/Directives
- EQU (equate)
used to define a constant without occupying a memory
location. It does not set aside storage for a data item but
associates a constant value with a data label so that when
the label appears in the program. Its constant value will be
substituted for the label.
- DB (define byte)
used to define 8-bit data and store them in assigned
memory locations. Define data can be in decimal, binary,
hex, or ASCII formats.
Vocational Training Council, Hong Kong.
Week 5
14
Week 5
15
Tmachine cycle
1
=
.(12 ) = 1 .085 s
6
11 .0592 10
Week 5
16
Example 5-1
For an 8051 system of 11.0592MHz, find how long it takes
to execute each of the following instructions.
(a) MOV R3, #55
(b) DEC R3
(c) DJNZ R2, target
(d) LJMP
(e) SJMP
(f) NOP
(g) MUL AB
Vocational Training Council, Hong Kong.
Week 5
17
Machine
Time to execute
cycles
1 x 1.085s = 1.085 s
(b) DEC R3
1 x 1.085s = 1.085 s
2 x 1.085s = 2.17 s
(d) LJMP
2 x 1.085s = 2.17 s
(e) SJMP
2 x 1.085s = 2.17 s
(f) NOP
1 x 1.085s = 1.085 s
(g) MUL AB
4 x 1.085s = 4.34 s
Week 5
18
Example 5-2
Find the execution time for the following program segment,
assuming a crystal frequency of 11.0592MHz.
Machine Cycle
ORG
00h
--
MOV
A, #100
MOV
MUL
PUSH
B, #2
AB
01
2
4
1
MOV
PUSH
ADD
MOV
SWAP
A, B
02
A, R1
30h, A
A
2
1
1
2
1
END
Vocational Training Council, Hong Kong.
-Week 5
19
Week 5
20
Week 5
21
destination, source
Week 5
22
MOV 5Ch, A
Week 5
23
destination, source
Week 5
24
MOVX @R0, A
- Copy data from A to the 8-bit address in R0
MOVX A, @R1
- Copy data from the 8-bit address in R1 to A
MOVX A, @DPTR
- Copy data from the 16-bit address in DPTR to A
Vocational Training Council, Hong Kong.
Week 5
25
Week 5
26
Week 5
27
Write
Read
A register
Write
Read
Data
Data
External
RAM
Internal
and
External
ROM
MOVX @Ri
R0 or R1
MOVX @DPTR
DPTR
MOVC A,@A + DPTR
DPTR + A
MOVC A,@A + PC
PC + A
Week 5
28
The Stack
Stack: an area of internal RAM for fast data storage and
retrieval
Stack operation follows the first-in-last-out (FILO), or
equivalently, last-in-first-out (LIFO) logic
(i.e. the stack grows up as data is stored)
Stack pointer (SP): a register that stores the address of
the stacks top item
Adding data to the stack:
SP increments (+1) and then data is stored on the stack
Getting data from the stack:
Data is read from the stack and then SP decrements (1)
Vocational Training Council, Hong Kong.
Week 5
29
source
Week 5
30
destination
Week 5
31
Other Remarks
SP register is set to 07h when the 8051 is reset
To prevent the stack from running out of memory,
programmer needs to initialize the SP to a value above
the highest address likely to be used by the program
This value is usually above the register banks
The first PUSH operation writes data to R0 in bank
1(08h)
SP rolls over to 00h after it reaches FFh
PUSH > 7Fh will result in error as the RAM ends at
address 7Fh
Vocational Training Council, Hong Kong.
Week 5
32
SP + 2
Push Y
Pop Y
SP
SP + 1
Push X
Pop X
SP - 1
SP
Increment Before
PUSHing
SP - 2
Internal
RAM
Decrement After
POPing
Week 5
33
Examples
MOV
81h, #30h
MOV
R0, #0ACh
PUSH
00h
PUSH
00h
POP
01h
POP
80h
Week 5
34
Data Exchanging
XCH and XCHD are data exchange instructions
data movement is bi-directional
(i.e. source destination)
Data exchanging operations are internal to the
8051
All data exchanging operations must use
register A
Week 5
35
destination, source
XCHD
destination, source
Week 5
36
Examples
XCH
A, R7
Exchange bytes between registers A and R7
XCH
A, 0F0h
Exchange bytes between registers A and B
XCH
A, @R1
Exchange bytes between register A and address in R1
XCHD A, @R1
Exchange lower-nibble in register A and the address
in R1
Vocational Training Council, Hong Kong.
Week 5
37
Read reference
The 8051 Microcontroller and Embedded Systems Using Assembly and C, Mazidi
Chapter 2
P.37 P.63
Week 5
38
END of Lecture 5
8051 Assembly Language Programming (1)
Vocational Training Council, Hong Kong.
Week 5