8051 Assembly Language Programming
8051 Assembly Language Programming
1
OBJECTIVES
List the registers of the 8051 microcontroller
Manipulate data using the registers and MOV instructions
Code simple 8051 Assembly language instructions
Assemble and run an 8051 program
Describe the sequence of events that occur upon 8051 power-up
Examine programs in ROM code of the 8051
Explain the ROM memory map of the 8051
Detail the execution of 8051 Assembly language instructions
Describe 8051 data types
Explain the purpose of the PSW (program status word) register
Discuss RAM memory space allocation in the 8051
Diagram the use of the stack in the 8051
2
SECTION 2.1: INSIDE THE 8051
Registers
8 Bit Registers
Figure 21a
Some 8-bit Registers of the 8051
3
16 Bit Registers
4
Most widely used registers are A, B, R0, R1, R2, R3, R4, R5,
R6, R7, DPTR and PC
All registers are 8-bits, except DPTR and the program
counter which are 16 bit
Register A is used for all arithmetic and logic instructions
5
MOV instruction
MOV destination, source
MOV instruction is used to copy source to
destination
MOV A,#55H ;load value 55H into reg A
MOV R0,A ;copy contents of A into R0 (A=R0=55H)
MOV R1,A ;copy contents of A into R1 (A=R0=R1=55H)
MOV R2,A ;copy contents of A into R2 (A=R0=R1=R2=55H)
MOV R3,#95H ;load value 95H into R3 (R3=95H)
MOV A,R3 ;copy contents of R3 into A (A=R3=95H)
6
MOV notes
Values can be loaded directly into any of register A, B, or R0-
R7
To indicate immediate value it must be preceded with a pound
sign (#)
MOV R0,#23H
If values 0 to F are moved into any of 8 bit register, the rest of
the bits are assumed to be zero
In MOV A,#5, result will be A=05 or 00000101 in binary
Moving a value that is too large into a register will cause an
error
MOV A,#7F2H ; Illegal
When a value without pound sign is loaded to register, it
means the value is loaded from a memory location (RAM)
MOV A,17H ;load a value from memory location (address) 17H
7
ADD instruction
ADD A, source
8
Can also be written:
9
SECTION 2.2: INTRODUCTION TO 8051 ASSEMBLY
PROGRAMMING
Structure of Assembly language
11
SECTION 2.3: ASSEMBLING AND RUNNING
AN 8051 PROGRAM
14
Example of ROM contents
Pg 47
15
ROM memory map in the 8051 family
Different size of ROM means different ending
address
17
Example: Make Data in ROM
18
Example: Make constant data
19
SECTION 2.5: 8051 DATA TYPES AND
DIRECTIVES
Rules for labels in Assembly language
each label name must be unique
first character must be alphabetic
reserved words must not be used as labels
20
SECTION 2.6: 8051 FLAG BITS AND THE
PSW REGISTER
PSW (program status word) register
21
Figure 24 Bits of the PSW Register
ADD instruction and the PSW
Table 21
Instructions That
Affect Flag Bits
22
Example
23
24
25
SECTION 2.7: 8051 REGISTER BANKS AND
STACK
RAM memory space allocation in the 8051
128 bytes: address 00H-7FH
Figure 25
RAM Allocation in the 8051
26
SECTION 2.7: 8051 REGISTER BANKS AND
STACK
Register banks in the 8051
28
Example 2.6 and 2.7
29
30
SECTION 2.7: 8051 REGISTER BANKS AND
STACK
Stack in the 8051
section of RAM used to store information temporarily
could be data or an address
CPU needs this storage area since there are only a
limited number of registers
PUSH put into stack
SP points to last address used
SP is incremented
POP getting out from stack
SP is decremented
31
PUSH operation
32
POP operation
33
34
Reference
The 8051 Microcontroller and Embedded Systems Using
Assembly and C, 2nd ed., Mazidi, Prentice Hall, 2006
35