Professional Documents
Culture Documents
8051Microcontroller
Assembly language
Programming
Dr.L.Suganthi
Introduction
• An instruction is an order or command given to a
processor by a computer program.
• All commands are known as instruction set and
set of instructions is known as program.
• As the 8051 family of Microcontrollers are 8-bit
processors, the 8051 Microcontroller Instruction
Set is optimized for 8-bit control applications.
• As a typical 8-bit processor, the 8051
Microcontroller instructions have 8-bit Opcodes.
As a result, the 8051 Microcontroller instruction
set can have up to 28 = 256 Instructions.
• 8051 have in total 111 instructions, i.e. 111
different words available for program writing.
Instruction Format
• OPCODE 8 bit
• Eg: CLC
Types of Instructions
• immediate addressing
28
Register Indirect Addressing
Mode
• The advantage is that it makes accessing data dynamic
rather than static as in direct addressing mode.
• Looping is not possible in direct addressing
mode.
Write a program to clear 16 RAM locations starting
at RAM address 60H.
29
Register indirect addressing
Instruction Format
Op code i
mov a, @Ri ; i = 0 or 1
Example:
Long Addressing
• This mode of addressing is used with the LCALL
and LJMP instructions
• It is a 3-byte instruction and the last 2 bytes
specify a 16-bit destination location where the
program branches
• It allows use of the full 64 K code space
• The program will always branch to the same
location no matter where the program was
previously
• This mode of addressing is used with the LCALL
and LJMP instructions. It is a 3-byte instruction
and the last 2 bytes specify a 16-bit destination
location where the program branches to. It allows
use of the full 64K code space
Long Addressing
Example:
Acc Register Address
• A register can be accessed by direct and register
mode
• Stack instructions
PUSH byte ;increment stack pointer,
;move byte on stack
POP byte ;move from stack to byte,
;decrement stack pointer
• Exchange instructions
XCH a, byte ;exchange accumulator and byte
XCHD a, byte ;exchange low nibbles of
;accumulator and byte
Data Transfer Instructions
MOV <destination>, <source>: allows data to be
transferred between any two internal RAM or SFR
locations
Data Transfer Instructions
-MOV
Data Transfer Instructions
-MOV
Data Transfer Instructions
Instruction XCH A, <source> causes the
accumulator and the address byte to exchange
data
Instruction XCHD A, @Ri causes the low-order
nibbles to be exchanged.
pop
push
stack pointer
stack
55
Exchange Instructions
Arithmetic Instructions
Logic Instructions
Arithmetic Instructions
• Add
• Subtract
• Increment
• Decrement
• Multiply
• Divide
• Decimal adjust
Arithmetic Instructions
Mnemonic Description
ADD A, byte add A to byte, put result in A
ADDC A, byte add with carry
SUBB A, byte subtract with borrow
INC A increment A
INC byte increment byte in memory
INC DPTR increment data pointer
DEC A decrement accumulator
DEC byte decrement byte
MUL AB multiply accumulator by b register
DIV AB divide accumulator by b register
DA A decimal adjust the accumulator
ADD Instructions
65
The 16-bit ADD example
BCD Number System
BCD Number System
DA Instruction
DA Instruction
Subtract Instruction
Example:
SUBB A, #0x4F ;A A – 4F – C
Notice that
There is no subtraction WITHOUT borrow.
Therefore, if a subtraction without borrow is desired,
it is necessary to clear the C flag.
Example:
Clr c
SUBB A, #0x4F ;A A – 4F
Subtract Instruction
Subtract Instruction
Increment and Decrement
INC A increment A
INC byte increment byte in memory
INC DPTR increment data pointer
DEC A decrement accumulator
DEC byte decrement byte
75
Multiplication of Unsigned
Numbers
When multiplying two 8-bit numbers, the size of the
maximum product is 16-bits
FF x FF = FE01
(255 x 255 = 65025)
Division of Unsigned Numbers
DIV AB ; divide A by B
• MOV A,#95H ;load 95 into A
• MOV B,#10H ;load 10 into B
• DIV AB ;now A = 09 (quotient) and B = 05
(remainder)
Table :Unsigned Division Summary (DIV AB)
Division Numerator Denominator Quotient Remainder
byte / byte A B A B
Examples:
ANL AND 00001111
ORL OR ANL 10101100
00001100
XRL XOR
CPL Complement 00001111
ORL 10101100
10101111
00001111
XRL 10101100
10100011
CPL 10101100
01010011
Uses of Logic Instructions
CLR - clear
RL – rotate left
RLC – rotate left through Carry
RR – rotate right
RRC – rotate right through Carry
SWAP – swap accumulator nibbles
CLR ( Set all bits to 0)
CLR A
CLR byte (direct mode)
CLR Ri (register mode)
CLR @Ri (register indirect mode)
Rotate
• Rotate instructions operate only on a
RL a
RR a
mov a, #0A9h ; a A9
add a, #14h ; a BD (10111101), C0
rrc a ; a 01011110, C1
RLC a
C
SWAP a
swap a ; a 27h
Example-Swap Instruction
Bit Logic Operations
ANL C, bit
ORL C, bit
CLR C
CLR bit
CPL C
CPL bit
SETB C
SETB bit
• Conditional jumps
92
Unconditional Jumps
loop: mov a, P1
jz loop ; if a=0, goto loop,
; else goto next
instruction
mov b, a
• There is no zero flag (z)
• Content of A checked for zero on time
Conditional jumps
Mnemonic Description
JZ <rel addr> Jump if a = 0
JNZ <rel addr> Jump if a != 0
JC <rel addr> Jump if C = 1
JNC <rel addr> Jump if C != 1
JB <bit>, <rel addr> Jump if bit = 1
JNB <bit>,<rel addr> Jump if bit != 1
JBC <bir>, <rel addr> Jump if bit =1, &clear
bit
CJNE A, direct, <rel Compare A and
addr> memory, jump if not
equal
More Conditional Jumps
Mnemonic Description
CJNE A, #data <rel Compare A and data,
addr> jump if not equal
CJNE Rn, #data <rel Compare Rn and data,
addr> jump if not equal
CJNE @Rn, #data <rel Compare Rn and
addr> memory, jump if not
equal
DJNZ Rn, <rel addr> Decrement Rn and then
jump if not zero
DJNZ direct, <rel addr> Decrement memory and
then jump if not zero
Conditional Jump -JNB
Conditional Jump -JNB
Conditional Jump -JNB
JZ,JNZ Conditional Jump Instructions
104
JZ Conditional Jump Instructions
JNC- Conditional Jump Instructions
JNC- Conditional Jump Instructions
Looping instruction -DJNZ
109
Loop inside a Loop (Nested Loop)
110
Delay Calculation
Delay Calculation
Example - delay Calculation
Example - delay Calculation
Example - delay Calculation
Example - delay Calculation
Mr. P.8051
Intel Suresh
Programming 120
Call and Return
ret ; PC stack
Why Subroutines?
sqrt: inc a
movc a, @a + PC
subroutine
ret
Sqrs: db 0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3
data
end
Subroutine – Examples
GREEN_LED: equ P1.6
org ooh reset service
ljmp Main
org 100h
Main: clr GREEN_LED
main program
Again: acall Delay
cpl GREEN_LED
sjmp Again