of 104
8/13/2019 AL41
1/104
2
Assembly Language
8/13/2019 AL41
2/104
Assembly Language
Addressing Modes
Types of InstructionArithmeticLogical
Data Transfer
Boolean Variables
Program Branching
1.2.
3.
4.
5.
4
8/13/2019 AL41
3/104
AddressingModes
5
8/13/2019 AL41
4/104
1. Immediate Addressing
Value to be stored in memory immediately follows theoperation code in memoryOr
The instruction itself dictatesmemory
ExampleMOV A,
the value to be storedin
#20h Accumulator will be loaded with the value that
immediately follows; in this case 20h
6
8/13/2019 AL41
5/104
1. Immediate Addressing
It is very fast
Value to be loaded is includedin instruction
The value tobeloadedisfixedatcompiletimeitsnotveryflexible
7
8/13/2019 AL41
6/104
1. ImmediateAddressing
Examples MOV MOV MOV
A,#25HR4,#62H
B,#40H
COUNT EQU 30 MOVA,#COUNT MOVP1,#34
8
8/13/2019 AL41
7/104
Important
MOVA,#FH ; giveserror
MOVA,#0FH ;OKAY
9
8/13/2019 AL41
8/104
2. Direct Addressing
Value to be stored in memory is obtained bydirectly
retrievingitfromanother memorylocation
ExampleMOV A, 30H
The above instruction will read30H and put that data in A.
datafromRAMaddress
10
8/13/2019 AL41
9/104
2. Direct Addressing
Generally fast, although the value to be stored is notincluded in the instruction
Its quickly accessible sinceInternal RAM
value is stored in 8051s
Much more flexible than immediate addressing. Valuetobe loaded is whatever it is found at that location whichmay be variable
Entire 128 bytes of RAM (0 to FF) can be accessed
11
8/13/2019 AL41
10/104
2 . Direct Addressing
Examples MOVR0, 40H
MOV56H, A MOVR4,7FH
12
8/13/2019 AL41
11/104
3. Indirect Addressing Also called indirect register addressing
Very powerful and flexible addressing
ExampleMOV A,@R0
This instruction analyzes the value of R0.A will be loaded withvalue from RAM whose address is found in R0
For example if R0 holds value 40H and RAM address 40h holdsvalue of 67H. Then A will have value of 67H
13
mailto:@R0mailto:@R08/13/2019 AL41
12/104
3. Indirect Addressing Only R0 and R1
can
be
used
for
this
purpose
MOV A,@R0 MOV @R1,B
Note: @signaddressing
iscompulsoryinordertouseIndirect
14
mailto:@R0mailto:@R1mailto:@R1mailto:@R08/13/2019 AL41
13/104
3. Indirect Addressing This mode makes data access dynamic
(as in case of direct addressing)instead
of
static
Only R0 and R1 can be used
Since theseregistersare8bit,useisrestrictedtointernalRAMonly
15
8/13/2019 AL41
14/104
4. Register Addressing In register addressing mode, we
stored in registerdirectly use the value
8051 has 8 working registers (ofthecurrentlyactiveregister bank) R0 toR7
ExamplesMOV A, R7
MOV A, R5 Contents of R7 and R5 will be moved in above
instructions
16
8/13/2019 AL41
15/104
4. Register Addressing Easy to use register name instead of address
Source and destination registers should matchinsize
MovebetweenA and registers is allowedMOV A, R4 ; OKAY
Movebetweenregisters is not allowedMOV R7, R4 ; gives error
17
8/13/2019 AL41
16/104
5. Relative Addressing mode Used with certain jump instructions
8 bit signed offset
127 locationisused,rangeforjumpingis-128to
SJMP[label]
18
8/13/2019 AL41
17/104
6. Absolute Addressing
Used only with ACALL and AJMP instruction
Itallowsbranchingwithin2Klimit(11bit)
19
8/13/2019 AL41
18/104
7. Long Addressing
Used only with LCALL and LJMP instruction
Allowsbranchingwithin64Klimit(16bit)
20
8/13/2019 AL41
19/104
8. Index Addressing Used with JMP and MOVC instruction
Use base register (either PC or DPTR) and an offset (theaccumulator) in formingtheeffectiveaddress
MOVC A, @A+ DPTR JMP @A+ DPTR
It is used for look up tablePreviously we studied how its
Wellcover its usage later on
stored
21
mailto:@Amailto:@Amailto:@Amailto:@Amailto:@A8/13/2019 AL41
20/104
22
Instruction Types
8/13/2019 AL41
21/104
Types of Instructions
ArithmeticLogical
Data Transfer
Boolean Variable Manipulation
1.
2.
3.
4.
5. ProgramBranching
23
8/13/2019 AL41
22/104
MOV Summary
MOV [Destination],[Source]
Examples
MOVA,#55H ; A will be loadedwith55H
MOVR2, A ; Load value of A in R2
24
8/13/2019 AL41
23/104
1. ArithmeticInstructions
Addition Subtraction Multiplication Division Increment Decrement
25
8/13/2019 AL41
24/104
1. Arithmetic Instructions - AdditionADD
Addition Add A, source Add A, #data
; Add source to A; Add data to A
Destination operand is always register A
Examplemov a,#5mov b,#10
Add a,b
;;
;
Set a = 5Set b = 10
what will be contents
of a?
26
8/13/2019 AL41
25/104
1. Arithmetic Instructions - AdditionADDC
Addition with carry Used in case of adding 16 bit numbers
High byte and Low Byte are added separatelyinstructions
using 2
If there isbe added
a Carry out from addition of low bytes, it hasin sum of high bytes
to
27
8/13/2019 AL41
26/104
1. Arithmetic Instructions-AdditionADDC
Addition with carry Example
Write a program558DH
toaddtwo16bitnumbers2CE7Hand
28
8/13/2019 AL41
27/104
1. Arithmetic Instructions - AdditionADDCWrite a program toadd two 16 bit numbers 2CE7H and558DH
CLR CMOV A, #0E7H
ADD A, #8DH
;clear carry flag. CY = 01st;
;
;
;
;
;
;
;
Load lower byte of numbernumber2ndAdd lower byte of
now A = 74H and CY = 1;Move lower byte of sum in R0MOV R0,A
MOV A, #2CH
ADDC A,#55H
1stLoad higher byte ofAdd with carry
number
2C + 55 + 1 (carry) = 82 HMove higher byte of sum in R1MOV R1, A
29
8/13/2019 AL41
28/104
1. Arithmetic Instructions - SubtractionSUBBSUBtract with Borrow
Subtraction with borrow Uses carry flag to read if there is any borrow or not Format
SUBB A, Source ; A = A source CY
Before the subtraction, Carry If carry = 0, there is no carry If carry = 1, there is a borrow
flag means Borrow
Note: There is no subtractioninstruction withoutborrowin8051 (many microprocessors have it)
30
8/13/2019 AL41
29/104
1. Arithmetic Instructions-SubtractionSUBB
SUBtract with Borrow
After the operation Carry = 0 if result Carry = 1 if result
is POSITIVEis NEGATIVE
Negative result isstored in formatof 2s complement
31
8/13/2019 AL41
30/104
1. Arithmetic Instructions-SubtractionSUBB
SUBtract with Example
Borrow
clr Cmov a,#25h
mov r1,#10h
subb a,r1
What will bevalue ofA?32
8/13/2019 AL41
31/104
1. Arithmetic Instructions -SubtractionSUBBSUBtract with
ExampleBorrow
clr Cmov
mov
;;
;
;
set carry flag = 0A = 25h
r1 = 10h
a,#25hr1,#10h
subb a,r1 a =ar1=2510=15
What will bevalue of A?15hCarry = 0
33
8/13/2019 AL41
32/104
1. Arithmetic Instructions-SubtractionSUBB
SUBtract with Example
Borrow
setb Cmov a,#25h
mov r1,#10h
subb a,r1
What will bevalue ofA?34
8/13/2019 AL41
33/104
1. Arithmetic Instructions -SubtractionSUBBSUBtract with
ExampleBorrow
setb Cmov a,#25h
mov r1,#10h
subb a,r1
;;
;
;
set carry flag = 1A = 25h
r1 = 10h
a =ar11=25101=14
What will bevalue of A?14hCarry = 0
35
8/13/2019 AL41
34/104
8/13/2019 AL41
35/104
1. Arithmetic Instructions -SubtractionSUBBSUBtract with Example
Borrow
clr C
movmov
;
;;
set carry flag = 0
A = 10hr1 = 20ha,#10hr1,#20hsubb a,r1 ;a = F0h
What will bevalue of A?
F0h = 1111 0000. Taking 2s complement (by inverting and adding 1)= 0000 1111 + 1 = 0001 0000 = 10h
Carry = 1. Showing it is a negative number
37
8/13/2019 AL41
36/104
1. ArithmeticInstructions- MultiplicationMUL MultiplicationFormat
MUL AB Multiplies A andB Operands must be Result
inAandB
A has Low byte
B has high byte
38
8/13/2019 AL41
37/104
1. ArithmeticInstructions-MultiplicationMUL MultiplicationExample
mov a,#16mov b,#4
mul ab
What willbevalueof A and B?
39
8/13/2019 AL41
38/104
1. ArithmeticInstructions-MultiplicationMUL MultiplicationExample
mov a,#16mov b,#4mul ab
What will bevalue= 64 of A and B?A = 40hB = 0
40
8/13/2019 AL41
39/104
8/13/2019 AL41
40/104
1. ArithmeticInstructions-DivisionDIVExample
mov a,#16mov b,#4div ab
What willbevalue of A and B?
42
8/13/2019 AL41
41/104
1. ArithmeticInstructions-DivisionDIVExample
mov a,#16mov b,#4div ab
What will bevalue of A and B?A = 4hB = 0
43
8/13/2019 AL41
42/104
8/13/2019 AL41
43/104
1. ArithmeticInstructions - DecrementDEC DecrementFormat
DEC
source
source
Subtracts 1from Example
mov r1,#55h
dec r1
R1 = 54h
45
8/13/2019 AL41
44/104
1. Arithmetic InstructionsBCD NumbersBCD NumbersBinary Coded Decimal
Unpacked BCD Lower 4 Bits represent decimal Remaining bits are zero Example: 9 will be represented
number (0 to 9)
as 0000 1001 1 byte can represent 1 number onlyPacked BCD
Each 4 bits represent a decimal number (0 to 9)1 byte can store more 2 digits
Example: 79 will be represented as 0111 1001
Utilizes space more efficiently than unpacked BCD
46
8/13/2019 AL41
45/104
1. Arithmetic Instructions BCD NumbersBCD NumbersBinary Coded Decimal Addition of BCD numbers Consider additionoffollowingBCDnumbers
mov A,#59hadd A,#1h
Result is 5AhIs it a BCD? NO
The answer should
Solution?
have been 60
47
8/13/2019 AL41
46/104
1. ArithmeticInstructionsBCDNumbersDAADecimal Adjust
mov A,#59h
add A,#1hDAA A
Result is 60hIs it a BCD? Yes
4th 5thAuxiliary Carry (from to bit)is used to check this condition
48
8/13/2019 AL41
47/104
1. Arithmetic InstructionsImportant
Play with arithmetic instructionsand seetheresults
GoodtoreviseBinaryArithmeticfirst
49
8/13/2019 AL41
48/104
2. LogicalInstructions
AND OR Exclusive OR Clear Complement Rotate Left Right
SWAP
50
8/13/2019 AL41
49/104
2. LogicalInstructions - ANDAND Format
ANL destination,source Result
destination = destination AND source
ThedestinationisnormallyAccumulator
51
8/13/2019 AL41
50/104
2. LogicalInstructions-ANDAND Example
mov a,#74hanl a,#0Fh
What will bevalue of A?
52
8/13/2019 AL41
51/104
2. LogicalInstructions-ANDAND Example
mov a,#74hanl a,#0Fh
What will beA = 04h value of A?
53
8/13/2019 AL41
52/104
8/13/2019 AL41
53/104
2. LogicalInstructions-OROR Example
mov a,#74horl a,#0Fh
What will bevalue of A?
55
8/13/2019 AL41
54/104
2. LogicalInstructions-OROR Example
mov a,#74horl a,#0Fh
What will beA = 7Fh value of A?
56
8/13/2019 AL41
55/104
2. Logical Instructions - XORXOR (Exclusive OR) Format
XRL
Resultdestination,source
destination = destination XOR source
ThedestinationisnormallyAccumulator
57
8/13/2019 AL41
56/104
2. LogicalInstructions-XORXOR Example
mov a,#74hxrl a,#0Fh
What will bevalue of A?
58
8/13/2019 AL41
57/104
8/13/2019 AL41
58/104
2. LogicalInstructions- CLEARCLEAR Format
CLR operand Result
operand = 0
The operandPort Bit etc
maybeAccumulator,carryflag (C), any IO
60
8/13/2019 AL41
59/104
2. LogicalInstructions-CLEARCLEAR Example
mov a,#74h
clr a
WhatA = 0
willbevalueofA?
61
8/13/2019 AL41
60/104
2. LogicalInstructions - COMPLEMENTCPL Format
CPL operand Result
All bits of operand inverted
The operandPort Bit etc
maybeAccumulator, carryflag(C), any IO
62
8/13/2019 AL41
61/104
2. LogicalInstructions-COMPLEMENTCPL Example
mov a,#74h
cpl a
What
will be
value of A?
63
8/13/2019 AL41
62/104
2. LogicalInstructions-COMPLEMENTCPL Example
mov a,#74h
cpl a
What will be
A = 8Bhvalue of A?
64
8/13/2019 AL41
63/104
8/13/2019 AL41
64/104
2. Logical Instructions RotateLeftRLC (Rotate Format
Left through Carry)
RLC A Result
RL: All bits of A shiftedtoleft
Carrybitis included inrotation
CY MSB LSB
66
8/13/2019 AL41
65/104
8/13/2019 AL41
66/104
2. Logical Instructions RotateRightRRC (Rotate Format
Rightthrough Carry)
RRC A Result
RL: All bits of A shiftedtoleft
Carryflagisincludedinrotation
CYMSB LSB
68
8/13/2019 AL41
67/104
8/13/2019 AL41
68/104
2. LogicalInstructions SWAPSWAP Format
SWAP A Result
Swaps lower and highernibbles
Worksonlywithaccumulator
70
8/13/2019 AL41
69/104
2. LogicalInstructionsSWAPSWAP Example
mov a,#74h
swap a
WhatwillbevalueofA?
71
8/13/2019 AL41
70/104
2. LogicalInstructionsSWAPSWAP Example
mov a,#74h
swap a
What willA = 47h
bevalueofA?
72
8/13/2019 AL41
71/104
3. Data Transfer
MOV Addressing Modes
(Already Covered)
MOVC XCH PUSH POP
73
8/13/2019 AL41
72/104
8/13/2019 AL41
73/104
3. Data Transfer MOVCMOVC Example
Write a program which reads anumberfromport1and
sendsitssquaretop2continuously
75
8/13/2019 AL41
74/104
3. Data Transfer MOVCMOVC
Write a program which readssquare to p2 continuously
a numberfrom port1 and sends its
ORG 00hmov dptr,#300h
Label: mov a,p1movc a, @a+ dptrmov p2,a
jmp Label
; Index Addressing Mode
ORGsq:
end
300hdb 0,1,4,9,16,25,36,49,64,81,100
76
mailto:@amailto:@a8/13/2019 AL41
75/104
3. Data Transfer XCHXCH Exchanges Format
thecontents ofoperands
XCH A,source
ExampleMov a,#15h
Mov r1,#0FEh
XCH a,r1
;;
;
;
A = 15hR1 = FEh
swaps data of A and R1A = FEh and R1 = 15h
77
8/13/2019 AL41
76/104
3. Data Transfer PUSHPUSH Used to Push or store data onto stack
Stack Pointer (SP) stores the address of current stacklocation.
At power up, SP = 07 First number will be stored at 08 and so on
When data is stored (using PUSH) command,incremented automatically
SP is
78
8/13/2019 AL41
77/104
3. Data Transfer PUSHPUSH
RAM locations 08h to 1Fhcan be used for stack
RAM locations 20h to 2Fhare bit-addressable locations
More space if SP is set to pointto30h to 7Fh i.e. general purpose RAM
79
8/13/2019 AL41
78/104
3. Data Transfer PUSHPUSH Format
PUSH expression must be Number of a register
1 for R1 2 for R2 and so on
Address
expression
Symbol referring to anaddress
Note: Accumulator cannot be used withPUSH statement
80
8/13/2019 AL41
79/104
3. Data TransferPUSHPUSH Format
PUSH expression Example
PushPush
Push
31
55h
;;
;
PushPush
Push
contents of R3contents of R1
value stored atmemory
location 55h
81
8/13/2019 AL41
80/104
3. Data Transfer POPPOP Format
POP expression expression must be Number of a register
1 for R1 2 for R2 and so on
Address Symbol referring to anaddress
Retrieves the value the last value stored Automatically decrements the SP
by PUSH
82
8/13/2019 AL41
81/104
8/13/2019 AL41
82/104
3. Data TransferPOPPOP Examplemov r7,#85hmov 80h,#5h
; R7 = 85h; Store 5h at memory location 80h
PUSH 7PUSH 80h
; Store R7 using PUSH; Store contents of location 80h
POP 1POP 75h
; Retrieve last pushed value in R1. R1 = 5h; Retrieve the current value in stack and save
; it in memory location 75h
mov a,75h ; A = contents of location 75h. A = 85h
84
8/13/2019 AL41
83/104
4. Boolean Variable Manipulation CLR CLR bit Clears the bit Examples
CLR C CLR P0^3
i.e.makesthebit=0
SETBSETB bit
Sets the bitExamples
SETB C SETB P2^0
i.e. makes the bit =1
85
l bl l
8/13/2019 AL41
84/104
4. Boolean Variable ManipulationANL ANL
bit1,bit2
Takes logical AND of both bits Example
anl c,P1^7
ORL ORL
bit1,bit2 Takes logical OR of both bits Example
orl c,P3^5
CPLCPL bitInverts the value of bit
Example
CPL P0^0
86
h
8/13/2019 AL41
85/104
5. Program BranchingCALL and RETUnconditional AJMP SJMP LJMPConditional
CJNEDJNZ
JZ
JNZ
JB
JNB
JC
JNC
87
hi CALL d
8/13/2019 AL41
86/104
5. Program BranchingCALLandRETCALL and RET Used toCall Subroutines(SR)
FormatCALL name ; Calling Subroutine
Return from SRRET ;
If SR is Interrupt Subroutine (ISR), return is:
RETI ; Return from interruptSR
88
5 P B hi CALL d RET
8/13/2019 AL41
87/104
5. Program Branching CALLandRETCALL and RET ExampleORG 00hCallCall
Jmp
fn1fn2
$
; Calling Subroutine fn1; Calling Subroutine fn2
; Infinite Loop
fn1: ..
RET
RET
What if infinite loop isomitted?fn2:
89
5 P B hi CALL d RET
8/13/2019 AL41
88/104
5. Program Branching CALL and RETCALL and RETImportant
When SR is being executed, PSW can be changed Carry Flag Auxiliary Carry Overflow etc
All required registers are stored before calling SR and retrievedafter the SR has finished
PSW A B R0 R7
90
5 P B hi CALL d RET
8/13/2019 AL41
89/104
5. Program Branching CALLandRETCALL and RETImportant
Saving Required registers
PUSH them in stack before calling SR1.2. POPthemfromstackafterreturningfromSR
91
5 P B hi U diti l J
8/13/2019 AL41
90/104
5. Program Branching Unconditional JumpsSJMP Canjump128 to 127 (8 bit signed) from currentaddress
SJMP REL; REL is 8 bit offsetAJMP Canjumptoanyaddress represented
AJMP Addr11
by11bits
LJMP Canjumptoanyaddress represented
AJMP Addr16
by16bits
JMP We can write JMP and assembler will
appropriate jumpconvert it to
92
5 P B hi U diti l J
8/13/2019 AL41
91/104
5. Program BranchingUnconditionalJumpsSJMP, AJMP, LJMP Example
Org 00h
Main:movmov
mov
a,#23ha,#25h
a, #27h
jmp main ;;
Automatically decides the appropriateJmp
end
93
5 P B hi C diti l J
8/13/2019 AL41
92/104
5. Program Branching ConditionalJumpsCJNE Compare and Jump if NOT Equal
CJNE A, expression, LabelDJNZ Decrement and Jump if NOT Zero
DJNZ, register, Label
JZ
Jump if Accumulator is ZeroJZ Label
JNZ
Jump is Accumulator is NOT ZeroJNZ Label
94
5 P B hi C diti l J
8/13/2019 AL41
93/104
5. Program BranchingConditionalJumpsJB Jump ifBitisset
JBbit, LabelJNB Jump ifBitisNotset
JNB bit, labelJC Jump ifCarry is set
JC LabelJNC Jump ifCarry NOT set
JNC Label95
5 Program Branching Conditional Jumps
8/13/2019 AL41
94/104
5. Program Branching Conditional JumpsCJNE Compare and Jump if NOT Equal
CJNE expression1, expression1 may be
A Register (R0 R7)
expression2,Label
expression2maybeNumber
Address
96
5 Program Branching Conditional Jumps
8/13/2019 AL41
95/104
5. ProgramBranchingConditionalJumpsCJNE Example
mov a,#01h
main: INC aCJNE A, #09h, main
Jmp $
What is this program doing?97
5 Program Branching Conditional Jumps
8/13/2019 AL41
96/104
5. Program Branching ConditionalJumpsDJNZ Decrement and Jump if Not Zero
DJNZ register, Label
Register may be any register of the active bank
R0 to R7
First it decrements the valuethe value is zero or not
ofregisterandthenchecksif
98
5 Program Branching Conditional Jumps
8/13/2019 AL41
97/104
5. ProgramBranchingConditionalJumpsDJNZ Example
mov r7,#05h
main:DJNZ r7,main
jmp $
What is this program doing?99
5 Program Branching Conditional Jumps
8/13/2019 AL41
98/104
5. Program Branching ConditionalJumpsJZ Jump if accumulator Format
is Zero
JZ expression
expression may beLabel8 bit signed
offset address
100
5 Program Branching Conditional Jumps
8/13/2019 AL41
99/104
5. ProgramBranchingConditionalJumpsJZ Example
mov a,#1h
main: DEC aJZ main
jmp $
What is thisprogram doing?101
5 Program Branching Conditional Jumps
8/13/2019 AL41
100/104
5. Program Branching ConditionalJumpsJNZ Jump if accumulator is Not Zero
JNZ expression
Expression may beLabel8bitsignedoffsetaddress
102
5 Program Branching Conditional Jumps
8/13/2019 AL41
101/104
5. ProgramBranchingConditionalJumpsJNZ Examplemov a,#07h
main: DEC aJNZ main
jmp $
What is thisprogram doing?103
5 Program Branching Conditional Jumps
8/13/2019 AL41
102/104
5. Program Branching ConditionalJumpsJB Jump if Bit isset
JBbit,Label Example
Mov a,#0FhLabel: DEC A
mov p0, a
JB P0^3, Label
What is thisprogram doing?104
5 Program Branching Conditional Jumps
8/13/2019 AL41
103/104
5. Program Branching ConditionalJumpsJNB Jump if Bit isNOTset
JNBbit,Label Example
Mov a,#01hLabel: INC A
mov P0,a
JNB P0^3, Label
What is this program doing?105
5 Program Branching Conditional Jumps
8/13/2019 AL41
104/104
5. Program Branching ConditionalJumpsJC Jump ifCarryis set
JC Label
JNC Jump ifCarryNotset
JNCLabel