8051 Assembly Language Programming
8051 Assembly Language Programming
8051 Assembly Language Programming
LANGUAGE
PROGRAMMING
Register are used to store information
INSIDE THE
8051
temporarily, while the information
couldbe
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-bitregisters
There is only one data type, 8bits
2
The 8 bits of a register are shown from
INSIDE THE
8051
MSB D7 to the LSB D0
With an 8-bit data type, any data larger
Registers than 8 bits must be broken into 8-bit
(cont’) chunks before it is processed
most least
significant bit significant bit
D7 D6 D5 D4 D3 D2 D1 D0
8 bit Registers
3
The most widely used registers
INSIDE THE
8051 A (Accumulator)
For all arithmetic and logic instructions
Registers B, R0, R1, R2, R3, R4, R5, R6, R7
(cont’) DPTR (data pointer), and PC (program
counter)
A
B
R0
DPTR DPH DPL
R1
R2
PC PC (Programcounter)
R3
R4
R5
R6
R7
4
MOV destination, source ;copysourcetodest.
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 it means to load from a
Add a 0 to indicate that memory location
(cont’) F is a hex number and
not a letter
6
ADD A, source ;ADD the source operand
INSIDE THE ;to the accumulator
8051 The ADD instruction tells the CPU to add the source
byte to register A and put the result in register A
ADD Source operand can be either a register or
Instruction immediate data, but the destination must always
be register A
“ADD R4, A” and “ADD R2, #12H” are invalid
since A must be the destination of any arithmetic
operation
MOV A, #25H ;load 25H into A
MOV R2, #34H ;load 34H into R2
ADD A, R2 ;add R2 to Accumulator
There are always ;(A = A + R2)
many ways to write
the same program, MOV A, #25H ;load one operand
depending on the ;into A (A=25H)
registers used ADD A, #34H ;add the second
;operand 34H to A
7
In the early days of the computer,
8051 programmers coded in machine language,
ASSEMBLY consisting of 0s and 1s
PROGRAMMING
Tedious, slow and prone to error
Assembly languages, which provided
Structure of
mnemonics for the machine code instructions,
Assembly
plus other features,were developed
Language
An Assembly language program consist of a series
of lines of Assembly language instructions
Assembly language is referred to a low-
level language
It deals directly with the internal structure of the
CPU
8
Assembly language instruction includes
8051 A mnemonic (abbreviation easy to remember)
ASSEMBLY the commands to the CPU, telling it what those to
PROGRAMMING do with those items
Optionally followed by one or two operands
Structure of The data items being manipulated
Assembly
A given Assembly language program is a
Language
series of statements, or lines
Assembly language instructions
Tell the CPU what to do
Directives (or pseudo-instructions)
Give directions to the assembler
9
An Assembly language instruction
8051
ASSEMBLY consistsoffourfields:
[label:] Mnemonic [operands] [;comment]
PROGRAMMING
ORG 0H ;start(origin) at location
0
Structureof MOV R5, #25H ;load 25H into R5
Assembly MOV R7, #34H ;load 34H i nto R7
Directives do not
Language MOV A, #0 ;load 0 int o generate
A any machine
ADD A, R5 ;add conten tscode
ofand
R5aretoused
A
;now A = A + only
R5 by the assembler
Mnemonics ADD A, R7 ;add contents of R7 to A
produce ;now A = A + R7
opcodes ADD A, #12H ;add to A value 12H
;now A = A + 12H
HERE: SJMP HERE ;stay in this loop
END ;endComments
of asm may
source file
be at the end of a
The label field allows line or on a line by themselves
the program to refer to a The assembler ignores comments
line of code by name
10
The step of Assembly language
ASSEMBLING
AND RUNNING
program are outlines as follows:
AN 8051 1) First we use an editor to type a program,
PROGRAM many excellent editors or word
processors are available that can be used
to create and/ or edit the program
Notice that the editor must be able to produce
an ASCII file
For many assemblers, the file names follow
the usual DOS conventions, but the source file
has the extension “asm“ or “src”, depending
on which assembly you are using
11
2) The “asm” source file containing the
ASSEMBLING program code created in step 1 is fed to
AND RUNNING an 8051 assembler
AN 8051 The assembler converts the instructions into
PROGRAM machine code
(cont’) The assembler will produce an object file and
a list file
The extension for the object file is “obj” while
the extension for the list file is “lst”
3) Assembler require a third step called
linking
The linker program takes one or more object
code files and produce an absolute object file
with the extension “abs”
This abs file is used by 8051 trainers that
have a monitor program
12
4) Next the “abs” file is fed into a program
ASSEMBLING called “OH” (object to hex converter)
AND RUNNING which creates a file with extension “hex”
AN 8051 that is ready to burn into ROM
PROGRAM This program comes with all 8051 assemblers
(cont’)
Recent Windows-based assemblers combine
step 2 through 4 into one step
13
EDITOR
PROGRAM
ASSEMBLING myfile.asm
AND RUNNING
AN 8051 ASSEMBLER
PROGRAM PROGRAM
myfile.lst
Other obj files
myfile.obj
Steps to Create
a Program LINKER
PROGRAM
myfile.abs
OH
PROGRAM
myfile.hex
14
The lst (list) file, which is optional, is
ASSEMBLING
ANDRUNNING
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
lstFile 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
15
The program counter points to the
PROGRAM
COUNTER AND
address of the next instruction to be
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
Theprogram counter is 16 bits wide
This means that it can access program
addresses 0000 to FFFFH, a total of 64K
bytes of code
16
All 8051 members start at memory
PROGRAM
COUNTER AND
address 0000 when they’re powered
ROM SPACE up
Program Counter has the value of 0000
Power up The first opcode is burned into ROM
address 0000H, since this is where the
8051 looks for the first instruction when it
is booted
We achieve this by the ORG statement in
the source program
17
Examine the list file and how the code
PROGRAM is placed in ROM
COUNTER AND 1 0000 ORG 0H ;start (origin) at 0
2 0000 7D25 MOV R5,#25H ;load 25H into R5
ROM SPACE 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
Placing Code in 6 0007 2F ADD A,R7
;now A = A + R5
;add contents of R7 to A
ROM ;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
18
After the program is burned into ROM,
PROGRAM
COUNTERAND
the opcode and operand are placed in
ROMSPACE ROM memory location starting a t0000
ROM contents
Address Code
PlacingCodein
0000 7D
ROM 0001 25
(cont’) 0002 7F
0003 34
0004 74
0005 00
0006 2D
0007 2F
0008 24
0009 12
000A 80
000B FE
19
A step-by-step description of the
PROGRAM
COUNTER AND
action of the 8051 upon applying
ROM SPACE power on it
1. When 8051 is powered up, the PC has
Executing 0000 and starts to fetch the first opcode
Program from location 0000 of program ROM
Upon executing the opcode 7D, the CPU
fetches the value 25 and places it in R5
Now one instruction is finished, and then the
PC is incremented to point to 0002, containing
opcode 7F
2. Upon executing the opcode 7F, the value
34H is moved into R7
The PC is incremented to 0004
20
(cont’)
PROGRAM
COUNTER AND 3. The instruction at location 0004 is
ROM SPACE executed and now PC=0006
4. After the execution of the 1-byte
Executing instruction at location 0006, PC=0007
Program 5. Upon execution of this 1-byte instruction
(cont’) at 0007, PC is incremented to 0008
This process goes on until all the instructions
are fetched and executed
The fact that program counter points at the
next instruction to be executed explains some
microprocessors call it the instruction pointer
21
No member of 8051 family can access
PROGRAM
COUNTER AND
more than 64 Kbytes of opcode
ROM SPACE The program counter is a 16-bitregister
Map in 8051
Family 0FFF
8751
AT89C51 3FFF
DS89C420/30
7FFF
DS5000-32
22
8051 microcontroller has only one data
8051 DATA
TYPES AND
type-8bits
DIRECTIVES The size of each register is also 8 bits
It is the job of the programmer to break
Data Type down data larger than 8 bits (00 to FFH,
or 0 to 255 in decimal)
The data types can be positive or negative
23
The DB directive is the most widely
8051 DATA
used data directive in the assembler
TYPES AND
DIRECTIVES It is used to define the 8-bit data
When DB is used to define data,the
Assembler Numbers can be in decimal, binary, hex,
Directives ASCII formats The “D” after the decimal
number is optional, but using
“B” (binary) and “H”
ORG 500H (hexadecimal) for the others is
required
DATA1: DB 28 ;DECIMAL (1C in Hex)
DATA2: DB 00110101B ;BINARY (35 in Hex)
DATA3: DB 39H ;HEX
The Assembler will ORG 510H Place ASCII in quotation marks
convert the numbers DATA4: DB “2591” The;ASCII
AssemblerNUMBERS
will assign ASCII
into hex code for the numbers or characters
ORG 518H
DATA6: DB “My name is Joe”
;ASCII CHARACTERS
Define ASCII strings larger
than two characters
24
ORG (origin)
8051 DATA The ORG directive is used to indicate the
TYPES AND beginning of the address
DIRECTIVES The number that comes after ORG can be
either in hex and decimal
Assembler If the number is not followed by H, it is decimal
Directives and the assembler will convert it to hex
(cont’) END
This indicates to the assembler the end of
the source (asm) file
The END directive is the last line of an
8051program
Mean that in the code any thing after the END
Directive is ignored by the assembler
25
EQU (equate)
8051 DATA
TYPES AND This is used to define a constant without
DIRECTIVES occupying a memory location
The EQU directive does not set aside
Assembler storage for a data item but associates a
directives constant value with a data label
(cont’) When the label appears in the program, its
constant value will be substituted for the label
26
EQU (equate) (cont’)
8051 DATA
TYPES AND Assume that there is a constant used in
DIRECTIVES many different places in the program, and
the programmer wants to change its value
Assembler throughout
By the use of EQU, one can change it once and
directives
the assembler will change all of its occurrences
(cont’)
Use EQU for the
counter constant
COUNT EQU 25
... ....
MOV R3, #COUNT
27
The program status word (PSW)
FLAG BITS AND
PSW REGISTER register, also referred to as the flag
register, is an 8 bit register
Program Status Only 6 bits are used
Word These four are CY (carry), AC (auxiliary carry), P
(parity), and OV (overflow)
– They are called conditional flags, meaning
that they indicate some conditions that
resulted after an instruction was executed
The PSW3 and PSW4 are designed as RS0 and
RS1, and are used to change the bank
The two unused bits are user-definable
28
CY AC F0 RS1 RS0 OV -- P
FLAG BITS AND A carry from D3 to D4
CY PSW.7 Carry flag.
PSW REGISTER AC PSW.6 Auxiliary carry flag. Carry out from the d7 bit
-- PSW.5 Available to the user for general purpose
Program Status RS1 PSW.4 Register Bank selector bit 1.
Word (cont’) RS0 PSW.3 Register Bank selector bit 0.
OV PSW.2 Overflow flag.
Reflect the number of 1s
-- PSW.1 User definable bit. in register A
The result of
signed number P PSW.0 Parity flag. Set/cleared by hardware each
operation is too instruction cycle to indicate an odd/even
large, causing number of 1 bits in the accumulator.
the high-order
bit to overflow RS1 RS0 Register Bank Address
into the sign bit
0 0 0 00H– 07H
0 1 1 08H– 0FH
1 0 2 10H– 17H
1 1 3 18H– 1FH
29
Instructions that affect flag bits
FLAG BITS AND Instruction CY OV AC
PSW REGISTER ADD X X X
ADDC X X X
ADD SUBB X X X
MUL 0 X
Instruction And DIV 0 X
PSW DA X
RPC X
PLC X
SETBC 1
CLRC 0
CPLC X
ANLC,bit X
ANLC,/bit X
ORLC,bit X
ORLC,/bit X
MOVC,bit X
CJNE X
30
The flag bits affected by the ADD
FLAGBITSAND
PSWREGISTER
instruction are CY, P, AC, and OV
Example 2-2
ADD Show the status of the CY, AC and P flag after the addition of 38H
and 2FH in the following instructions.
InstructionAnd
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)
31
Example 2-3
FLAG BITS AND
Show the status of the CY, AC and P flag after the addition of 9CH
PSW REGISTER and 64H in the following instructions.
MOV A, #9CH
ADD
ADD A, #64H ;after the addition A=00H, CY=1
Instruction And
Solution:
PSW
(cont’) 9C 10011100
+ 64 01100100
100 00000000
CY = 1 since there is a carry beyond the D7 bit
AC = 1 since there is a carry from the D3 to the D4 bi
P = 0 since the accumulator has an even number of 1s (it has zero 1s)
32
Example 2-4
FLAGBITS AND
Show the status of the CY, AC and P flag after the addition of 88H
PSW REGISTER and 93H in the following instructions.
MOV A, #88H
ADD
ADD A, #93H ;after the addition A=1BH, CY=1
Instruction And
PSW Solution:
(cont’) 88 10001000
+ 93 10010011
11B 00011011
CY = 1 since there is a carry beyond the D7 bit
AC = 0 since there is no carry from the D3 to the D4 bi
P = 0 since the accumulator has an even number of 1s (it has four 1s)
33
There are 128 bytes of RAM in the
REGISTER 8051
BANKS AND
Assigned addresses 00 to 7FH
STACK
The 128 bytes are divided into three
RAM Memory different groups as follows:
Space 1) A total of 32 bytes from locations 00 to
Allocation 1F hex are set aside for register banks
and the stack
2) A total of 16 bytes from locations 20H to
2FH are set aside for bit-addressable
read/write memory
3) A total of 80 bytes from locations 30H to
7FH are used for read and write storage,
called scratch pad
34
RAM Allocation in 8051
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
35
These 32 bytes are divided into 4
8051
REGISTER banks of registers in which each bank
BANKS AND has 8 registers, R0-R7
STACK RAM location from 0 to 7 are set aside for
bank 0 of R0-R7 where R0 is RAM location
Register Banks 0, R1 is RAM location 1, R2 is RAM
location 2, and so on, until memory
location 7 which belongs to R7 of bank0
It is much easier to refer to these RAM
locations with names such as R0,R1,and
soon, than by their memory locations
Register bank 0 is the default when
8051 is powered up
36
8051 Register banks and their RAM address
REGISTER Bank 3
Bank 0 Bank 1 Bank 2
BANKS AND
7 R7 F R7 17 R7 1F R7
STACK
6 R6 E R6 16 R6 1E R6
D
Register Banks 5 R5 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
37
We can switch to other banks by use
8051
REGISTER of the PSW register
BANKS AND Bits D4 and D3 of the PSW are used to
STACK select the desired register bank
Use the bit-addressable instructions SETB
Register Banks and CLR to access PSW.4 and PSW.3
(cont’)
PSW bank selection
RS1(PSW.4) RS0(PSW.3)
Bank0 0 0
Bank1 0 1
Bank2 1 0
Bank3 1 1
38
Example 2-5
8051
MOV R0, #99H ;load R0 with 99H
REGISTER MOV R1, #85H ;load R1 with 85H
BANKS AND
STACK
Example 2-6
MOV 00, #99H ;RAM location 00H has 99H
Register Banks 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
40
The storing of a CPU register in the
8051
stack is called a PUSH
REGISTER
BANKS AND SP is pointing to the last used location of
STACK the stack
As we push data on to the stack, the SP
Stack is incremented by one
(cont’) This is different from many microprocessors
Loading the contents of the stack back
into a CPU register is called a POP
With every pop, the top byte of the stack
is copied to the register specified by the
instruction and the stack pointer is
decremented once
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
PUSH 4
Stack
Solution:
AfterPUSH6 AfterPUSH1 AfterPUSH4
0B 0B 0B 0B
0A 0A 0A 0A F3
09 09 09 12 09 12
08 08 25 08 25 08 25
StartSP=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
REGISTER push is
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
45
When 8051 is powered up, register
8051
REGISTER bank 1 and the stack are using the
BANKS same memory space
AND We can reallocate another section of RAM
STACK to the stack
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:
AfterPUSH2 AfterPUSH1 AfterPUSH4
63 63 63 63
62 62 62 62 F3
61 61 61 12 61 12
60 60 25 60 25 60 25
StartSP=5F SP=60 SP=61 SP=62
47