| 32-bit multiply / divide helpers GCC calls on the 68000 (it has only 16-bit mulu/divu). | Ubuntu's m68k libgcc is built for the 68020: its __modsi3 calls __divsi3 with bsr.l, which a | 68000 reads as a short branch to an odd address. So these are ours (after GCC's lb1sf68.S | 68000 paths), libgcc isn't linked, and a helper we lack fails the link instead of the console. | C calling convention: arguments on the stack, result in d0, d0-d1/a0-a1 free. .globl __mulsi3, __udivsi3, __divsi3, __umodsi3, __modsi3 __mulsi3: | (a, b) -> a * b, low 32 bits move.w 4(sp),d0 | a.hi * b.lo mulu.w 10(sp),d0 move.w 6(sp),d1 | + a.lo * b.hi mulu.w 8(sp),d1 add.w d1,d0 swap d0 clr.w d0 move.w 6(sp),d1 | + a.lo * b.lo mulu.w 10(sp),d1 add.l d1,d0 rts __udivsi3: | (a, b) -> a / b, unsigned move.l d2,-(sp) move.l 12(sp),d1 | divisor move.l 8(sp),d0 | dividend cmpi.l #0x10000,d1 bcc 3f move.l d0,d2 | 16-bit divisor: two divu steps clr.w d2 swap d2 divu.w d1,d2 | high quotient : remainder move.w d2,d0 swap d0 move.w 10(sp),d2 | remainder : low dividend divu.w d1,d2 move.w d2,d0 bra 6f 3: move.l d1,d2 | wide divisor: shift both until it fits 16 bits 4: lsr.l #1,d1 lsr.l #1,d0 cmpi.l #0x10000,d1 bcc 4b divu.w d1,d0 | tentative quotient (at most one too large) andi.l #0xFFFF,d0 move.l d2,d1 | quotient * divisor, up to 33 bits mulu.w d0,d1 swap d2 mulu.w d0,d2 swap d2 tst.w d2 bne 5f add.l d2,d1 bcs 5f cmp.l 8(sp),d1 bls 6f 5: subq.l #1,d0 6: move.l (sp)+,d2 rts __divsi3: | (a, b) -> a / b, signed (rounds toward zero) move.l d2,-(sp) moveq #1,d2 | the result's sign move.l 12(sp),d1 bpl 1f neg.l d1 neg.b d2 1: move.l 8(sp),d0 bpl 2f neg.l d0 neg.b d2 2: move.l d1,-(sp) move.l d0,-(sp) bsr __udivsi3 addq.l #8,sp tst.b d2 bpl 3f neg.l d0 3: move.l (sp)+,d2 rts __umodsi3: | (a, b) -> a % b, unsigned move.l 8(sp),-(sp) move.l 8(sp),-(sp) bsr __udivsi3 addq.l #8,sp bra 1f __modsi3: | (a, b) -> a % b, signed (the dividend's sign) move.l 8(sp),-(sp) move.l 8(sp),-(sp) bsr __divsi3 addq.l #8,sp 1: move.l 8(sp),-(sp) | a - (a / b) * b move.l d0,-(sp) bsr __mulsi3 addq.l #8,sp move.l 4(sp),d1 sub.l d0,d1 move.l d1,d0 rts