; Implements 32-bit interger division by long division ; Matt Sprinkle, 2-14-2000 Divsor: .equ -21 ; Divisor Divend: .equ 143 ; Dividend (divided by divisor) .org 0 ; start program at 0x0000 lar r30, Qout ; load address of Qout loop for branching lar r31, Rem ; load address of Rem loop for branching lar r29, Ncheck ; load address of dividend check lar r28, Main ; load address of main program lar r27, Finish ; load address of finish of program la r1, Divend ; load dividend la r2, Divsor ; load divisor andi r9,r9,#0 ; clear register 9 (negitive counter) brpl r29, r2 ; branch to next check if divisor positive not r2, r2 ; not divisor addi r2,r2,#1 ; convert to 2s complement addi r9,r9,#1 ; increment counter Ncheck: brpl r28, r1 ; branch to main program if dividend is positive not r1, r1 ; not dividend addi r1,r1,#1 ; convert to 2s complement addi r9,r9,#1 ; increment counter Main: andi r5,r5,#0 ; clear register 5 (counter) andi r6,r6,#0 ; clear register 6 (sum) addi r8,r5,#1 ; set r8 to 1 Qout: addi r5,r5,#1 ; add 1 to counter (increment counter) add r6,r6,r2 ; add divisor to sum sub r7,r1,r6 ; subtract sum from dividend (calc) brmi r31,r7 ; branch to Rem loop if calc is negative br r30 ; branch to Qout (repeat loop) Rem: sub r3,r5,r8 ; subtract 1 from counter and store in qoutient sub r6,r6,r2 ; subtract divisor from sum sub r4,r1,r6 ; subtract sum from dividend, store to remainder sub r9,r9,r8 ; reduce negative counter by 1 brzr r27,r9 ; branch to finish if negative count is 0 st r3,Qoutient ; store qoutient to memory st r4,Remainder ; store remainder to memory stop Finish: neg r3,r3 ; neg r3 st r3,Qoutient ; store qoutient to memory st r4,Remainder ; store remainder to memory stop .org 32772 ; jump to 0x8004 Qoutient: .dw 1 ; storage of qoutient Remainder: .dw 1 ; storage of remainder