16-Bit Subtraction
16-bit subtraction is the subtraction of one 16-bit value from another. A subtraction of this nature results in another 16-bit value. Why? The number 65535 is a 16-bit value. If we subtract 1 from it, we have 65534 which is also a 16-bit value. Thus any 16-bit subtraction will result in another 16-bit value.
Let's consider the subtraction of the following two decimal values: 8923 - 6905. The answer is 2018. How do we go about subtracting these values with the 8051? As is the case with addition, the first step is to convert the expression to hexadecimal. The above decimal subtraction is equivalent to the following hexadecimal subtraction: 22DB - 1AF9.
Again, we'll go back to the way we learned it in primary school:
First we subtract the second value in the 1's column (low byte): DB - F9. Since F9 is greater than DB, we need to "borrow" from the 256's column. Thus we actually perform the subtraction 1DB - F9 = E2. The value E2 is what we leave in the 1's column.
Now we must perform the subtraction 22 - 1A. However, we must remember that we "borrowed" 1 from the 256's column, so we must subtract an additional 1. So the subtraction for the 256's column becomes 22 - 1A - 1 = 7, which is the value we leave in the 256's column.
Thus our final answer is 07E2. If we conver this back to decimal, we get the value 2018, which coincides with the math we originally did in decimal.
As we did with addition, we'll use a small table to help us conver the above process to 8051 assembly language:
Since we're subtracting 16-bit values, each value requires two 8-bit registers. Essentially, the value to be subtracted from will be held in R6 and R7 (the high byte in R6 and the low byte in R7) while the value to be subtracted will be held in R4 and R5 (the high byte in R4 and the low byte in R5). We will leave our answer in R2, and R3.
Let's review the steps involved in subtracting the values above:
We'll now convert the above process to assembly language, step by step.
Combining the code from the two steps above, we come up with the following subroutine:
文章评论(0条评论)
登录后参与讨论