Professional Documents
Culture Documents
Sections
2.1 2.2 2.3 2.4 2.5 2.6 2.7 Inside the 8051 Introduction to 8051 Assembly Programming Assembling and Running an 8051 Program The Program Counter and ROM Space in the 8051 Data Types and Directives 8051 Flag Bits and the PSW Register 8051 Register Banks and Stack
Objective
8051
8051 8051 8051 8051 8051 ROM ROM 8051 8051 RAM
Registers
Register are used to store information temporarily. The 8051 has 8-bit registers and 16-bit registers.
a lot of 8-bit registers Figure 2-1 (a) two 16-bit registers Figure 2-1(b)
B
R0 R1 R2 R3 R4 R5 R6
R7
DPH
DPL
8
PC
PC (program counter)
A 8-bit Register
The 8051 use 8-bit data type.
Exampleinteger and character are 8 bits.
Any data larger than 8-bits must be broken into 8-bit chunks before it is processed.
D7
D6
D5
D4
D3
D2
D1
D0
2. Arithmetic Operations
ADD, SUB, INC, DEC, MUL, DIV
3. Logical Operations
ANL, ORL, XRL, CLR
MOV1/3
Copy the source operand to the destination operand. MOV destination, source
copy
MOV
;load value 55H into reg. A ;now A=55H MOV R6,#12 ;load 12 decimal into R6 ;now R6=12=0CH MOV R0,A ;copy contents of A into R0 ;now A=55H, R0=55H The pound sign # indicate that it is an immediate value. You can write your command after the semicolon ;.
A,#55H
11
MOV2/3
Others examples
MOV R5,#0F9H ;load F9H into R5 ;now R5=F9H
There is a need for 0 between the # and F to indicate that F is a hex number and not a letter.
MOV MOV MOV R5,#F9H R5,#425 A,17H ;illegal ;illegal ;load the value held in memory ;location 17H to reg. A
12
The value must be within 0-0FFH (or decimal 0-255). If no # exists, it means to load from a memory location.
MOV3/3
Others examples
MOV ;load ASCII 4 into A ;now A=34H The immediate value can be copied to A, B, R0-R7. A,#4
13
ADD1/2
Add the source operand to register A and put the result in A. ADD A, source
A + source A
A,#25H ;load 25H into A R2,#34H ;load 34H into R2 A,R2 ;add R2 to AA=A+R2 ;now A=59H, R2=34H
R0,A ;illegal
14
ADD2/2
Other examples
MOV ADD A,#25H A,#34H ;load 25H into A ;add 34H to AA=A+34H=59H
The second value is called an immediate operand. The format for Assembly language instruction, descriptions of their use, and a listing of legal operand types are provide in Appendix A.1. (discuss in Chap 5)
15
16
Program Languages
Machine language
a program that consists of 0s and 1s. CPU can work on machine language directly. Example 7D25
Low-level language
It deals directly with the internal structure of the CPU. Programmers must know all details of the CPU. Example MOV R5,#25H8051 assembly language
High-level language
Machine independent Example a=37;C++
17
Assembly Language
Assembly languages were developed which provided mnemonics for the machine code instructions, plus other features.
Mnemonicthe instruction
ExampleMOV, ADD
Provide decimal number, named registers, label, command programming faster and less prone to error.
Assembly language programs must be translated into machine code by a program called an assembler.
18
Assembler
Assembler
a software program can translate an Assembly language program into machine code. Source program Object program, opcode, object code
Assembler
21
ORG END
ORG tells the assembler to place the opcode at ROM with a chosen start address. ORG start-address
ORG 0200H ;put the following codes ;start at location 200H
END indicates to the assembler the end of the source code. END
END ;end of asm source file
22
24
25
myfile.asm
ASSEMBLER PROGRAM myfile.lst myfile.obj LINKER PROGRAM myfile.abs OH PROGRAM
26
myfile.hex
myfile.a51
ORG MOV MOV MOV ADD ADD ADD 0H R5,#25H R7,#34H A,#0 A,R5 A,R7 A,#12H ;start 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 ;stay in this loop ;end of asm source file
27
Myfile.lst
Intel-8051 Macro Assembler - Version 1.00 1 2 3 4 5 6 7 8 9 10 11 12 0000 0000 0002 0004 0006 0007 0007 0008 0008 000A 000A 000A ORG MOV MOV MOV ADD ADD ADD 0H R5,#25H R7,#34H A,#0 A,R5 A,R7 A,#12H ;start 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 ;stay in this loop ;end of asm source file
28
List File
The list file myfile.lst lists
line number, ROM address for this opcode, the opcode, instruction and command.
1 2 3 4 5 6 7 8 9 10 11 12 0000 0000 0002 0004 0006 0007 0007 0008 0008 000A 000A 000A
ADD
ADD
A,R7
A,#12H
;start 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 ;stay in this loop ;end of asm source file
29
Linking
When we write a large program, we may partition the job into several little programs. These little programs are assembled separately by different programmers. Finally, link them together and produce an absolute program with an absolute addressing.
a2.obj
a1.obj
main
a3.obj
a1.abs
30
Myfile.obj
:0A0000007D257F3474002D2F24129B
start address length opcode
Except opcodes, all other bytes are used to tell the loader how to load the program to ROM.
31
Code
7D 25 7F 34 74
0005
0006 0007 0008 0009
00
2D 2F 24 12
32
Section 2.4 The Program Counter and ROM Space in the 8051
33
Program Counter1/2
The Program CounterPC points to the address of the next instruction to be executed. As the CPU fetches the opcode from the program ROM, the program counter is incremented to point to the next instruction. PC is called instruction pointer, too.
PC F E D C B A 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
16-bit register
0000H 0001H 0002H FFFEH FFFFH 10000H addresses
34
Program Counter2/2
The PC in the 8051 is 16-bits wide.
The 8051 can access program addresses 0000 to FFFFH, a total of 64K bytes of code.
10000H (in hex) 24*4 216 26 210 = 64 K
The exact range of program addresses depends on the size of on-chip ROM.
When the 8051 is powered up, the PC has the value of 0000 in it.
That is, the address of the first executed opcode at ROM address is 0000H.
ROM Address
Machine Language
Assembly Language
0000
0002
7D25
2 byte opcode
MOV R5,#25H
MOV R7,#34H
2D 2F 2412
Program is form 0000 to 0009. Total 10 bytes. 000A is the address of the next instruction if exists.
36
Notes
The pseudo instructions ORG END are not translated into opcodes. Since ORG 0H, the program will be burned into the program ROM with the start address 0000H. The opcodes can be found in Appendix H (page 225,2-26, 2-27). (discuss in Chap.5)
37
38
Example 2-1
Find the ROM memory address of the following 8051 chips. (a) AT89C51 (or 8751) with 4KB (b) DS5000-32 with 32KB Solution: (a) 4K bytes 4 1024 bytes = 4096 bytes 1000H bytes (or 4K bytes 22+10 bytes 212 bytes = 1000 H bytes) This memory maps to address locations of 0000 to 0FFFH. Notice that 0 is always the first location.
(b) 32K bytes 32 1024 bytes 32,768 bytes (or 32K bytes 25+10 bytes 215 bytes = 8000 H bytes) Converting 32,768 to hex, we get 8000H. Therefore, the memory space is 0000 to 7FFFH.
40
byte 0000
8K = 213 =8192 =2000H
byte 0000
32K = 215 =32768 =8000H
0FFF
8751 AT89C51
1FFF
8752 AT89C52
7FFF
DS5000-32
41
42
Data Type
The 8051 microcontroller has only one data type.
8-bit data 00 to FFH data range (0 to 255 in decimal) Programmers must take care the meaning of data type by themselves. Assembly Language defines some data representations and pseudo instructions.
DB, EQU
DB1/3
Define byte in the program. data-name DB data-value
address
ROM
ORG DATA1: DB 28 ;decimal (1C in hex) DATA2: DB 00110101B;binary (35 in hex) DATA3: DB 39H ;hexadecimal data-name is the label refer to the ROM address saved the content data-value. It is an address, DATA1=0500H, DATA2=0501H.
44
DB2/3
Define ASCII number or characters
ORG DATA1: DB ORG DATA2: DB 510H 2591 518H My name is Joe
......
Assembler translates the ASCII numbers or characters to binary number. ASCII Codes in Appendix F (p401) The label is the address of first content at ROM. You can think them as a table.
4D 79 20 6E 61 6D 65
......
45
DB3/3
The data-label is a 16-bit value.
address
ROM
Usually, we use the register DPTR to point the first content of data.
ORG MOV ... DATA1: DB 0H DPTR,#DATA1 .... 2591
32 35 39 31
......
DATA1=0510H DPTR=0510H
46
EQU
Define a constant without occupying a memory location. data-name EQU data-value
data-value data-name
EQU just associates a constant value with a data label. When the label appears in the program, its constant value will be substituted for the label by assembler.
47
48
Flags
When the CPU perform arithmetic operations, sometimes an exception may occur.
Exampleoverflow
How does the CPU tell programmers that an exception occurs? Answer is the flags.
A flag is a bit to denote an exception occurred. Carry flagCY Auxiliary carry flagAC Parity checkP OverflowOV
49
CYCarry
If there is an carry out from the D7 bit during an operation, CY is set; otherwise CY is cleared.
The CY is used to detect errors in arithmetic operations. FFH+80H=17FH Carry out overflow It is large than the data range of 8-bit register.
D7
D6
D5
D4
D3
D2
D1
D0
CY
AC
50
ACAuxiliary Carry
If there is an carry from D3 to D4 during an operation, AC is set; otherwise AC is cleared.
The AC flag is used by instructions that perform BCD (binary coded decimal) arithmetic. (See Chapter 6) 88+08 = 96 Auxiliary carry out overflow
D7
D6
D5
D4
D3
D2
D1
D0
AC
51
OVOverflow(1/2)
OV is set whenever the result of a signed number operation is too large, causing the high-order bit to overflow into the sign bit. (See Chapter 6)
2s complement method is used in the signed numbers. The range of 8-bit number is 128 to 127 in decimal.
In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs:
1. There is a carry from D6 to D7 but no carry out of D7. 2. There is a carry from D7 out but no carry from D6 to D7.
52
OVOverflow(2/2)
In the following example, there is an carry from D6 to D7 but no carry out of D7. Thus, the overflow bit is set.
40H + 40H = 80 H Overflow the range -80H to 7FH CY=0, OV=1, AC=0
D7
D6
D5
D4
D3
D2
D1
D0
AC
53
PParity Check
The parity flag reflects the number of 1s in the A accumulatorregister only. If A contains an odd number of 1s, then P=1. If A has an even number of 1s, then P=0. Example
A = 0011 0011 # of 1s = 4 P = 0 A = 1011 0011 # of 1s = 5 P = 1
54
Example 2
MOV ADD A,#41H A,#4EH
PSW register
CY
AC
F0
RS1
RS0
OV
--
56
CY CY AC -RS1 RS0 OV -P
F0
RS1
RS0
OV
--
Carry flag. Auxiliary carry flag. Available to the user for general purpose. Register Bank selector bit 1. Register Bank selector bit 0. Overflow flag. User definable bit. Parity flag. Set/cleared by hardware each instruction cycle to indicate an odd/even number of 1 bits in the 57 accumulator.
58
59
Example 2-2
Show the status of the CY, AC, and P flags after the addition of 38H and 2FH in the following instructions.
MOV ADD Solution: 38 0011 1000 A,#38H A,#2FH ;after the addition A=67H,CY=0
+2F
67
0010 1111
0110 0111
CY=0 since there is no carry beyond the D7 bit AC=1 since there is a carry from the D3 to the D4 bit P= 1 since the accumulator has an odd number of 1s (it has five 1s).
60
Example 2-3
Show the status of the CY, AC, and P flags after the addition of 9CH and 64H in the following instructions.
MOV ADD Solution: 9C 1001 1100 A,#9CH A,#64H ;after addition A=00 and CY=1
+64
100
0110 0100
0000 0000
Example 2-4
Show the status of the CY, AC, and P flags after the addition of 88H and 93H in the following instructions.
MOV ADD Solution: 88 1000 1000 A,#88H A,#93H ;after the addition A=1BH,CY=1
+93
11B
1001 0011
0001 1011
63
64
Bit-Addressable RAM
20 1F 18 17 10 0F 08 07
Register Bank 3 Register Bank 2 Register Bank 1 (stack) Register Bank 0 Register Banks and the stack
00
65
R0 to R7
The 8051 uses 8 registers as general register.
They are named as R0,R1,...,R7. They form a register bank.
66
Bank 1
R7 R6 R5 R4 R3 R2 R1 R0 17 16 15 14 13 12 11 10
Bank 2
R7 R6 R5 R4 R3 R2 R1 R0 1F 1E 1D 1C 1B 1A 19 18
Bank 3
R7 R6 R5 R4 R3 R2 R1 R0
67
Register Banks
RS1 and RS0 decide the bank used by R0-R7.
RS1 and RS0 are bits 4 and 3 of PSW register, respectively.
68
Example 2-5
State the contents of RAM locations after the following program: MOV R0,#99H ;load R0 with value 99H MOV R1,#85H ;load R1 with value 85H MOV R2,#3FH ;load R2 with value 3FH MOV R7,#63H ;load R7 with value 63H MOV R5,#12H ;load R5 with value 12H Solution:
RAM location 1 has value 85H RAM location 7 has value 63H
Example 2-6
Repeat Example 2-5 using RAM addresses instead of register names. Solution: This is called direct addressing mode and uses the RAM address location for the destination address. See Chapter5 for a more detailed discussion of addressing modes. MOV MOV MOV MOV MOV 00,#99H 01,#85H 02,#3FH 07,#63H 05,#12H ;load ;load ;load ;load ;load R0 R1 R2 R7 R5 with with with with with value value value value value 99H 85H 3FH 63H 12H
70
Example 2-7
State the contents of the RAM locations after the following program: SETB PSW.4 ;select bank 2 MOV R0,#99H ;load R0 with value 99H MOV R1,#85H ;load R1 with value 85H MOV R2,#3FH ;load R2 with value 3FH MOV R7,#63H ;load R7 with value 63H MOV R5,#12H ;load R5 with value 12H
Solution:
By default, PSW.3=0 PSW.4=0 SETB PSW.4 sets RS1=1 and RS0=0 Register bank 2.
71
Stack
Stacka section of RAM to store data items Two operations on the stack
PUSHput an item onto the top of the stack POPremove an item from the top of the stack
PUSH PUSH PUSH POP POP
73
74
SP
PUSH
Put the content of the RAM-address into the top of the stack. PUSH 6 RAM RAM Addr. PUSH RAM-address Addr. 06 25
MOV R6,#25H PUSH 6
R6 25
0B 0A 09 08 SP=07 0B 0A 09 08 25 SP=08
SP is incremented by 1 automatically. The storing of a CPU register in the stack is called a PUSH.
76
POP
Remove the top value from the stack to the assigned RAM-address. POP 3 RAM RAM 08 25 Addr. POP RAM-address Addr.
POP 3
0B 0B
0A
09 08 25
0A
09 08
SP=08
SP=07
R3
25
SP is decremented by 1 automatically. The loading the contents of the stack back into a CPU register is called a POP.
77
Example 2-8
Show the stack and stack pointer for the following. Assume the default stack area. MOV R6,#25H MOV R1,#12H MOV R4,#0F3H PUSH 6 PUSH 1 PUSH 4 Solution:
RAM Addr.
PUSH
25
RAM Addr.
PUSH RAM
12
PUSH
F3
Addr.
RAM Addr.
0B
0B
0B
0B
0A
09 08
0A
09 08 25
0A
09 08 12 25
0A
09 08
F3 12 25
78
SP=07
SP=08
SP=09
SP=0A
Example 2-9
Examining the stack, show the contents of the registers and SP after execution of the following instructions. All values are in hex. POP 3 ;POP stack into R3 POP 5 ;POP stack into R5 POP 2 ;POP stack into R2 Solution:
After POP 3 0B 0A 54 F9 0B 0A F9 After POP 5 0B 0A After POP 2 0B 0A
09
08
76
6C
09
08
76
6C
09
08
76
6C
09
08 6C
79
Start SP=0B
SP=0A
SP=09
SP=08
SPStack Pointer
SP register is used to access the stack.
When the 8051 is powered upi.e., no data in the stack, the SP register contains value 07H. The locations 20-2HF of RAM are reserved for bitaddressable memory and must not be used by the stack. The stack size is 08H-1FH (24 bytes). If in a given program we need more than 24 bytes of stack, we can change the SP to point to RAM location 30H - 7FH. MOV SP,#5FH ;make RAM location 60H is ;the first stack location
80
Example 2-10
Show the stack and stack pointer for the following instructions. MOV SP,#5FH ;make RAM location 60H ;first stack location MOV R2,#25H MOV R1,#12H MOV R4,#0F3H PUSH 2 PUSH 1 PUSH 4 Solution:
After PUSH 2 After PUSH 1 After PUSH 4
63
62 61 60 Start SP=5F
63
62 61 60 25 SP=60
63
62 61 60 12 25 SP=61
63
62 61 60 F3 12 25 SP=62
81
Instructions References
Appendix A.1 shows the usage of instructions. Table A-1page 356
Mnemonics byteopcode sizemachine cycle Types of mnemonics
Questions
What are the byte, machine cycle and hex code for the following instructions ADD A,R0 ;code=28, byte=1, MC=1 ADD A,R5 ;code=2D, byte=1, MC=1 MOV R0,#0FFH ;code=78, byte=2, MC=1 MOV R5,#0FFH ;code=7D, byte=2, MC=2 PUSH data addr ;code=C0, byte=2, MC=2 POP data addr ;code=D0, byte=2, MC=2
84
Simulator
SIMLAB-8051
Registers, ROM, RAM C 8051 F1 8051
F1
Simlab_8051
85
Simlab_8051
F1
1. 2. 3. 4.
,() ,()
5. ()
Demo
86
Homework
Chapter 2 Problems 7,22,23,25,27,29,37,38,40,42,43,47,48 Note:
Please write and compile the program of Problems 47,48. For the programming problem, please use "8 LED learning board" for the following question.
89