
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Address | Value |
---|---|
… | |
20H | 73H |
21H | BDH |
… | |
30H | 01H |
31H | B6H |
… |
Advertisements