home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
utils
/
squsq
/
fcrnch11.lbr
/
IDIV.MZC
/
IDIV.MAC
Wrap
Text File
|
1986-12-24
|
2KB
|
76 lines
extrn .c2bc
;
; unsigned integer divide
; (de) := (dehl) DIV (bc)
; (hl) := (dehl) MOD (bc)
; carry for overflow (or zero divide)
; when registers unchanged
; divisor, remainder and quoutient range 0 to 65535
; dividend range 0 to 4295*10^6 (approx)
; f, d, e, h, l
.idiv:: ora a; clear any cy
push psw; save (a)
mov a,e; check for overflow
sub c
mov a,d
sbb b
jnc errexit; overflow or divide by zero
xchg
push b
call dvstep; div into hi 3 bytes of dividend
mov e,a
call dvstepm; next 8 bits of quoutient
mov e,a
pop b
pop psw
ret
;
; error exit from various routines
errexit:
pop psw
stc
ret
;
; unsigned divide, 24 bit by 16 bit
; no overflow checks.
; (a) := (hld) / (bc)
; (hl) := (hld) MOD (bc)
; (d) := (e on entry)
; a,f,b,c,d,e,h,l
dvstep: call .c2bc; (bc) := -(bc)
; " "
; same, divides by -(bc)
; a,f,d,e,h,l
dvstepm:
mov a,d; low order dividend byte
mov d,e; save lo dividend or hi quoutient byte
mvi e,9; loop counter
dvstep2:
dcr e
rz; done
dad h; l shift dividend
jc dvstep5; 1 bit out, generate 1 bit
add a; l shift lo dividend, quoutient
jnc dvstep3
inx h; propagate into (hl)
dvstep3:
push h; save for restoration
dad b; trial
jc dvstep4; succeeds, generate 1 bit
pop h; fails, restore, generate 0 bit
jmp dvstep2
dvstep4:
inx sp; clean stack
inx sp; of saved partial dividend
inr a; generate 1 bit
jmp dvstep2
dvstep5:
adc a; finish dividend shift, 1 bit generate
jnc dvstep6
inx h; propagate cy
dvstep6:
dad b; subtraction
jmp dvstep2
end
4