.DISPLAY " DIVISION " .EJECT ; generate a new page in the output listing. ;+--------------------------------------------------------------------------+ ;| DIVISION A / B | ;| | ;| WRITTEN BY : Jean-Francois GATTO | ;| DATE : 16/09/91 | ;| REVISION : V01.00 | ;| | ;| SOFTWARE DESCRIPTION : This routine divides two 8 bit numbers, A and | ;| B, the result is saved into an 8 bit register. | ;| A and B >= 0. | ;| | ;| INPUT PARAMETERS : NUMBER_A register contains the number A. | ;| NUMBER_B register contains the number B. | ;| | ;| INTERNAL PARAMETERS : SAVE_A register saves the accumulator. | ;| COUNTER register contains the shift counter. | ;| | ;| OUTPUT PARAMETERS : RESULT register contains the result. | ;| REMAIND register contains the remainder. | ;| | ;| WORST CASE SPEED : 410 cycles | ;| WORST EXECUTION TIME : Tcyc x 410 = 666 us at 8 Mhz | ;| BYTE : 52 bytes | ;| | ;| EXAMPLE : ;***** program ***** | ;| .ORG 880h | ;| LDI NUMBER_A,E7h | ;| LDI NUMBER_B,10h | ;| CALL DIVISION | ;| - do... | ;| - do... | ;| .END | ;| ;***** subroutine ***** | ;| .INPUT " DIVIS.ASM " | ;| | ;+--------------------------------------------------------------------------+ DIVISION ; Initialize registers and save the contexte. LD SAVE_A,A ; save the accumulator. CLR RESULT ; clear the result register. CLR REMAIND ; clear the remainder register. ; Test A and B in order to enable the division subroutine. LD A,NUMBER_B ; B = 0 then the result = 0 ( not valid ). JRNZ DIV0 END_DIV LD A,SAVE_A ; restore the accumulator. RET DIV0 LD A,NUMBER_A ; A = 0 then the result = 0. JRZ END_DIV ; Division subroutine. ADDI A,0FFh ; clear the carry. LDI COUNTER,08h ; load the shift counter. LD A,NUMBER_A ; \ RLC A ; | rotate left and save in result register. LD RESULT,A ; / DIV1 LD A,REMAIND ; \ RLC A ; | rotate left the remainder register in LD REMAIND,A ; / order to include the carry. SUB A,NUMBER_B JRC DIV2 ; test if the subtraction is valid. LD REMAIND,A DIV2 LD A,RESULT ; \ RLC A ; | rotate left the result register. LD RESULT,A ; / DEC COUNTER JRZ DIV3 JP DIV1 DIV3 COM A ; complement the result. LD RESULT,A JP END_DIV