8051 Instruction Set

24
8051 Instruction Set

description

8051

Transcript of 8051 Instruction Set

8051

Instruction Set

8051: Frequently Used Registers

• A = Accumulator

• B = Accumulator used for multiplication/Division

• PC = Program Counter

• DPTR = DPH + DPL = Data Pointer

Instruction Set

• Copy

• Arithmetic

• Logic

• Bit Manipulation

• Redirect

• Control

Addressing Modes

• Immediate

MOV A, #32H

MOV B, #0F2H

MOV DPTR, #20FFH

• Register

MOV A, R7

MOV R5, A

MOV R1, R2 ; Illegal - Not allowed

Addressing Modes Contd.- Accessing Memory• Direct Addressing

MOV A, R2 is same as MOV A, 2

MOV P1, A is same as MOV 90H, A

• Stack and Direct Addressing Mode

PUSH A is invalid

PUSH 0E0H is valid

PUSH 05 - Push R5 on the stack

Indirect Addressing Mode: Using R0 and R1• MOV A, @R0 ; Copy contents of memory

; whose address is in R0

• MOV @R1, A ; Copy A into memory

; location pointed by R1

• This is limited only to R0 and R1

MOV A, @ R2 is invalid

Use of Indirect Addressing Mode in Setting Up a Loop:Example: Write a program to clear 20 R/W

memory locations starting from memory 50H.

Program: CLR A ; Clear A

MOV R1, #50H ; Set up R1 as a pointer

MOV R5, # 20 ;Set up R5 as a counter

REPEAT: MOV @R1, A ;Clear memory

INC R1;Point to next me memory

DJNZ R5, REPEAT

Using DPTR as a 16-bit Memory Pointer

MOV DPTR, #100H ; Set up DPTR as a pointer

CLR A

MOVC A, @ A + DPTR ;Copy from Code memory into A

Note Instruction: MOVC – Copy from code memory

Problem:

Write a program to transfer a string of data

terminated in 00 from code memory location 200H

to RAM location 50H.

Program:

MOV DPTR, #200H ;Initialize

pointer to ROM

MOV R0, #50H

;Initialize pointer to RAM

REPEAT: CLR A

MOV A, @A+DPTR

JZ EXIT

MOV @R0, A

INC DPTR

INC R0

SJMP REPEAT

HERE: SJMP HERE

Jump Instructions:

Unconditional:

• LJMP (Long Jump – 3 bytes –anywhere from 0000 to FFFFH)

Example: LJMP START

• SJMP (Short Jump 2 bytes – Opcode followed by an 8-bit signed number - limited to 00 to FFH – Backward and Forward - )

• All conditional Jump instructions are short jumps.

• JZ ; Short Jump if A = 0

• JNZ ; Short Jump if A =/ 0

• DJNZ ; Decrement and Jump if register =/ 0

• CJNE ; Compare and Jump if not equal

CJNE A, data

CJNE Reg, #data

Conditional Jump Instructions:

Signed and Unsigned Number

• Unsigned FA H = 25010

• Signed FA H = It is a negative number in 2’s

Complement

• 2’ Complement of FAH = - 06H

CY AC OV P

PSW.7 PSW.6 PSW.2 PSW.0

Find the flag status after the following operation:

MOV A, #57H

MOV R2, # 3AH

ADD A, R2

Single Bit Instructions

• SETB bit ; Set bit = 1

• CLR bit ; Clear bit

• CPL bit ;Complement the bit

• JB bit, Label; Jump if Bit = 1

• JNB bit, Label; Jump if bit = 0

• JBC bit, Label; Jump if bit = 1 and clear bit

Examples of Bit Addressing:

• SETB 42H ;Set Bit2 in R/W Memory Location 28H

• CLR 67H ;Set Bit7 in R/W Memory Location 2CH

• JB 72H, SKIP; Jump to SKIP if Bit2 =1 in Location 2EH

• JNB 28H, BACK; Jump to BACK if Bit0 =1 in Location 25H

• SETB 08H ;

Addresses of Selected Bit Addressable SFRs: Range: 80H to FFH

• Port P0.0 = 80H -------------- Port P0.7 = 87H

• Port P1.0 = 90H -------------- Port P1.7 = 97H

• Port P2.0 = A0H ------------ Port P2.7 = A7H

• Port P3.0 = B0H ------------ Port P3.7 = B7H

• TCON: Timer Control 88H – 8FH

• SCON: Serial Control -> 98H – 9FH

Examples of Bit Addressing:

• Read switch connected at bit7 from Port P0, and turn on/off LED connected to bit0 of Port P1

MOV C, P0.7 ;Copy/Read bit P0.7in Carry

MOV P1.0, C ;Turn On/Off LED at P1.0

• Using Bit Directive

SW BIT P0.7LED BIT P1.0

HERE: MOV C, SWLED , CSJMP HERE

Examples of Bit Addressing Contd.:

• Using EQU Directive

SW EQU P0.7LED EQU P1.0

HERE: MOV C, SWLED , CSJMP HERE