Microprocessors and Microcontrollers: Unit III: (b) 8051 Instruction Set and Programming

Program Examples

8051 Instruction Set and Programming

Review Question : 1. Write an 8051 based assembly language program for performing four basic arithmetic operations on two data.

Program Examples

Program 15.10.1 : Program to load accumulator A, DPH and DPL with 30H

MOV A, #30H ; Loads 30H in A register

MOV DPH, A ; (DPH) ← (A)

MOV DPL, A; (DPL) ← (A)

 

Program 15.10.2 : Copy byte in SCON to register R3.

Method 1 : Using direct address for SCON (98H)

MOV R3, 98H ; Copy SCON to R3

Method 2 : Using direct address for SCON (98H) and R3 (03H)

MOV 03H, 98H   ; Copy SCON to R3

Method 3 : Using indirect address for R3

MOV R1, # 03H  ; Initialize pointer to R3

MOV @ R1, 98H ; Copy SCON to R3

Method 4 : Using PUSH instruction

MOV 81H, #02H ; Set the SP to address 02H in RAM

PUSH 98H ; Push SCON (98H) to address (03H)

 

Program 15.10.3 : Put the number 90H in R2 and R3

Method 1 : Use immediate addressing mode

MOV R2, #90H

MOV R3, #90H

Method 2 : Use immediate and register addressing

MOV R2, #90H

MOV R3, R2

 

Program 15.10.4 : Add two 8-bit numbers.

MOV A, #30H ; (A) ← 30

ADD A, #50H ; (A) ← (A) + 50H

 

Program 15.10.5 : Add two 16-bit numbers.

MOV DPTR, #2040H ; (DPTR) ← 2040H (16-bit number)

MOV A, #2BH ; (A) ← 2BH (lower byte of second 16-bit number)

MOV B, #20H ; (B) ← 20H (Higher byte of second 16-bit number)

ADD A, DPL ; Add lower bytes

MOV DPL, A ; Save result of lower byte addition

MOV A, B  ; Get higher byte of second number in A

ADDC A, DPH ; Add higher bytes with any carry from lower byte addition

MOV DPH, A ; Save result of higher byte addition

 

Program 15.10.6 : Find the 2's complement of a number in R0.

MOV A, R0 ; (A) ← (R0)

CPL A ; 1's complement A

ADD A, #01         ; Add 1 to it to get 2's complement

 

Program 15.10.7 : Unpack the packed BCD number stored in the accumulator and save the result in R0 and R1 such that (R0) ← LSB and (R1) ← MSB.

MOV B, A ; Save the packed BCD number

ANL A, #0FH ; Mask upper nibble of BCD number

MOV R0, A         ; Save the lower digit

MOV A, B  ; Get the packed BCD number

ANL A, #0F0H ; Mask lower nibble of BCD number

SWAP A ; Exchange the lower and upper nibbles

MOV R1,A ; Save the upper digit.

 

Program 15.10.8 : Subtract two 8-bit numbers and exchange digits.

MOV A, #9F ; Get the first number in A

MOV R0, #40 ; Get the second number in R0

CLR C ; Clear carry

SUBB A, R0 ; A ← A-(R0)

SWAP A ; Exchange digits

 

Program 15.10.9 : Subtract the contents of R1 of Bank0 from the contents of R0 of Bank2.

MOV PSW, #10 ; Select Bank2

MOV A, R0         ; (A) ← (R0) from Bank2

MOV PSW, #00 ; Select Bank0

CLR C ; Clear carry

SUBB A,R1         ; A ← A-(R1) from Bank0

 

Program 15.10.10 : Division of two 8-bit numbers.

MOV A,#90 ; Get the first number in A

MOV B,#20         ; Get the second number in B

DIV AB ; A + B, Remainder in B and Quotient in A

 

Program 15.10.11 : Multiply two 8-bit numbers.

MOV A, #8F ; Get the first number in A

MOV B, #79 ; Get the second number in B

MUL AB ; A × B, Higher byte of result in B and lower byte of result in A

 

Program 15.10.12 : Program to convert 8-bit binary number to its equivalent BCD.

Program Logic :

Step 1 : Divide number with 100 decimal and save quotient i.e. save hundred's digit.

Step 2 : Make remainder as a new number.

Step 3 : Divide number with 10 decimal and save quotient i.e. save tens digit.

Step 4 : Save remainder as ones digit.

Sample Example :


Program :

MOV A, #76H ; Load the binary number in A

MOV B, #100 ; Load B with 100 decimal

DIV AB ; Divide number with 100

MOV R0 ,A         ; Save the hundreds of the number

; (Quotient of the previous division)

MOV A, B ; Get the remainder

MOV B, #10 ; Load B with 10 decimal

DIV AB ; Divide number with 10

MOV R1, A ; Save the tens of the number

MOV R3, B ; Save the ones of the number


 

Program 15.10.13 : To add two 16-bit BCD numbers.

Program :


MOV DPTR, #1234H ; Load first number

MOV R0, #20H ; Load lower byte of second number

MOV R1, #30H   Load higher byte of second number

MOV A, R0         ; Get the lower byte of second number

ADD A, DPL ; Add two lower bytes

DA A          ; Adjust result to valid BCD

MOV DPL, A ; Store the sum of lower bytes

MOV A, R1 ; Get the higher byte of second number

ADDC A, DPH ; Add two higher bytes considering

; carry of lower byte addition

DA A          ; Adjust result to valid BCD

MOV DPH, A ; Store the sum of higher bytes

 

Program 15.10.14 : Implementing a BCD multiply using MUL and DIV

MULBCD:  UNPACK TWO BCD DIGITS RECEIVED IN ACC, FIND THEIR PRODUCT, AND RETURN PRODUCT IN PACKED BCD FORMAT IN ACC

MULBCD : MOV B, #10H ; DIVIDE INPUT BY 16

DIV AB ; A AND B HOLD SEPARATED DIGITS

; (EACH RIGHT JUSTIFIED IN REGISTER)

MUL AB ; A HOLDS PRODUCT IN BINARY FORMAT

; (0 - 99(DECIMAL) = 0 - 63H)

MOV B, #10 ; DIVIDE PRODUCT BY 10

DIV AB ; A HOLDS # OF TENS, B HOLDS REMAINDER

SWAP A

ORL A, B ; PACK DIGITS

RET

 

Program 15.10.15 : Subtract two 16-bit numbers.

MOV DPTR, #9080 ; (DPTR) ← 9080H (16-bit number)

MOV B, #50H ; (B) ←  50H Higher byte of second number

MOV A, #40H ; (A) ←  40H Lower byte of second number

CLR C ; Clear carry

SUBB A, DPL ; Subtract lower bytes

MOV DPL, A ; Save result of lower byte subtraction

MOV A,B ; Get the higher byte

SUBB A, DPH ; Subtract higher byte with borrow

MOV DPH, A ; Save result of higher bytes subtraction

 

Program 15.10.16 : Generate BCD up counter and send each count to port A.

MOV A, #00 ; Initialize counter

BACK : MOV P1, A ; Send count to port A

ADD A, #01 ; Increment counter

DA A ; Decimal adjust the counter

AJMP BACK ; Jump BACK

 

Program 15.10.17 : Find the maximum number from a given 8-bit ten numbers.

Flowchart :


MOV DPTR, #2000 ; Initialize pointer to memory where numbers are stored

MOV R0, #0A ; Initialize counter

MOV R3, #00 ; Maximum = 0

AGAIN : MOVX A, @DPTR ; Get the number from memory

CJNE A, R3, NE ; Compare number with maximum number

AJMP SKIP ; If equal go to SKIP

NE : JC SKIP ; If not equal check for carry, if carry go to SKIP

MOV R3, A ; Otherwise maximum = number

SKIP : INC DPTR ; Increment memory pointer

DJNZ R0, AGAIN ; Decrement count, if count = 0 stop otherwise go to AGAIN


Program 15.10.18 : Arrange the given ten 8-bit numbers in the ascending order.

Flowchart :


Program

MOV R0, #09 ; Initialize counter1

AGAIN : MOV DPTR, #2000H ; Initialize memory pointer

MOV R1, #09 ; Initialize counter2

BACK : MOV R2, DPL ; Save lower byte of memory address

MOVX A, @DPTR ; Get the number

MOV B, A ; Save the number

INC DPTR ; Increment memory pointer

MOVX A, @DPTR ; Get the next number

CJNE A,B,NE ; If not equal check

; for greater or less

AJMP SKIP         ; Otherwise go to skip

NE : JNC SKIP ; If

MOV DPL, R2 ; [ Exchange

MOVX @DPTR, A ; the contents

INC DPTR ; of two

MOV A,B ; memory

MOVX @DPTR, A ; locations ]

SKIP : DJNZ R1, BACK ; If R1 not equal to 0 go to BACK

DJNZ RO, AGAIN ; If R0 not equal to 0 go to AGAIN

 

Program 16.10.19 : Count number of one's in a number.

Flowchart :


Program :

MOV R2, #0 ; Initialize one’s

; counter = 0

MOV R1, #08 ; Initialize iteration

; count

MOV R0, #56 ; Load number

MOV A, R0 ; Get the number in accumulator

BACK : RRC A ; Rotate A and CY ← LSB

JNC SKIP ; If carry is not zero go to skip

INC R2 ; Otherwise increment

; one’s counter

SKIP : DJNZ R1, BACK ; Decrement iteration

; count and if not

; zero repeat

 

Program 15.10.20 : To find the sum of 10 numbers stored in the array.

Statement : Calculate the sum of series of numbers. The length of the series is in memory location 2200H and the series itself begins from memory location 2201H.

a. Assume the sum to be 8-bit number so you can ignore carries. Store the sum at memory location 2300H.

b. Assume the sum to be 16-bit number. Store the sum at memory locations 2300H and 2301H.

a. Sample problem

2200H = 04H

2201H = 20H

2202H = 15H

2203H = 13H

2204H = 22H

Result = 20 + 15 + 13 + 22 = 6AH

2300H = 6AH

Program:

a) MOV DPTR, #2200H ; Initialize memory pointer

MOVX A, @DPTR ; Get the count

MOV R0, #10 ; Initialize the iteration

; counter

INC DPTR ; Initialize pointer to array of

; numbers

MOV R1, #00 ; Result = 0

BACK: MOVX A, @DPTR ; Get the number

ADD A, R1; A ← Result + A

MOV R1, A ; Result ← A

INC DPTR ; Increment the array pointer

DJNZ R0, BACK ; Decrement iteration count

; if not zero repeat

MOV DPTR, #2300H ; Initialize memory pointer

MOV A, R1; Get the result

MOVX @DPTR, A ; Store the result

b. Sample problem

2200H = 04H   2201H = 9AH

2202H = 52H   2203H = 89H  2204H = 3EH

Result = 9AH + 52H + 89H + 3EH = 1B3H

 2300H = B3H Lower byte  2301H = 01H Higher byte

Flowchart :


Program :

b) MOV DPTR, #2200H ; Initialize memory pointer

MOVX A, @DPTR ; Get the count

MOV RO, #10 ; Initialize the iteration counter

INC DPTR ; Initialize pointer to array of numbers

MOV R2, #00 ; [Make

MOV R1, #00 ; result = OOH]

BACK : MOVX A, @DPTR ; Get the number

ADD A, R1 ; A ← Result + A

MOV R1, A ; Result ← A

ADDC R2, #00 ; If carry exists, add it to MSD

INC DPTR ; increment the array pointer

DJNZ R0, BACK ; Decrement iteration

; count if not zero repeat

MOV DPTR, #2300H ; Initialize memory pointer

MOV A, R1 ; Get the lower byte of result

MOVX @DPTR, A ; Store the lower byte of result

INC DPTR ; Increment memory pointer

MOV A, R2 ; Get the higher byte of result

MOVX @DPTR, A ; Store the higher byte of result


Program 15.10.21 : Data transfer from memory block Bl to memory block B2.

Statement : Assume two blocks are non-overlapped.

Flowchart :


Program :

MOV R2, #1 ; Initialize iteration counter

MOV R1, #20H ; Initialize source memory pointer

MOV R0, #30H ; Initialize destination memory pointer

BACK : MOV A, @R1 ; Get data

MOV @R0, A ; Store data

INC R1 ; Increment source memory pointer

INC R0 ; Increment destination memory pointer

DJNZ R2, BACK ; Decrement iteration count and if not zero repeat

 

 Program 15.10.22 : Write a program to load accumulator with values 55H and complement 70 times.

MOV RO, #70 ; Initialize iteration count

MOV A, # 55H ; load 55H in accumulator

HERE : GPL A ; Complement accumulator

DJNZ R0, HERE ; Repeat till R0 = 0

 

Program 15.10.23 : Write an 8051 assembly language program to copy the value 55H into RAM memory locations 40H to 45H using register indirect addressing with a loop.

AU : Dec.-12, Marks 8

MOV R0, #06H ; Initialize iteration count

MOV R1, #40H ; Initialize memory pointer

MOV A, #55H ; Load data byte in A

BACK : MOV @R1, A ; Copy data byte into RAM memory location

INC R1 ; Increment memory pointer

DJNZ R0, BACK ; Decrement iteration count and if not zero repeat

 

Program 15.10.24 : Write an 8051 assembly language program to clear the accumulator and add 3 to the accumulator 10 times.

Solution :

CLR A ; Clears the accumulator (A ← 0)

MOV R0, #0AH ; Set the iteration count

BACK : ADD A, #03H ; Add 3 to accumulator

DJNZ R0, BACK ; Decrement iteration count and if not zero repeat

Review Question

1. Write an 8051 based assembly language program for performing four basic arithmetic operations on two data.

Microprocessors and Microcontrollers: Unit III: (b) 8051 Instruction Set and Programming : Tag: : 8051 Instruction Set and Programming - Program Examples