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 add these two numbers and store the in same register bank as follows.
R4 – LSB of result
R5 – MSB of result
R6 – Carry if any.
Algorithm
Step 1: Select register bank 2.
Step 2: Initialize Carry Counter to get result > 16 bit.
Step 3: Load LSB of first number in Accumulator.
Step 4: Add LSB of Second number with LSB of first number.
Step 5: Store LSB of result.
Step 6: Load MSB of first number in Accumulator.
Step 7: Add MSB of second number with MSB of first Number.
Step 8: If Carry is not 1, then go to step 10.
Step 9: Increment Carry Counter by 1.
Step 10: Store MSB of result.
Step 11: Store Carry of result.
Step 12: Stop.
Flowchart
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.
MOV R6, #00H ; Initialize R6 as carry counter.
MOV A, R0 ; Load LSB of first number in accumulator.
ADD A, R2 ; Add LSB of second number with accumulator.
MOV R4, A ; Store the LSB of result into R4.
MOV A, R1 ; Load MSB of first number in accumulator.
ADDC A, R3 ; Add MSB of second number with accumulator and carry.
JNC NEXT ; Jump on next if carry=0.
INC R6 ; If carry=1, increment counter by 1.
NEXT: MOX 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