[A83] Re: Divide by 12


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

[A83] Re: Divide by 12



> My attempt to code this in assembly didn't work out, so
> if anyone wants to use this, you'll have to write your
> own code, sorry.
> 
Is it really so hard?

; A contains the dividend
 ld ix, 0        ; q = 0
 ld hl, (A) ; r = A + 1
 inc hl
Loop:
 ld a, h
 or a
 jr nz, G4
 ld a, l
 cp 4
 jr c, LoopEnd
G4:
 ld d, h ; y = r div 4
 ld e, l
 srl d
 rr e
 srl d
 rr e
 add ix, de ; q = q + y
 ld h, 0 ; x = r mod 4
 ld a, l
 and 3
 ld l, a
 add hl, de ; r = x + y
 jr Loop
LoopEnd:
 ; IX = quotient
 ; HL = remainder

Okay, this is absolutely unoptmisied. There's no need to use a 16-bit
register for storing a 2-bit modulus, and it could be unrolled, too.

PG

-- 
http://www.fastmail.fm - Choose from over 50 domains or use your own



Follow-Ups: References: