; pbdiv.psm
; original by Peter Smith
; imported to pBlazIDE and corrected

; divider function, 8 by 8
; data independent

divisor EQU s1
dividend EQU s2
rem EQU s3
temp EQU s4
count EQU s5

; assumes divisor is in divisor
; dividend in dividend
; returns quotient in dividend (original value overwritten)
; returns remainder in rem
;
div8: LOAD count, 8 ; loop count
LOAD rem, 0
SL0 dividend ; init for test
SLA rem ; get bit from dividend thru carry

divlp0: SUB rem, divisor ; do subtraction
JUMP C, cantsub ; <0, add back
LOAD temp, 1 ; otherwise, we'll push a 1 into the result
JUMP lptest ; go to loop test
cantsub: ADD rem, divisor ; add it back
LOAD temp, 0 ; and set a zero for result bit
lptest: SR0 temp
SLA dividend ; put new bit in result
SLA rem ; get bit from dividend thru carry
SUB count, 1 ; 8 done?
JUMP NZ, divlp0

RET ; yes