Table of Contents
ToggleIn this article two Assembly language programs for 8051 microcontroller are explained to find 2’s complement of an 8 bit Number.
8051 Program to find 2’s complement of an 8 bit Number
To find 2's complement of a number stored in register bank
Problem 1
Write an assembly language program to find 2’s complement of a number stored in Register R0 of Register Bank 0 and store the result in R1 of same bank of for 8051 microcontroller.
Algorithm
Finding 2’s Complement of a Number
- Select the register bank using PSW register.
- Load the number from Register R0.
- Invert all the bits of the number to get the 1’s complement.
- Add 1 to the 1’s complement to obtain the 2’s complement.
- Store the result in Register R1.
- Stop
Program
CLR PSW.4 ; Clear bit 4 of PSW register.
CLR PSW.3 ; Select register bank 0.
MOV A, R0 ; Load the number from R0 to Accumulator A.
CPL A ; Complement all bits to get 1’s complement.
INC A ; Increment the 1’s complement to get 2’s complement.
MOV R1, A ; Store the 2’s complement in R1.
Loop: AJMP Loop ; End of program.
Note:
Apart from using INC A instruction you can use ADD A,#01H instruction, which will increase the contents of accumulator by 1.
Tutorial on 8051 Program to Find 2’s complement
To find 2's complement of a number stored in external memory
Problem 2
Write an assembly language program to find 2’s complement of a number stored in memory location 3000H of external memory of 8051 microcontroller and store the result in 3001H.
Algorithm
Finding 2’s Complement of a Number
- Initialize the memory pointer.
- Load the number from memory location 3000H.
- Invert all the bits of the number to get the 1’s complement.
- Add 1 to the 1’s complement to obtain the 2’s complement.
- Store the result in memory location 3001H.
- End the program.
Program
MOV DPTR, #3000H ; Load address of memory location 3000H to DPTR.
MOVC A, @DPTR ; Move data from memory to Accumulator A.
CPL A ; Complement all bits to get 1’s complement.
INC A ; Increment the 1’s complement to get 2’s complement.
INC DPTR ; Load address of memory location 3001H to DPTR.
MOV @DPTR, A ; Store the 2’s complement in memory location 3001H.
Loop: AJMP Loop ; End of program.
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
- 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 16 bit Numbers
- 8051 Program to Multiply two 8 Bit numbers
- 8051 Program to Divide two 8 Bit numbers
- 8051 Program to Find the Largest Number in An Array
- 8051 Program to Find the Smallest Number in An Array Stored in Internal Memory
- Interrupts of 8051 Microcontroller
- 8051 Program to Arrange Numbers in Ascending Order
- Program For 8051 Microcontroller to Unpack A Number