Program to Subtract two 8 Bit numbers in 8051 Microprocessor


Now, in this section we will see how to subtract two 8-bit numbers using 8051 microcontroller. The register A (Accumulator) is used as one operand in the operations. There are seven registers R0 – R7 in different register banks. We can use any of them as second operand.

We are taking two number 73H and BDH at location 20H and 21H, After subtracting the result will be stored at location 30H and 31H.

Address
Value
 

20H
73H
21H
BDH
 

30H
00H
31H
00H
 


Program

      MOV R0, #20H ; set source address 20H to R0
      MOV R1, #30H ; set destination address 30H to R1
      MOV A, @R0 ; take the value from source to register A
      MOV R5, A ; Move the value from A to R5
      MOV R4, #00H ; Clear register R4 to store borrow
      INC R0 ; Point to the next location
      MOV A, @R0 ; take the value from source to register A
      MOV R3, A ; store second byte
      MOV A, R5 ;get back the first operand
      SUBB A, R3 ; Subtract R3 from A
      JNC SAVE
      INC R4 ; Increment R4 to get borrow
      MOV B, R4 ; Get borrow to register B
      MOV @R1, B ; Store the borrow first
      INC R1 ; Increase R1 to point to the next address
SAVE: MOV @R1, A ; Store the result
HALT: SJMP HALT ; Stop the program

So by subtracting 73H – BDH, the result will be B6H. At location 30H, we will get 01H. This indicates that the result is negative. The get the actual value from result B6H, we have to perform 2’s complement operation. After performing 2’s Complement, the result will be -4AH.

Output

AddressValue
 
20H73H
21HBDH
 
30H01H
31HB6H
 

Updated on: 09-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements