原创 [转]用8051做16位运算-03

2008-8-21 10:00 3645 6 6 分类: MCU/ 嵌入式

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:


e344a37b-b385-4773-854a-2a3177467d8b.jpg


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:


b869ce67-cb21-4914-9aac-aab52cc18cd1.jpg


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:



  1. Subtract the low bytes R5 from R7, leave the answer in R3.
  2. Subtract the high byte R4 from R6, less any borrow, and leave the answer in R2.

We'll now convert the above process to assembly language, step by step.


点击开大图


Programming Tip: The SUBB instruction always subtracts the second value in the instruction from the first, less any carry. While there are two versions of the ADD instruction (ADD and ADDC), one of which ignores the carry bit, there is no such distinction with the SUBB instruction. This means before you perform the first subtraction, you must always be sure to clear the carry bit. Otherwise, if the carry bit happens to be set you'll end up subtracting it from your first column value -- which would be incorrect.

Combining the code from the two steps above, we come up with the following subroutine:


点击开大图

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条