3 Assembly Language Programming
3 Assembly Language Programming
3 Assembly Language Programming
LANGUAGE
PROGRAMMING
Register are used to store information
INSIDE THE
8051
temporarily, while the information
could be
Registers • a byte of data to be processed, or
• an address pointing to the data to be
fetched
The vast majority of 8051 register are
8-bit registers
There is only one data type, 8 bits
D7 D6 D5 D4 D3 D2 D1 D0
8 bit Registers
3
The most widely used registers
INSIDE THE
A (Accumulator)
8051
For all arithmetic and logic instructions
4
MOV destination, source ;copy source to dest.
INSIDE THE The instruction tells the CPU to move (in reality,
8051 COPY) the source operand to the destination
operand
MOV “#” signifies that it is a value
Instruction
MOV A,#55H ;load value 55H into reg. A
MOV R0,A ;copy contents of A into R0
;(now A=R0=55H)
MOV R1,A ;copy contents of A into R1
;(now A=R0=R1=55H)
MOV R2,A ;copy contents of A into R2
;(now A=R0=R1=R2=55H)
MOV R3,#95H ;load value 95H into R3
;(now R3=95H)
MOV A,R3 ;copy contents of R3 into A
;now A=R3=95H
5
Notes on programming
INSIDE THE Value (proceeded with #) can be loaded
8051 directly to registers A, B, or R0 – R7
MOV A, #23H
MOV MOV R5, #0F9H If it’s not preceded with #,
Instruction Add a 0 to indicate that it means to load from a
memory location
(cont’) F is a hex number and
not a letter
a Program LINKER
PROGRAM
myfile.abs
OH
PROGRAM
myfile.hex
Department of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN 14
‰ The lst (list) file, which is optional, is
ASSEMBLING
AND RUNNING
very useful to the programmer
AN 8051 ¾ It lists all the opcodes and addresses as
PROGRAM well as errors that the assembler detected
¾ The programmer uses the lst file to find
lst File the syntax errors or debug
1 0000 ORG 0H ;start (origin) at 0
2 0000 7D25 MOV R5,#25H ;load 25H into R5
3 0002 7F34 MOV R7,#34H ;load 34H into R7
4 0004 7400 MOV A,#0 ;load 0 into A
5 0006 2D ADD A,R5 ;add contents of R5 to A
;now A = A + R5
6 0007 2F ADD A,R7 ;add contents of R7 to A
;now A = A + R7
7 0008 2412 ADD A,#12H ;add to A value 12H
;now A = A + 12H
8 000A 80EF HERE: SJMP HERE;stay in this loop
9 000C END ;end of asm source file
address
Department of omputer Science and Information Engineering
C
‰ The program counter points to the
PROGRAM
address of the next instruction to be
COUNTER AND
ROM SPACE executed
¾ As the CPU fetches the opcode from the
Program program ROM, the program counter is
Counter increasing to point to the next instruction
‰ The program counter is 16 bits wide
¾ This means that it can access program
addresses 0000 to FFFFH, a total of 64K
bytes of code
Family
0FFF
8751
AT89C51 3FFF
DS89C420/30
7FFF
DS5000-32
ADD Show the status of the CY, AC and P flag after the addition of 38H
and 2FH in the following instructions.
Instruction And
MOV A, #38H
PSW
(cont’) ADD A, #2FH ;after the addition A=67H, CY=0
Solution:
38 00111000
+ 2F 00101111
67 01100111
CY = 0 since there is no carry beyond the D7 bit
AC = 1 since there is a carry from the D3 to the D4 bi
P = 1 since the accumulator has an odd number of 1s (it has five 1s)
8051 7F
BANKS AND 30
STACK 2F
Bit-Addressable RAM
RAM Memory 20
Space 1F
Register Bank 3
Allocation 18
(cont’) 17 Register Bank 2
10
0F
Register Bank 1 (stack)
08
07
Register Bank 0
00
6 R6 E R6 16 R6 1E R6
Register Banks 5 R5 D R5 15 R5 1D R5
(cont’) 4 R4 C R4 14 R4 1C R4
3 R3 B R3 13 R3 1B R3
2 R2 A R2 12 R2 1A R2
1 R1 9 R1 11 R1 19 R1
0 R0 8 R0 10 R0 18 R0
Register Banks MOV 00, #99H ;RAM location 00H has 99H
MOV 01, #85H ;RAM location 01H has 85H
(cont’)
Example 2-7
SETB PSW.4 ;select bank 2
MOV R0, #99H ;RAM location 10H has 99H
MOV R1, #85H ;RAM location 11H has 85H
39
The stack is a section of RAM used by
8051 the CPU to store information
REGISTER temporarily
BANKS AND
This information could be data or an
STACK address
Stack The register used to access the stack
is called the SP (stack pointer) register
The stack pointer in the 8051 is only 8 bit
wide, which means that it can take value
of 00 to FFH
When the 8051 is powered up, the SP
register contains value 07
RAM location 08 is the first location begin used
for the stack by the 8051
41
Example 2-8
8051
Show the stack and stack pointer from the following. Assume the
REGISTER default stack area.
BANKS AND MOV R6, #25H
STACK MOV R1, #12H
MOV R4, #0F3H
PUSH 6
Pushing onto PUSH 1
Stack PUSH 4
Solution:
After PUSH 6 After PUSH 1 After PUSH 4
0B 0B 0B 0B
0A 0A 0A 0A F3
09 09 09 12 09 12
08 08 25 08 25 08 25
Start SP = 07 SP = 08 SP = 09 SP = 0A
42
Example 2-9
8051
Examining the stack, show the contents of the register and SP after
REGISTER execution of the following instructions. All value are in hex.
BANKS AND POP 3 ; POP stack into R3
STACK POP 5 ; POP stack into R5
POP 2 ; POP stack into R2
43
The CPU also uses the stack to save
8051
REGISTER the address of the instruction just
BANKS AND below the CALL instruction
STACK This is how the CPU knows where to
resume when it returns from the called
CALL subroutine
Instruction And
Stack
44
The reason of incrementing SP after
8051
push is
REGISTER
BANKS AND Make sure that the stack is growing
STACK toward RAM location 7FH, from lower to
upper addresses
Incrementing Ensure that the stack will not reach the
Stack Pointer bottom of RAM and consequently run out
of stack space
If the stack pointer were decremented
after push
We would be using RAM locations 7, 6, 5, etc.
which belong to R7 to R0 of bank 0, the default
register bank
46
Example 2-10
8051 Examining the stack, show the contents of the register and SP after
REGISTER execution of the following instructions. All value are in hex.
BANKS AND MOV SP, #5FH ;make RAM location 60H
;first stack location
STACK MOV R2, #25H
MOV R1, #12H
MOV R4, #0F3H
Stack And Bank PUSH 2
1 Conflict PUSH 1
PUSH 4
(cont’)
Solution:
After PUSH 2 After PUSH 1 After PUSH 4
63 63 63 63
62 62 62 62 F3
61 61 61 12 61 12
60 60 25 60 25 60 25
Start SP = 5F SP = 60 SP = 61 SP = 62
47