wasson65 16 Posted April 1, 2014 Share Posted April 1, 2014 I do all my programming in assembly and needed a little multiply routine, and came up with this. Not sure if it's been shown before. The method dates back to the ancient Chinese and Egyptians. Beats adding in a loop. No overflow checking, so if your numbers are too big it's going to wrap around. You'll need to declare op1, op2, and answer. I used R7, R8, and R9 mov #0100h, op1 mov #00F0h, op2 Multiply mov.w #0, answer ; clear our answer register nextBit clrc ; clear carry since it will come into MSB on the rrc rrc op1 ; get LSB of op1 into carry jnc dbl ; no bit, we dont add op2 into answer add op2,answer ; the bit was set, add into the answer dbl add op2,op2 ; double our op2 in prep for the next bit cmp #0,op1 ; see if we have any more bits! jz done ; nope, exit jmp nextBit ; yes, still have bits in op1, go again done ; op1, op2 have been destroyed, answer has multiplied value Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.