Assembly Code How can we use multiplication in assembly with
Assembly Code:
How can we use multiplication in assembly without using mul or imul instruction? For example how can we multiply a number by \"2017\" without using mul/imul instructions?
Solution
; INPUT CX,BX
; OUTPUT DX:AX - 32 bits
; CLOBBERS BX,CX,DI
xor ax,ax ; zero a reg
mov dx,ax ; 1 clock faster than xor
mov di,cx
or di,bx ; test for zero on both regs
jz @done
mov di,ax ; DI used for reg,reg adc
@loop:
shr cx,1 ; divide by two, bottom bit moved to carry flag
jnc @skipAddToResult
add ax,bx
adc dx,di ; reg,reg is faster than reg,imm16
@skipAddToResult:
add bx,bx ; faster than shift or mul
or cx,cx ; fast zero check
jnz @loop
@done:
ret
