TITLE "SMTH12 - Syslib 4.3a" NAME ('DIVHD') ;================================================================= ; The Libraries, Version 4, (C) 1989 by Alpha Systems Corp. ;----------------------------------------------------------------- ; Author : Joe Wright ; Date : 9 Dec 89 ; Version : 1.3 ; Module : SMTH12 ; Abstract: This module contains the routine DIVHD which divides ; the 16-bit unsigned value in the HL register by the 16-bit ; value in DE with the results being returned in HL. ; Revision: ; 1.3 - 26 Oct 90 - Added new MODHD and SDIV entries and sped up ; the division. JWW ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ; Module Entry Points PUBLIC DIVHD, MODHD, SDIV .Z80 CSEG ;=============================================================== ; NAME - DIVHD ; Entry: HL the dividend, DE the divisor (16-bit unsigned numbers) ; Exit : HL := HL div DE (the Quotient) ; Uses : HL ; Special Requirements: None ; Note ** Division by Zero returns FFFFh ** ;=============================================================== DIVHD: PUSH DE ; Save DE CALL SDIV ; DE := HL div DE EX DE,HL ; Quotient to HL POP DE ; Restore DE RET ;=============================================================== ; NAME - MODHD ; Entry: HL the dividend, DE the divisor (16-bit unsigned numbers) ; Exit: HL := HL mod DE (the Remainder) ; Uses: HL ; Special Requirements: None ;=============================================================== MODHD: PUSH DE ; Save DE CALL SDIV ; HL := HL mod DE POP DE ; Restore DE RET ;=============================================================== ; NAME - SDIV General Purpose Sixteen Bit Division. ; Entry: HL the dividend, DE the divisor (16-bit unsigned numbers) ; Exit: DE := HL div DE Division by 0 yields 0FFFFh (-1) ; HL := HL mod DE Remainder of the division ; Uses: HL and DE ; Special Requirements: None ;=============================================================== SDIV: PUSH BC PUSH AF ; Save user's registers LD B,D LD C,E ; Divisor to BC EX DE,HL ; Dividend to DE LD HL,0 ; Clear HL LD A,16+1 ; Seventeen times through the loop OR A ; Clear Carry ; Main loop. DIV: ADC HL,HL ; Shift ACC left plus Carry SBC HL,BC ; Subtract the divisor JR NC,DIV0 ; It worked.. ADD HL,BC ; Restore HL, Carry again DIV0: CCF ; Complement Carry flag EX DE,HL ; Get the dividend ADC HL,HL ; Shift Cy to L0 and H7 to Cy EX DE,HL ; Put it back DEC A ; Finished? JR NZ,DIV ; ..loop if not and do again ; End of loop. Quit. POP AF POP BC ; Restore registers RET ; Return to caller END