Re: A86: ASM Programming Test - div 3


[Prev][Next][Index][Thread]

Re: A86: ASM Programming Test - div 3




Awsome!  That routine is definitely going in my toolbox of useful routines.
You forgot the <sub c> with the <inc d>, but I figured that out from the
comments.  Do you think you could explain how it works?

;=========================================
; Divide
;  by Joshua Grams <j3grams@earthling.net>
; input:  d = dividend, c = divisor
; output: d = quotient, a = remainder
; total: 15b/355t to 363t (exluding RET)
;=========================================
Divide:
 push bc          ; save bc
 sub a            ; clear a
 ld b,8           ; 8 shifts
DivideL:
 sla d            ; shift left d
 rla              ; into a
 cp c             ; is a >= c
 jr c,DivideS     ; skip if not
 sub c            ; a = a - c
 inc d            ; set bit in d
DivideS:
 djnz DivideL     ; loop
 pop bc           ; restore bc
 ret              ; done

-----Original Message-----
From: Josh Grams <j3grams@earthling.net>
To: assembly-86@lists.ticalc.org <assembly-86@lists.ticalc.org>
Date: Thursday, December 10, 1998 3:22 PM
Subject: Re: A86: ASM Programming Test - div 3


>Shift routines work just fine. If I remember correctly, that's basically
>the way hardware dividers (integer) work. This routine divides a by c,
>returns quotient in d, remainder in a. Actually, it might not work, I just
>kind of dashed it off, let me know if it doesn't. . .
>
>; divisor in c
>push bc
>ld d,a ; put dividend in d
>sub a ; clear a
>ld b,8 ; 8 shifts
>l0: sla d ; shift left d
>rla ; into a
>cp c ;if a >= c, a = a-c, set bit in d
>jr c,l1
>inc d
>l1: djnz l0 ; loop
>pop bc
>ret
>
>--Joshua
>


Follow-Ups: