8051 Program for Subtraction of Two 8-Bit Numbers

Program 1: Numbers stored in Register Bank

Problem 1

Suppose the two data bytes are 66H and 55H stored in Register R2 and R3 of the Bank 1. Write an assembly language program for 8051 to subtract number stored in R3 from R2. Store result in Register R4 of Bank 1.

Algorithm

Step 1: Select Bank 1.

Step 2: Load first number from R2 to Accumulator.

Step 3: Subtract second number in R3 from Accumulator.

Step 4: Store Result to R4.

Step 5: Stop.

Flowchart

8051 Program for Subtraction of Two 8-Bit Numbers

Program

CLR PSW.4 ; Clear 4th bit of PSW.

SETB PSW.3 ; Set 3rd bit of PSW,

 ; Above steps are used to select register bank 1.

CLR PSW.7 ; Clear carry flag.

MOV A, R2 ; Copy content of accumulator to accumulator

SUBB A, R3 ; Subtract content of R3 from accumulator and result is stored into accumulator.

MOV R4, A ; Copy the result from accumulator into R4.

LOOP: AJMP LOOP ; Stop.

Program 2: Numbers stored in external memory

Problem 2

Suppose two data bytes are stored in memory location 3000H and 3001H. Write a program to subtract number at 3001H from 3000H from external memory locations and store result in memory location 3002H of the external memory.

Algorithm

Step 1: Initialize memory pointer using DPTR register with 3000H.

Step 2: Load first number from the memory location 3000H.

Step 3: Increment memory pointer by 1.

Step 4: Load second number from memory location 3001H.

Step 5: Subtract second number from first number.

Step 6: Store Result in memory location 3002H by incrementing memory pointer.

Step 7: Stop.

Flowchart

8051 Program for Subtraction of Two 8-Bit Numbers

Program

MOV DPTR, #3000H ; Load 3000h into DPTR.

MOVX A, @DPTR ; Copy the data from 3000h to accumulator indirectly .

MOV R0, A ; Copy content of accumulator into R0.

INC DPTR ; Increment DPTR by 1, now DPTR= 3001h.

MOVX A, @DPTR ; Copy data from 3001h into accumulator.

SUBB A, R0 ; Subtract second number from first, result in accumulator.

INC DPTR ; Increment DPTR by 1, now DPTR= 3002h.

MOVX @DPTR, A ; Copy the result in accumulator to address 3002h.

LOOP: AJMP LOOP ; Stop.

Recent posts

Leave a Comment

Your email address will not be published. Required fields are marked *