Jump to content
43oh

16 bit multiply in asm - pretty quick


Recommended Posts

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


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...