8051 Program for Subtraction of Two 16 bit Numbers

Problem

Two 16-bit numbers are stored in register bank 2 as follows.

R0 – LSB of first number

R1 – MSB of first number

R2 – LSB of second number

R3 – MSB of second number

Write an assembly language program for 8051 microcontroller to subtract second number from first and store the result in same register bank as follows.

R4 – LSB of result

R5 – MSB of result.

Algorithm

Step 1:   Select register bank 2.

Step 2:   Load LSB of first number in Accumulator.

Step 3:   Subtract LSB of Second number from LSB of first number.

Step 4:   Store LSB of result.

Step 5:   Load MSB of first number in Accumulator.

Step 6:   Subtract MSB of second number from MSB of first Number.

Step 7: Store MSB of result.

Step 8:  Stop.

Flowchart

flowchart for 8051 Program for Subtraction of Two 16-bit Numbers using 8051 microcontroller

Program

SETB PSW.4                         ; Set 4th bit of PSW.

CLR PSW.3                           ; Clear 3rd bit of PSW.

                                             ; Above steps are performed to select register bank 2.

CLR PSW.7                           ; Clear carry flag.

MOV A, R0                          ; Load LSB of first number in accumulator.

SUBB A, R2                          ; Subtract LSB of second number from accumulator.

MOV R4, A                          ; Store the LSB of result into R4.

MOV A, R1                          ; Load MSB of first number in accumulator.

SUBB A, R3                          ; Subtract MSB of second number from accumulator.

MOV R5, A                          ; Store MSB of result to R5.

LOOP: AJMP LOOP             ; Stop.

Recent posts

Leave a Comment

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