RosettaCodeData/Task/Function-definition/Z80-Assembly/function-definition.z80

28 lines
612 B
Z80 Assembly

doMultiply:
;returns HL = HL times A. No overflow protection.
push bc
push de
rrca ;test if A is odd or even by dividing A by 2.
jr c, isOdd
;is even
ld b,a
loop_multiplyByEvenNumber:
add hl,hl ;double A until B runs out.
djnz loop_multiplyByEvenNumber
pop de
pop bc
ret
isOdd:
push hl
pop de ;de contains original HL. We'll need it later.
ld b,a
loop_multiplyByOddNumber:
add hl,hl
djnz loop_multiplyByOddNumber
add hl,de ;now add in original HL for the leftover add.
pop de
pop bc
ret