You are on page 1of 102

Microprocessor and Microcontroller

Dr Mohsin Tiwana
Assembly Language

Continued

2
Assembly Language

 Addressing Modes

 Types of Instruction
1. Arithmetic
2. Logical
3. Data Transfer
4. Boolean Variables
5. Program Branching

3
Addressing Modes

4
MOV Summary

 MOV [Destination],[Source]

Examples

 MOV A,#55H ; A will be loaded with 55H

 MOV R2, A ; Load value of A in R2

5
1. Immediate Addressing
 Value to be stored in memory immediately follows the
operation code in memory
Or
 The instruction itself dictates the value to be stored in
memory
 Example
MOV A, #20h
 Accumulator will be loaded with the value that
immediately follows; in this case 20h

6
1. Immediate Addressing

 It is very fast

 Value to be loaded is included in instruction

 The value to be loaded is fixed at compile time – its not


very flexible

7
1. Immediate Addressing
Examples

 MOV A,#25H
 MOV R4,#62H
 MOV B,#40H

 COUNT EQU 30
 MOV A,#COUNT

 MOV P1,#34

8
Important

 MOV A, #FH ; gives error

 MOV A, #0FH ; OKAY

9
2 . Direct Addressing
 Value to be stored in memory is obtained by directly
retrieving it from another memory location

 Example
MOV A, 30H

 The above instruction will read data from RAM address


30H and put that data in A.

10
2 . Direct Addressing
 Generally fast, although the value to be stored is not
included in the instruction

 Its quickly accessible since value is stored in 8051’s


Internal RAM

 Much more flexible than immediate addressing. Value to


be loaded is whatever it is found at that location – which
may be variable

 Entire 128 bytes of RAM (0 to FF) can be accessed


11
2 . Direct Addressing
Examples

 MOV R0, 40H

 MOV 56H, A

 MOV R4, 7FH

12
3. Indirect Addressing
 Also called indirect register addressing

 Very powerful and flexible addressing

 Example
MOV A, @R0

 This instruction analyzes the value of R0. A will be loaded with


value from RAM whose address is found in R0

 For example if R0 holds value 40H and RAM address 40h holds
value of 67H. Then A will have value of 67H

13
3. Indirect Addressing
 Only R0 and R1 can be used for this purpose

 MOV A,@R0
 MOV @R1, B

 Note: ‘@’ sign is compulsory in order to use Indirect


addressing

14
3. Indirect Addressing
 This mode makes data access dynamic instead of static
(as in case of direct addressing)

 Only R0 and R1 can be used

 Since these registers are 8 bit, use is restricted to internal


RAM only

15
4. Register Addressing
 In register addressing mode, we directly use the value
stored in register

 8051 has 8 working registers (of the currently active


register bank) R0 to R7

 Examples
MOV A, R7
MOV A, R5
 Contents of R7 and R5 will be moved in above
instructions

16
4. Register Addressing
 Easy to use register name instead of address

 Source and destination registers should match in size

 Move between A and registers is allowed


MOV A, R4 ; OKAY

 Move between registers is not allowed


MOV R7, R4 ; gives error

17
5. Relative Addressing mode
 Used with certain jump instructions

 8 bit signed offset is used, range for jumping is -128 to


127 location

 SJMP [label]

18
6. Absolute Addressing

 Used only with ACALL and AJMP instruction

 It allows branching within 2K limit (11 bit)

19
7. Long Addressing

 Used only with LCALL and LJMP instruction

 Allows branching within 64K limit (16 bit)

20
8. Index Addressing
 Used with JMP and MOVC instruction
 Use base register (either PC or DPTR) and an offset (the
accumulator) in forming the effective address

 MOVC A, @A + DPTR
 JMP @A + DPTR

 It is used for look up table


 Previously we studied how its stored
 We’ll cover its usage later on

21
Instruction Types

22
Types of Instructions

1. Arithmetic
2. Logical
3. Data Transfer
4. Boolean Variable Manipulation
5. Program Branching

23
1. Arithmetic Instructions

 Addition
 Subtraction
 Multiplication
 Division
 Increment
 Decrement

24
1. Arithmetic Instructions - Addition
ADD
 Addition
 Add A, source ; Add source to A
 Add A, #data ; Add data to A

 Destination operand is always register A

Example
 mov a,#5 ; Set a = 5
 mov b,#10 ; Set b = 10
 Add a,b ; what will be contents of a?
25
1. Arithmetic Instructions - Addition
ADDC
Addition with carry
 Used in case of adding 16 bit numbers

 High byte and Low Byte are added separately using 2


instructions

 If there is a Carry out from addition of low bytes, it has to


be added in sum of high bytes

26
1. Arithmetic Instructions - Addition
ADDC
Addition with carry
 Example

Write a program to add two 16 bit numbers 2CE7H and


558DH

27
1. Arithmetic Instructions - Addition
ADDC
Write a program to add two 16 bit numbers 2CE7H and 558DH

CLR C ;clear carry flag. CY = 0


MOV A, #0E7H ; Load lower byte of 1st number
ADD A, #8DH ; Add lower byte of 2nd number
; now A = 74H and CY = 1;
MOV R0,A ; Move lower byte of sum in R0
MOV A, #2CH ; Load higher byte of 1st number
ADDC A,#55H ; Add with carry
; 2C + 55 + 1 (carry) = 82 H
MOV R1, A ; Move higher byte of sum in R1
28
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract 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 flag means Borrow


 If carry = 0, there is no carry
 If carry = 1, there is a borrow

 Note: There is no subtraction instruction without borrow in


8051 (many microprocessors have it)
29
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow

 After the operation


 Carry = 0 if result is POSITIVE
 Carry = 1 if result is NEGATIVE

 Negative result is stored in format of 2’s complement

30
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

clr C
mov a,#25h
mov r1,#10h
subb a,r1

 What will be value of A?

31
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

clr C ; set carry flag = 0


mov a,#25h ; A = 25h
mov r1,#10h ; r1 = 10h
subb a,r1 ; a = a – r1 = 25 – 10 = 15

 What will be value of A?


 15h
 Carry = 0

32
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

setb C
mov a,#25h
mov r1,#10h
subb a,r1

 What will be value of A?

33
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

setb C ; set carry flag = 1


mov a,#25h ; A = 25h
mov r1,#10h ; r1 = 10h
subb a,r1 ; a = a – r1 – 1 = 25 – 10 – 1 = 14

 What will be value of A?


 14h
 Carry = 0

34
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

clr C
mov a,#10h
mov r1,#20h
subb a,r1

 What will be value of A?

35
1. Arithmetic Instructions - Subtraction
SUBB
SUBtract with Borrow
 Example

clr C ; set carry flag = 0


mov a,#10h ; A = 10h
mov r1,#20h ; r1 = 20h
subb a,r1 ;a = F0h

 What will be value of A?


 F0h = 1111 0000. Taking 2’s complement (by inverting and adding 1)
 = 0000 1111 + 1 = 0001 0000 = 10h
 Carry = 1. Showing it is a negative number

36
1. Arithmetic Instructions - Multiplication
MUL
 Multiplication
Format
MUL AB
 Multiplies A and B
 Operands must be in A and B
 Result
 A has Low byte
 B has high byte

37
1. Arithmetic Instructions - Multiplication
MUL
 Multiplication
Example

mov a,#16
mov b,#4
mul ab

What will be value of A and B?

38
1. Arithmetic Instructions - Multiplication
MUL
 Multiplication
Example

mov a,#16
mov b,#4
mul ab

What will be value of A and B?


 A = 40h = 64
 B=0

39
1. Arithmetic Instructions - Division
DIV
 Division
Format
DIV AB
 Divide A by B
 Operands must be in A and B
 Result
 A has Quotient
 B has Remainder

40
1. Arithmetic Instructions - Division
DIV
Example

mov a,#16
mov b,#4
div ab

What will be value of A and B?

41
1. Arithmetic Instructions - Division
DIV
Example

mov a,#16
mov b,#4
div ab

What will be value of A and B?


 A = 4h
 B=0

42
1. Arithmetic Instructions - Increment
INC
 Increment
Format
INC source
 Adds 1 to source

 Example
 mov r1,#55h
 inc r1

 R1 = 56h

43
1. Arithmetic Instructions - Decrement
DEC
 Decrement
Format
DEC source
 Subtracts 1 from source

 Example
 mov r1,#55h
 dec r1

 R1 = 54h

44
1. Arithmetic Instructions
Important

 Play with arithmetic instructions and see the results

 Good to revise Binary Arithmetic first

45
2. Logical Instructions

 AND
 OR
 Exclusive OR
 Clear
 Complement
 Rotate
 Left
 Right
 SWAP

46
2. Logical Instructions - AND
AND
 Format
ANL destination, source
 Result
destination = destination AND source

 The “destination” is normally Accumulator

47
2. Logical Instructions - AND
AND
 Example

mov a,#74h
anl a,#0Fh

What will be value of A?

48
2. Logical Instructions - AND
AND
 Example

mov a,#74h
anl a,#0Fh

What will be value of A?


A = 04h

49
2. Logical Instructions - OR
OR
 Format
ORL destination, source
 Result
destination = destination OR source

 The “destination” is normally Accumulator

50
2. Logical Instructions - OR
OR
 Example

mov a,#74h
orl a,#0Fh

What will be value of A?

51
2. Logical Instructions - OR
OR
 Example

mov a,#74h
orl a,#0Fh

What will be value of A?


A = 7Fh

52
2. Logical Instructions - XOR
XOR (Exclusive OR)
 Format
XRL destination, source
 Result
destination = destination XOR source

 The “destination” is normally Accumulator

53
2. Logical Instructions - XOR
XOR
 Example

mov a,#74h
xrl a,#0Fh

What will be value of A?

54
2. Logical Instructions - XOR
XOR
 Example

mov a,#74h
xrl a,#0Fh

What will be value of A?


A = 7bh

55
2. Logical Instructions - CLEAR
CLEAR
 Format
CLR operand
 Result
operand = 0

 The “operand” may be Accumulator, carry flag (C), any IO


Port Bit etc

56
2. Logical Instructions - CLEAR
CLEAR
 Example

mov a,#74h
clr a

What will be value of A?


A=0

57
2. Logical Instructions - COMPLEMENT
CPL
 Format
CPL operand
 Result
All bits of operand inverted

 The “operand” may be Accumulator, carry flag (C), any IO


Port Bit etc

58
2. Logical Instructions - COMPLEMENT
CPL
 Example

mov a,#74h
cpl a

What will be value of A?

59
2. Logical Instructions - COMPLEMENT
CPL
 Example

mov a,#74h
cpl a

What will be value of A?


A = 8Bh

60
2. Logical Instructions – Rotate Left
RL (Rotate Left)
 Format
RL A
 Result
RL: All bits of A shifted to left

 LSB is replaced by MSB

MSB LSB

61
2. Logical Instructions – Rotate Left
RLC (Rotate Left through Carry)
 Format
RLC A
 Result
RL: All bits of A shifted to left

 Carry bit is included in rotation

CY MSB LSB

62
2. Logical Instructions – Rotate Right
RR (Rotate Right)
 Format
RR A
 Result
All bits of A shifted to right

MSB is replaced by LSB

MSB LSB

63
2. Logical Instructions – Rotate Right
RRC (Rotate Right through Carry)
 Format
RRC A
 Result
RL: All bits of A shifted to left

 Carry flag is included in rotation

MSB LSB CY

64
2. Logical Instructions – Rotate
Use of Rotate
 Used in bit manipulation

 Rotate Left used as Multiplication

 Rotate Right used as Division

 Less memory required


 Much faster

65
2. Logical Instructions – SWAP
SWAP
 Format
SWAP A
 Result
Swaps lower and higher nibbles

 Works only with accumulator

66
2. Logical Instructions – SWAP
SWAP
 Example

mov a,#74h
swap a

What will be value of A?

67
2. Logical Instructions – SWAP
SWAP
 Example

mov a,#74h
swap a

What will be value of A?


A = 47h

68
3. Data Transfer

 MOV
 Addressing Modes
(Already Covered)

 MOVC
 XCH
 PUSH
 POP

69
3. Data Transfer – MOVC
MOVC
 Used to read data (look-up tables etc) stored in ROM

 How to save data in ROM?


 Dbit, DB, DW etc

 Format
MOVC destination, source
 Destination is mostly A
 Source is in ROM

70
3. Data Transfer – MOVC
MOVC
 Example

Write a program which reads a number from port1 and


sends its square to p2 continuously

71
3. Data Transfer – MOVC
MOVC
Write a program which reads a number from port1 and sends its
square to p2 continuously

ORG 00h
mov dptr,#300h
Label: mov a,p1
movc a, @a + dptr ; Index Addressing Mode
mov p2,a
jmp Label

ORG 300h
sq: db 0,1,4,9,16,25,36,49,64,81,100
end

72
3. Data Transfer – XCH
XCH
 Exchanges the contents of operands
 Format
XCH A, source

 Example
Mov a,#15h ; A = 15h
Mov r1,#0FEh ; R1 = FEh
XCH a,r1 ; swaps data of A and R1
; A = FEh and R1 = 15h

73
3. Data Transfer – PUSH
PUSH
 Used to Push or store data onto stack

 Stack Pointer (SP) stores the address of current stack


location.
 At power up, SP = 07
 First number will be stored at 08 and so on

 When data is stored (using PUSH) command, SP is


incremented automatically

74
3. Data Transfer – PUSH
PUSH

 RAM locations 08h to 1Fh


can be used for stack

 RAM locations 20h to 2Fh


are bit-addressable locations

 More space if SP is set to point to


30h to 7Fh i.e. general purpose RAM

75
3. Data Transfer – PUSH
PUSH
 Format
PUSH expression
 expression must be
 Number of a register
 1 for R1
 2 for R2 and so on
 Address
 Symbol referring to an address

 Note: Accumulator cannot be used with PUSH statement

76
3. Data Transfer – PUSH
PUSH
 Format
PUSH expression
 Example

Push 3 ; Push contents of R3


Push 1 ; Push contents of R1
Push 55h ; Push value stored at memory location 55h

77
3. Data Transfer – POP
POP
 Format
POP expression
 expression must be
 Number of a register
 1 for R1
 2 for R2 and so on
 Address
 Symbol referring to an address

 Retrieves the value the last value stored by PUSH


 Automatically decrements the SP

78
3. Data Transfer – POP
POP
 Example

mov r7,#85h
mov 80h,#5h

PUSH 7
PUSH 80h

POP 1
POP 75h

mov a,75h

What is this program doing?

79
3. Data Transfer – POP
POP
 Example

mov r7,#85h ; R7 = 85h


mov 80h,#5h ; Store 5h at memory location 80h

PUSH 7 ; Store R7 using PUSH


PUSH 80h ; Store contents of location 80h

POP 1 ; Retrieve last pushed value in R1. R1 = 5h


POP 75h ; Retrieve the current value in stack and save
; it in memory location 75h

mov a,75h ; A = contents of location 75h. A = 85h

80
4. Boolean Variable Manipulation
 CLR
 CLR bit
 Clears the bit i.e. makes the bit = 0
 Examples
 CLR C
 CLR P0^3

 SETB
 SETB bit
 Sets the bit i.e. makes the bit = 1
 Examples
 SETB C
 SETB P2^0

81
4. Boolean Variable Manipulation
 ANL
 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

 CPL
 CPL bit
 Inverts the value of bit
 Example
 CPL P0^0

82
5. Program Branching
 CALL and RET
 Unconditional
 AJMP
 SJMP
 LJMP
 Conditional
 CJNE
 DJNZ
 JZ
 JNZ
 JB
 JNB
 JC
 JNC
83
5. Program Branching – CALL and RET
CALL and RET
 Used to Call Subroutines (SR)

 Format
CALL name ; Calling Subroutine
RET ; Return from SR

 If SR is Interrupt Subroutine (ISR), return is:

RETI ; Return from interrupt SR

84
5. Program Branching – CALL and RET
CALL and RET
 Example

ORG 00h
Call fn1 ; Calling Subroutine fn1
Call fn2 ; Calling Subroutine fn2
Jmp $ ; Infinite Loop

fn1: ….
….
RET What if infinite loop is
fn2: … omitted?

RET

85
5. Program Branching – CALL and RET
CALL and RET
Important

 When SR is being executed, PSW can be changed


 Carry Flag
 Auxiliary Carry
 Overflow etc

 All required registers are stored before calling SR and retrieved


after the SR has finished
 PSW
 A
 B
 R0 – R7
86
5. Program Branching – CALL and RET
CALL and RET
Important
 Saving Required registers

1. PUSH them in stack before calling SR


2. POP them from stack after returning from SR

87
5. Program Branching – Unconditional Jumps
SJMP
 Can jump 128 to 127 (8 bit signed) from current address
SJMP REL; REL is 8 bit offset
AJMP
 Can jump to any address represented by 11 bits
AJMP Addr11
LJMP
 Can jump to any address represented by 16 bits
AJMP Addr16
JMP
 We can write JMP and assembler will convert it to
appropriate jump
88
5. Program Branching – Unconditional Jumps
SJMP, AJMP, LJMP
 Example

Org 00h
Main: mov a,#23h
mov a,#25h
mov a, #27h
jmp main ; Automatically decides the appropriate
; Jmp
end

89
5. Program Branching – Conditional Jumps
CJNE
 Compare and Jump if NOT Equal
CJNE A, expression, Label
DJNZ
 Decrement and Jump if NOT Zero
DJNZ, register, Label
JZ
 Jump if Accumulator is Zero
JZ Label
JNZ
 Jump is Accumulator is NOT Zero
JNZ Label
90
5. Program Branching – Conditional Jumps
JB
 Jump if Bit is set
JB bit, Label
JNB
 Jump if Bit is Not set
JNB bit, label
JC
 Jump if Carry is set
JC Label
JNC
 Jump if Carry NOT set
JNC Label
91
5. Program Branching – Conditional Jumps
CJNE
 Compare and Jump if NOT Equal
CJNE expression1, expression2, Label
 expression1 may be
 A
 Register (R0 – R7)
 expression2 may be
 Number
 Address

92
5. Program Branching – Conditional Jumps
CJNE
 Example

mov a,#01h

main: INC a
CJNE A, #09h, main
Jmp $

What is this program doing?


93
5. Program Branching – Conditional Jumps
DJNZ
 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 value of register and then checks if


the value is zero or not

94
5. Program Branching – Conditional Jumps
DJNZ
 Example

mov r7,#05h

main:
DJNZ r7, main

jmp $

What is this program doing?


95
5. Program Branching – Conditional Jumps
JZ
 Jump if accumulator is Zero
 Format
JZ expression

 expression may be
 Label
 8 bit signed offset address

96
5. Program Branching – Conditional Jumps
JZ
 Example

mov a,#1h

main: DEC a
JZ main
jmp $

What is this program doing?


97
5. Program Branching – Conditional Jumps
JNZ
 Jump if accumulator is Not Zero
JNZ expression

 Expression may be
 Label
 8 bit signed offset address

98
5. Program Branching – Conditional Jumps
JNZ
 Example

mov a,#07h

main: DEC a
JNZ main

jmp $

What is this program doing?


99
5. Program Branching – Conditional Jumps
JB
 Jump if Bit is set
JB bit, Label
 Example

Mov a,#0Fh
Label: DEC A
mov p0, a
JB P0^3, Label

What is this program doing?


100
5. Program Branching – Conditional Jumps
JNB
 Jump if Bit is NOT set
JNB bit, Label
 Example

Mov a,#01h
Label: INC A
mov P0,a
JNB P0^3, Label

What is this program doing?


101
5. Program Branching – Conditional Jumps
JC
 Jump if Carry is set
JC Label

JNC
 Jump if Carry Not set
JNC Label

102

You might also like