Table of Contents

Toggle## 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

## Program

SETB PSW.4 ; Set 4^{th} bit of PSW.

CLR PSW.3 ; Clear 3^{rd} 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

### Related posts:

- Assembler Directives in 8051 Microcontroller
- Features of 8051 Microcontroller
- Memory Organization of 8051 Microcontroller
- Addressing Modes in 8051 Microcontroller
- Instruction Set in 8051 Microcontroller
- Architecture of 8051 Microcontroller
- PSW Register in 8051 Microcontroller | Program Status Word
- Pin Configuration of 8051 Microcontroller
- The Stack and Stack Pointer in 8051 Microcontroller
- Stack Pointer Data Pointer and Program Counter in 8051 Microcontroller
- Functions of Timing and Control Unit of 8051
- Functions of Ports in 8051 Microcontroller
- Port Structure of 8051 Microcontroller
- Reset Circuit of 8051 Microcontroller
- Registers in 8051 Microcontroller
- Boolean Processor in 8051 Microcontroller
- Power Saving Mode in 8051 | 8051 Power Down and Idle Mode
- 8051 Microcontroller Family
- Program Development Steps in ALP
- Addition of Two 8-Bit Numbers in 8051 Microcontroller
- Addition of Two 8 Bit Numbers with Carry in 8051 Microcontroller
- 8051 Program for Addition of Two 16 Bit Numbers
- 8051 Program to Add an Array of Numbers
- 8051 Program for Subtraction of Two 8-Bit Numbers
- 8051 Program to Multiply two 8 Bit numbers