; TITLE "SMTH11 - Syslib 4.3a" NAME ('MULHD') ;================================================================= ; The Libraries, Version 4, (C) 1989 by Alpha Systems Corp. ;----------------------------------------------------------------- ; Author : Joe Wright ; Date : 9 Dec 89 ; Version : 1.3 ; Module : SMTH11 ; Abstract: This module contains the routine MULHD which mul- ; tiplies two unsigned 16-bit values and returns the ; result. ; Revision: ; 1.4 - 26 Oct 90 - Add SMUL entry. Shortened and sped up ; multiply routine. JWW ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ; Module Entry Points PUBLIC MULHD, SMUL .Z80 CSEG ;=============================================================== ; NAME - MULHD ; Entry: HL,DE - Two 16-bit unsigned values to multiply ; Exit : HL := HL * DE ; F - Carry Set (C) indicates overflow ; Uses : AF, HL ; Special Requirements: None ;=============================================================== MULHD: PUSH BC PUSH DE PUSH AF ; Save user's registers CALL SMUL ; DE,HL := DE * HL LD A,D OR E ; Clear Cy, NZ if Overflow POP DE ; User's AF, D = A LD A,D ; Put it in A POP DE ; User's DE POP BC ; User's BC RET Z ; No Overflow SCF ; Set Carry if overflow RET ;=============================================================== ; NAME - SMUL General Purpose Sixteen Bit Multiplication ; Entry: HL, DE - Two 16-bit unsigned values to multiply ; Exit : DE, HL - The 32-bit Product of the multiplication ; Uses : HL, DE, BC and AF ; Special Requirements: None ;=============================================================== SMUL: LD B,H LD C,L ; Multiplicand to BC LD HL,0 ; Zero an accumulator LD A,16 ; Loop counter ; Main loop. We do this sixteen times. MLHD: ADD HL,HL ; Shift Accumulator left into Cy 11 EX DE,HL ; Multiplier to HL 4 ADC HL,HL ; MSB to Carry 15 EX DE,HL ; Put it back 4 JR NC,MLHD1 ; MSB was Zero, else.. ~10 ADD HL,BC ; Add multiplicand to accumulator ~5 MLHD1: DEC A ; Decrement the count 4 JR NZ,MLHD ; Finished? 12 ; Sub 63 ; 16 * Sub = 1010 RET ; Product in DE, HL END ; of SMTH11.Z80