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

2008-8-21 10:06 3290 8 8 分类: MCU/ 嵌入式

16-Bit Multiplication


16-bit multiplication is the multiplication of two 16-bit value from another. Such a multiplication results in a 32-bit value.



Programming Tip: In fact, any multiplication results in an answer which is the sum of the bits in the two multiplicands. For example, multiplying an 8-bit value by a 16-bit value results in a 24-bit value (8 + 16). A 16-bit value multiplied by another 16-bit value results in a 32-bit value (16 + 16), etc.


For the sake of example, let's multiply 25,136 by 17,198. The answer is 432,288,928. As with both addition and subtraction, let's first convert the expression into hexadecimal: 6230h x 432Eh.


Once again, let's arrange the numbers in columns as we did in primary school to multiply numbers, although now the grid becomes more complicated. The green section represents the original two values. The yellow section represents the intermediate calculations obtained by multipying each byte of the original values. The red section of the grid indicates our final answer, obtained by summing the columns in the yellow area.


8b6766e0-cc6e-4061-aa4a-68c214568cb2.jpg


Remember how we did this in elementary school? First we multiply 2Eh by 30h (byte 1 of both numbers), and place the result directly below. Then we multiply 2Eh by 62h (byte 1 of the bottom number by byte 2 of the upper number). This result is lined up such that the right-most column ends up in byte 2. Next we multiply 43h by 30h (byte 2 of the bottom number by byte 1 of the top number), again lining up the result so that the right-most column ends up in byte 2. Finally, we multiply 43h by 62h (byte 2 of both numbers) and position the answer such that the right-most column ends up in byte 3. Once we've done the above, we add each column, with appropriate carries, to arrive at the final answer.


Our process in assembly language will be identical. Let's use our now-familiar grid to help us get an idea of what we're doing:


c45598ba-08f0-4773-ad8d-456b8d6b9745.jpg


Thus our first number will be contained in R6 and R7 while our second number will be held in R4 and R5. The result of our multiplication will end up in R0, R1, R2 and R3. At 8-bits per register, these four registers give us the 32 bits we need to handle the largest possible multiplication. Our process will be the following:




  1. Multiply R5 by R7, leaving the 16-bit result in R2 and R3.
  2. Multiply R5 by R6, adding the 16-bit result to R1 and R2.
  3. Multiply R4 by R7, adding the 16-bit result to R1 and R2.
  4. Multiply R4 by R6, adding the 16-bit result to R0 and R1.

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:


点击开大图


And to call our routine to multiply the two values we used in the example above, we'd use the code:


7ff7493f-1470-4ee8-8921-b7820a21f100.jpg

PARTNER CONTENT

文章评论0条评论)

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