Re: A83: Re: Signed Division
[Prev][Next][Index][Thread]
Re: A83: Re: Signed Division
These are Andreas Ess's signed and unsigned division routines from an Usgard
demo on ticalc.org. No idea whether they're efficient or not, but they
definitely work.
;-----------------------------------------------------------------------------
;SDIV16: signed division,uses UDIV16
;-----------------------------------------------------------------------------
SDIV16:
push bc
ld a,h
ld (SREM),a
xor d
ld (SQUOT),a
bit 7,d
jr z,CheckDE
or a
sub a
sub e
ld e,a
sbc a,a
sub d
ld d,a
CheckDE:
bit 7,h
jr z,DoDiv
or a
sub a
sub l
ld l,a
sbc a,a
sub h
ld h,a
DoDiv:
call UDIV16
ld a,(SQUOT)
bit 7,a
jr z,DoRem ;!
or a
sub a
sub l
ld l,a
sbc a,a
sub h
ld h,a
DoRem:
pop bc
ret
;-----------------------------------------------------------------------------
;UDIV16: divides HL through DE
; quotient in HL
; remainder in DE
; carry = 0
;-----------------------------------------------------------------------------
UDIV16:
Divide:
ld c,l
ld a,h
ld hl,0 ;hl = remainder
ld b,16 ;16 bits in dividend
or a ;clear carry
DivLoop:
rl c
rla
rl l
rl h
push hl
sbc hl,de
ccf
jr c, Drop
ex (sp),hl
Drop:
inc sp
inc sp
djnz DivLoop
ex de,hl
rl c
ld l,c
rla
ld h,a
or a
ret
SQUOT:
.db 0
SREM:
.db 0
----
Jonah Cohen
<ComAsYuAre@aol.com>
http://linux.hypnotic.org/~jonah/ (down)