.text ! ! routine to compute (a*base + b) mod c (d) ! (a*base + b) / c (e) ! base=2^30. ! Note: 0 <= a < c < base; 0 <= b < base. ! ! Assemble with: as -o longdiv.o -P longdiv.s ! or some such; note the -P flag. ! ! C call sequence: ! longdiv(a,b,c,d,e) int a,b,c,*d,*e; ! ! a, b and c are assumed to be in %o0, %o1 and %o2 resp. ! the addresses of d and e are assumed to be in %o3 and %o4. ! this routine uses %o0,%o1,%o2,%o3,%o4 and %o5 ! ! The macros below define the two kinds of addition steps performed. ! #define SS0(label) \ addx %o0,%o0,%o0; /* shift high order, insert bit */\ subcc %o0,%o2,%o5; /* subtract divisor */\ bcc label; /* if succeeds continue with o5 */\ addxcc %o1,%o1,%o1 /* and shift lower, quotient bit */ #define SS1(label) \ addx %o5,%o5,%o5; /* shift high order, insert bit */\ subcc %o5,%o2,%o0; /* subtract divisor */\ bcc label; /* if succeeds continue with o0 */\ addxcc %o1,%o1,%o1 /* and shift lower, quotient bit */ shift = 2 magic = 3 .globl longdiv longdiv: sll %o1,shift,%o1 ! position low order addcc %o1,%o1,%o1 ! start things happening add %o1,magic,%o1 ! magic constant Lc00: SS0(Ld01) Lc01: SS0(Ld02) Lc02: SS0(Ld03) Lc03: SS0(Ld04) Lc04: SS0(Ld05) Lc05: SS0(Ld06) Lc06: SS0(Ld07) Lc07: SS0(Ld08) Lc08: SS0(Ld09) Lc09: SS0(Ld10) Lc10: SS0(Ld11) Lc11: SS0(Ld12) Lc12: SS0(Ld13) Lc13: SS0(Ld14) Lc14: SS0(Ld15) Lc15: SS0(Ld16) Lc16: SS0(Ld17) Lc17: SS0(Ld18) Lc18: SS0(Ld19) Lc19: SS0(Ld20) Lc20: SS0(Ld21) Lc21: SS0(Ld22) Lc22: SS0(Ld23) Lc23: SS0(Ld24) Lc24: SS0(Ld25) Lc25: SS0(Ld26) Lc26: SS0(Ld27) Lc27: SS0(Ld28) Lc28: SS0(Ld29) Lc29: SS0(Ld30) Lc30: xor %o1,-1,%o1 ! one's complement quotient st %o1,[%o4] retl st %o0,[%o3] Ld01: SS1(Lc02) Ld02: SS1(Lc03) Ld03: SS1(Lc04) Ld04: SS1(Lc05) Ld05: SS1(Lc06) Ld06: SS1(Lc07) Ld07: SS1(Lc08) Ld08: SS1(Lc09) Ld09: SS1(Lc10) Ld10: SS1(Lc11) Ld11: SS1(Lc12) Ld12: SS1(Lc13) Ld13: SS1(Lc14) Ld14: SS1(Lc15) Ld15: SS1(Lc16) Ld16: SS1(Lc17) Ld17: SS1(Lc18) Ld18: SS1(Lc19) Ld19: SS1(Lc20) Ld20: SS1(Lc21) Ld21: SS1(Lc22) Ld22: SS1(Lc23) Ld23: SS1(Lc24) Ld24: SS1(Lc25) Ld25: SS1(Lc26) Ld26: SS1(Lc27) Ld27: SS1(Lc28) Ld28: SS1(Lc29) Ld29: SS1(Lc30) Ld30: xor %o1,-1,%o1 ! one's complement quotient st %o1,[%o4] retl st %o5,[%o3]