0% found this document useful (0 votes)
3 views47 pages

Microcontroller Lecture 7

The document outlines the instruction set for the 8051 microcontroller, detailing various categories such as data transfer, arithmetic, logical, and control instructions. It includes specific instructions for manipulating bits, using the carry flag, and performing data exchanges, as well as examples of how to use these instructions in programming. Additionally, it covers branching instructions for program flow control, including conditional jumps and subroutine calls.

Uploaded by

Ashish Goyal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views47 pages

Microcontroller Lecture 7

The document outlines the instruction set for the 8051 microcontroller, detailing various categories such as data transfer, arithmetic, logical, and control instructions. It includes specific instructions for manipulating bits, using the carry flag, and performing data exchanges, as well as examples of how to use these instructions in programming. Additionally, it covers branching instructions for program flow control, including conditional jumps and subroutine calls.

Uploaded by

Ashish Goyal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

8051 Instructions Set

Part 2
Total instruction set (255)---(111)

Data transfer Arithmetic Logical Boolean


instructions instructions Instructions Instructions

Control Branching Rotate Shift


Instructions Instructions Instructions Instructions

Call
Instructions
Bit-processing/Boolean
instructions (0/1)
Bit addressable registers

SFR D7 D6 D5 D4 D3 D2 D1 D0

P0 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0

P1 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0

P2 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0

P3 P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0

ACC ACC.7 ACC.6 ACC.5 ACC.4 ACC.3 ACC.2 ACC.1 ACC.0

B B.7 B.6 B.5 B.4 B.3 B.2 B.1 B.0

PSW PSW.7 PSW.6 PSW.5 PSW.4 PSW.3 PSW.2 PSW.1 PSW.0

TCON TCON.7 TCON.6 TCON.5 TCON.4 TCON.3 TCON.2 TCON.1 TCON.0

IE IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0

IP IP.7 IP.6 IP.5 IP.4 IP.3 IP.2 IP.1 IP.0


Instruction using Carry

Instruction Description
SETB C Set the carry flag to 1, C = 1
CLR C Clear the carry flag to 0, C = 0
CPL C Complement the carry flag, C = NOT C
MOV bit, C Copy status of the carry flag to bit address bit, (bit) =
C
MOV 01H, C If C = 0, → bit address (01H) = 0
MOV C, bit Copy status of bit address bit to the carry flag, C = (bit)
MOV C, 10H If bit address (10H) = 0, → C = 0
Instruction using Carry
ANL C, bit AND operation between C and bit, C = C AND bit

ANL C, 05H If C = 0 and (05H) = 1, → C = 0 AND 1 = 0


ANL C, /bit
AND operation between C and (bit)’, C = C AND NOT bit, does not
affect status of bit
ANL C, /05H If C = 1 and (05H) = 0, → C = 1 AND NOT 0 = 1
ORL C, bit OR operation between C and bit, C = C OR bit
ORL C, 05H If C = 0 and (05H) = 1, → C = 0 OR 1 = 1
ORL C, /bit OR operation between C and bit’, C = C OR NOT bit, does not affect
status of bit

ORL C, /05H If C = 0 and (05H) = 0, → C = 0 AND NOT 0 = 1


Other Instructions
Instruction Description
SETB bit Set bit address bit to 1, (bit) = 1

SETB 00H (00H)Bit = 1

CLR bit Clear bit address bit to 0, (bit) = 0

CLR 00H (00H)Bit = 0

CPL bit Complement bit address bit, (bit) = NOT(bit)

CPL 00H If (00H)Bit = 1 → (00H)Bit = 0


Simulate the
given circuit in
8051

Bit addressable inputs and outputs


Assume bit address of each input

A=20H MOV C,20H

B=21H ANL C,21H


MOV 26H,C

C=22H MOV C,22H

D=23H ORL C, 23H

E=24H ANL C,26H

F=25H CPL C
MOV 27H,C

G=26H MOV C,24H


ANL C,/25H

H=26H ORL C,27H

I=27H MOV 26H,C


Total instruction set (255)---(111)

Data transfer Arithmetic Logical Boolean


instructions instructions Instructions Instructions

Control Branching Rotate Shift


Instructions Instructions Instructions Instructions

Call
Instructions
Data transfer instructions
MOV A, Rn MOV instructions
MOV A, direct
MOV A, @Ri (RAM)
MOV Rn, A MOV direct, A
MOV Rn, direct MOV direct, Rn

MOV Rn, #data MOV direct, #data


MOV @Ri, A
MOV @Ri, #data
MOV DPTR, #data16
MOV : Different types
Instruction Description Example
MOV A, Rn Move data from register Rn to accumulator A. MOV A, R0 ; Move contents of R0 to A

MOV A, direct Move data from direct address to accumulator A. MOV A, 30H ; Move contents of RAM location 30H to A

MOV A, @Ri Move data from indirect address pointed by Ri to A. MOV A, @R0 ; Move contents pointed by R0 to A

MOV Rn, A Move data from accumulator A to register Rn. MOV R1, A ; Move contents of A to R1

MOV Rn, direct Move data from direct address to register Rn. MOV R2, 40H ; Move contents of RAM location 40H to R2

MOV Rn, #data Move immediate data to register Rn. MOV R3, #5AH ; Move immediate data 5AH to R3

MOV direct, A Move data from accumulator A to a direct address. MOV 50H, A ; Move contents of A to RAM location 50H

MOV direct, Rn Move data from register Rn to a direct address. MOV 60H, R4 ; Move contents of R4 to RAM location 60H

MOV direct, #data Move immediate data to a direct address. MOV 70H, #0FFH ; Move immediate data FFH to RAM location 70H

MOV @Ri, A Move data from accumulator A to indirect address pointed MOV @R1, A ; Move contents of A to location pointed by R1
by Ri.

MOV @Ri, #data Move immediate data to an indirect address pointed by Ri. MOV @R0, #33H ; Move immediate data 33H to location pointed
by R0

MOV DPTR, Load 16-bit immediate data to data pointer register DPTR. MOV DPTR, #0A5F0H ; Load DPTR with 0A5F0H
#data16
MOVC : Different types (ROM)
Instruction Description Example
MOVC A, @A+DPTR Moves a byte from code memory (ROM) to MOV DPTR, #3000H ; Load
the accumulator A. The address is calculated DPTR with 3000H
by adding the contents of DPTR and A. MOV A, #02H ; Load A with
02H
MOVC A, @A+DPTR ; Move
byte from (3000H + 02H) to A
MOVC A, @A+PC Moves a byte from code memory to the MOV A, #04H ; Load A with
accumulator A. The address is calculated by 04H
adding the contents of the PC and A. MOVC A, @A+PC ; Move byte
from (PC + 04H) to A
MOVX : Different types (Ext-RAM)
Instruction Description Example
MOVX A, @DPTR Moves a byte from external data memory MOV DPTR, #8000H ; Load DPTR with
pointed by the Data Pointer Register (DPTR) external memory address 8000H
to the accumulator A. MOVX A, @DPTR ; Move byte from external
memory to A
MOVX @DPTR, A Moves a byte from the accumulator A to MOV DPTR, #8000H ; Load DPTR with
the external data memory location pointed external memory address 8000H
by DPTR. MOVX @DPTR, A ; Move byte from A to
external memory
MOVX A, @Ri Moves a byte from external data memory MOV R0, #00H ; Load R0 with address low
pointed by the indirect register (R0 or R1) byte
to the accumulator A. MOVX A, @R0 ; Move byte from external
memory to A using R0
MOVX @Ri, A Moves a byte from the accumulator A to the MOV R1, #00H ; Load R1 with address low
external data memory location pointed by byte
the indirect register (R0 or R1). MOVX @R1, A ; Move byte from A to
external memory using R1
Write a programme to add the contents of the two memory locations and store in
another memory location ([Link] and [Link])
2000
2001
20
2002
21
22
MOV DPTR,#2000H

MOVX A,@DPTR

MOV A,20H MOV R0,A

INC DPTR
ADD A,21H
MOVX A,@DPTR

MOV 22H,A ADD A,R0

INC DPTR

MOVX @DPTR,A
Push and POP instructions
PUSH direct
// (1) Increment SP, (2) copy data from address direct to address in SP

POP direct
// (1) copy data from address in SP to address direct, and (2) then decrement SP

For example,

MOV SP, #50H // initialize SP to point to address 50H


MOV 35H, #10H // load data 10H into address 35H
PUSH 35H // Increment SP to 51H, copy contents of address 35H to address
51H i.e. (51H) = 10H
POP 00H // copy data from address 51H to address 00H, i.e. (00H) =10H and
decrement SP to 50H.
07F
Another example
DATA EXCHANGE: Bi-directional data exchange
Instruction Description
XCH A, Rn Exchanges the contents of the accumulator A with
the register Rn (R0-R7).
XCH A, direct Exchanges the contents of the accumulator A with
the direct address specified.
XCH A, @Ri Exchanges the contents of the accumulator A with
the data memory location pointed by the indirect
register (R0 or R1).
XCHD A, @Ri Exchanges the lower nibble of the accumulator A
with the lower nibble of the data memory location
pointed by the indirect register (R0 or R1).
DATA EXCHANGE: Bi-directional data exchange
Instruction Description Example
XCH A, Rn Exchanges the contents of the MOV A, #0AH ; Load A with 0AH
MOV R1, #0BH ; Load R1 with 0BH
accumulator A with the register Rn (R0-
XCH A, R1 ; Exchange A with R1 (A=0BH, R1=0AH)
R7).
XCH A, direct Exchanges the contents of the MOV A, #0CH ; Load A with 0CH
MOV 20H, #0DH ; Load RAM location 20H with 0DH
accumulator A with the direct address
XCH A, 20H ; Exchange A with 20H (A=0DH, 20H=0CH)
specified.
XCH A, @Ri Exchanges the contents of the MOV A, #0EH ; Load A with 0EH
MOV R0, #30H ; Point R0 to RAM location 30H
accumulator A with the data memory
MOV @R0, #0FH ; Load 30H with 0FH
location pointed by the indirect register XCH A, @R0 ; Exchange A with @R0 (A=0FH, 30H=0EH)
(R0 or R1).
XCHD A, @Ri Exchanges the lower nibble of the MOV A, #0F1H ; Load A with 0F1H
accumulator A with the lower nibble of MOV R1, #40H ; Point R1 to RAM location 40H
the data memory location pointed by the MOV @R1, #0F2H ; Load 40H with 0F2H
indirect register (R0 or R1). XCHD A, @R1 ; Exchange lower nibble of A with
lower nibble of 40H (A=0F2H, 40H=0F1H)
Example: XCH
XCHD A, @Ri Exchanges the lower nibble of the
accumulator A with the lower nibble of
Example: XCHD
the data memory location pointed by
the indirect register (R0 or R1). Don’t try with B

Before After
Total instruction set (255)---(111)

Data transfer Arithmetic Logical Boolean


instructions instructions Instructions Instructions

Control Branching Rotate Shift


Instructions Instructions Instructions Instructions

Call
Instructions
Branching Instructions
•Program-Flow Control Instructions

No more sequence operations


Jump and call
instructions
JMP Start
CALL
Start

Subroutine
CALL

Push

RET
End
POP
SJMP
End
AJMP
LJMP
Start
CALL Three subroutines

Stack stores three addresses

Subroutine
CALL

Push

RET
POP

End
JZ (Jump if Zero)
JNZ (Jump if Not Zero)
JC (Jump if Carry)
Conditional JNC (Jump if No Carry)
JB (Jump if Bit is Set)
jumps
JNB (Jump if Bit is Not Set)
JBC (Jump if Bit is Set and Clear Bit)
CJNE (Compare and Jump if Not Equal)
DJNZ (Decrement and Jump if Not Zero)
CJNE (Compare and Jump if Not Equal)
CJNE X, Y Label

CJNE A, direct, rel Byte Jumps


Compare A with contents of address direct and jump to relative address
rel if they are not equal.
If A is less than contents of address direct, set carry flag to 1, otherwise
clear to 0.
MOV A, #05H
MOV 30H, #03H
CJNE A, 30H, TARGET
; Compare A with the content of address
30H and jump if not equal
CJNE A, #data, rel Byte Jumps

Compare A with immediate value data and jump to relative address rel if
they are not equal. If A is less than immediate value data, set carry flag to
1, otherwise clear to 0.

MOV A, #05H
CJNE A, #03H, TARGET
; Compare A with immediate value 03H and jump
if not equal.
CJNE Rn, #data, rel Byte Jumps

Compare Rn with immediate value data and jump to relative address rel
if they are not equal. If Rn is less than immediate value data, set carry
flag to 1, otherwise clear to 0.

MOV R0, #05H


CJNE R0, #03H, TARGET
; Compare R0 with 03H and jump
if not equal.
CJNE @Ri, #data, rel Byte Jumps

Compare contents of address in Ri with immediate value data and jump to relative
address rel if they are not equal. If contents of address in Ri is less than immediate
value data, set carry flag to 1, otherwise clear to 0.

MOV R0, #20H


CJNE @R0, #10H, TARGET
; Compare the content at address pointed by R0 with 10H and jump if not equal.
Instruction Description Example
Compare A with contents of address direct and jump to MOV A, #05H
CJNE A, relative address rel if they are not equal. If A is less than MOV 30H, #03H
direct, rel contents of address direct, set carry flag to 1, otherwise CJNE A, 30H, TARGET
clear to 0. ; Compare A with the content of address 30H and
jump if not equal.
Compare A with immediate value data and jump to relative MOV A, #05H
CJNE A, address rel if they are not equal. If A is less than immediate CJNE A, #03H, TARGET
#data, rel value data, set carry flag to 1, otherwise clear to 0. ; Compare A with immediate value 03H and jump
if not equal.
Compare Rn with immediate value data and jump to relative MOV R0, #05H
CJNE Rn, address rel if they are not equal. If Rn is less than immediate CJNE R0, #03H, TARGET
#data, rel value data, set carry flag to 1, otherwise clear to 0. ; Compare R0 with 03H and jump if not equal.
Compare contents of address in Ri with immediate value MOV R0, #20H
CJNE @Ri, data and jump to relative address rel if they are not equal. If CJNE @R0, #10H, TARGET
#data, rel contents of address in Ri is less than immediate value data, ; Compare the content at address pointed by R0
set carry flag to 1, otherwise clear to 0. with 10H and jump if not equal.
Byte Jumps
DJNZ (Decrement and Jump if Not Zero)

DJNZ count, Label

No flags effected
Byte Jumps
DJNZ Rn, rel (Decrement and
Jump if Not Zero using Register)

Instruction: DJNZ R0, TARGET

MOV R0, #05H ; Load the value 5 into register R0


DJNZ R0, TARGET ; Decrement R0 by 1 and jump to TARGET if the result is not zero
NOP ; No operation (this line is skipped if R0 is not zero)
TARGET: ; Target label where control jumps if R0 is not zero
; Additional instructions can go here
DJNZ Rn, rel (Decrement and Jump if Not Zero using Register)

I want to create a loop of 256 iterations

For 9 iterations, you make R0=10

R0=FF---255 R0=00----256
DJNZ direct, rel (Decrement and Jump if Not Zero
using Direct Addressing)

Instruction: DJNZ 30H, TARGET


MOV 30H, #05H ; Load the value 5 into the
memory location 30H

DJNZ 30H, TARGET ; Decrement the content


of 30H by 1 and jump to TARGET if not zero

NOP ; No operation (this line is skipped


if 30H is not zero)

TARGET: ; Target label where control


jumps if 30H is not zero

; Additional instructions can go here


Byte Jumps
JZ rel
// jump to relative address rel if A is 0 (Jump if Zero)
JZ (Jump if Zero)

Description: The JZ instruction checks if the accumulator (A) is zero. If it is, the
program jumps to the address labeled “rel”

MOV A, #00H ; Load the value 0 into the accumulator


JZ TARGET ; Jump to TARGET if A is zero
NOP ; No operation (this line is skipped if A is zero)
TARGET: ; Target label where control jumps if A is zero
; Additional instructions can go here
JNZ (Jump if Not Zero) Byte Jumps

Instruction: JNZ TARGET

Description: The JNZ instruction checks if the accumulator (A) is not zero.
If it is not zero, the program jumps to the address labeled TARGET.

MOV A, #01H ; Load the value 1 into the accumulator


JNZ TARGET ; Jump to TARGET if A is not zero
NOP ; No operation (this line is skipped if A is not zero)
TARGET: ; Target label where control jumps if A is not zero
; Additional instructions can go here
JC (Jump if Carry) Bit Jumps
Instruction: JC TARGET

Description: The JC instruction checks if the carry flag is set (carry = 1). If the carry flag is
set, the program jumps to the address labeled TARGET. Else next instruction execute

MOV A, #0FFH ; Load the value 255 (0xFF) into the accumulator
ADD A, #01H ; Add 1 to A, resulting in overflow, which sets the carry flag
JC TARGET ; Jump to TARGET if the carry flag is set
NOP ; No operation (this line is skipped if the carry flag is set)
TARGET: ; Target label where control jumps if the carry flag is set
; Additional instructions can go here
JNC (Jump if No Carry) Bit Jumps
Instruction: JNC TARGET.

Description: The JNC instruction checks if the carry flag is not set (carry = 0). If the carry
flag is not set, the program jumps to the address labeled TARGET

MOV A, #0FH ; Load the value 15 into the accumulator


ADD A, #01H ; Add 1 to A, carry flag remains clear (no overflow)
JNC TARGET ; Jump to TARGET if the carry flag is not set
NOP ; No operation (this line is skipped if the carry flag is not set)
TARGET: ; Target label where control jumps if the carry flag is not set
; Additional instructions can go here
JB (Jump if Bit is Set) Bit Jumps
Instruction: JB bit , TARGET

Description: The JB instruction checks if a specified bit is set (1). If the bit is set,
the program jumps to the address labeled TARGET.

SETB P1.0 ; Set bit 0 of Port 1 (P1.0) to 1


JB P1.0, TARGET ; Jump to TARGET if P1.0 is set
NOP ; No operation (this line is skipped if P1.0 is set)
TARGET: ; Target label where control jumps if P1.0 is set
; Additional instructions can go here
JNB (Jump if Bit is not Set) Bit Jumps
Instruction: JNB bit , TARGET

Description: The JB instruction checks if a specified bit is not set (0). If the bit is
not set, the program jumps to the address labeled TARGET.

CLR P1.0 ; Set bit 0 of Port 1 (P1.0) to 1


JNB P1.0, TARGET ; Jump to TARGET if P1.0 is not set
NOP ; No operation (this line is skipped if P1.0 is set)
TARGET: ; Target label where control jumps if P1.0 is set
; Additional instructions can go here
JBC (Jump if Bit is Set and Clear Bit)

Instruction: JBC bit, TARGET


Description: The JBC instruction checks if a specified bit is set (1). If the bit is set, the
program jumps to the address labeled TARGET and clears the specified bit to 0.

SETB P1.2 ; Set bit 2 of Port 1 (P1.2) to 1


JBC P1.2, TARGET ; Jump to TARGET if P1.2 is set and then clear P1.2
NOP ; No operation (this line is skipped if P1.2 is set)
TARGET: ; Target label where control jumps if P1.2 is set
; Additional instructions can go here
Instruction Description Syntax
JZ (Jump if Zero) Jumps to the specified address if the accumulator is zero. JZ label
JNZ (Jump if Not Zero) Jumps to the specified address if the accumulator is not JNZ label
zero.
JC (Jump if Carry) Jumps to the specified address if the carry flag is set. JC label
JNC (Jump if No Carry) Jumps to the specified address if the carry flag is not set. JNC label
JB (Jump if Bit is Set) Jumps to the specified address if a specified bit is set. JB bit, label
JNB (Jump if Bit is Not Set) Jumps to the specified address if a specified bit is not set. JNB bit, label
JBC (Jump if Bit is Set and Jumps to the specified address if a specified bit is set, and JBC bit, label
Clear Bit) then clears the bit.
CJNE (Compare and Jump if Compares two operands and jumps to the specified address CJNE A, data, label or CJNE
Not Equal) if they are not equal. A, @Ri, label
DJNZ (Decrement and Jump Decrements the specified register or direct byte and jumps to DJNZ reg, label or DJNZ
if Not Zero) the specified address if the result is not zero. direct, label

You might also like