Re: LZ: Fractions


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

Re: LZ: Fractions



> The problem with this routine is that it won't work if you want to divide it
> by 3, (or 7, or 9, or whatever). I'd need a generic routine. If there is no
> generic routine, this would be a good idea, but I was hoping for a simple
> routine that would work for most numbers. (Actually, I need to divide
> numbers by 2, 3, 5, and 10, and maybe some others later).


This was posted by  Oskar Liljeblad (ol@kajen.com)  about 6 months ago
(along with 32-bit mult routines):


Div32:                                  ; arg1 / arg2 -> arg1 , res1 =
remainder
        ld      de,0                    ; uses de, b, hl
        ld      (res1),de
        ld      (res1+2),de
        ld      b,32
        and     a                       ; clear carry
Div32Loop:
        ld      de,(arg1)               ; arg1 >> 1 -> arg1
        rl      e
        rl      d
        ld      (arg1),de
        ld      de,(arg1+2)
        rl      e
        rl      d
        ld      (arg1+2),de
        ld      de,(res1)               ; res1 >> 1 -> res1
        rl      e
        rl      d
        ld      (res1),de
        ld      de,(res1+2)
        rl      e
        rl      d
        ld      (res1+2),de
        ld      hl,(res1)               ; res1 - arg2 -> res1
        ld      de,(arg2)
        sbc     hl,de
        ld      (res1),hl
        ld      hl,(res1+2)
        ld      de,(arg2+2)
        sbc     hl,de
        ld      (res1+2),hl
        jr      nc,Div32NoAdd
        ld      hl,(res1)               ; res1 + arg2 -> res1
        ld      de,(arg2)
        add     hl,de
        ld      (res1),hl
        ld      hl,(res1+2)
        ld      de,(arg2+2)
        adc     hl,de
        ld      (res1+2),hl
Div32NoAdd:
        ccf
        djnz    Div32Loop
        ld      de,(arg1)               ; arg1 >> 1 -> arg1
        rl      e
        rl      d
        ld      (arg1),de
        ld      de,(arg1+2)
        rl      e
        rl      d
        ld      (arg1+2),de
        ret


> 
> David Kristensen at the University of Missouri - Kansas City
> dkristensen@cctr.umkc.edu
> 
> The Mind conquers All...


<pre>
-- 
; Max Mansour  // mail : mmansour@gis.net
;             // irc : Justarius
; 
; The Presidents -> http://www.gis.net/~mmansour/
; HP OmniGo 100  -> http://www.gis.net/~mmansour/omnigo/omnigo.html
; ZShell         -> http://www.gis.net/~mmansour/zshell/zshell.html


.end
</pre>


References: