; ------------------------------------------------- ; Class-developed program for "fast" multiplication ; (meaning, in this case, that we swap arguments, ; although the main loop is O(smaller argument)) ; ; Special thanks to Ben Gardiner for transcription! ; ------------------------------------------------- READ R1,DD ; read arguments into R1, R2 READ R2,DD DATA #aftswap ; set up for the swapping jump COPY DR,J0 COPY R2,R4 ; duplicate R2 into R4 (saving R2) SUB R1,R4 ; compare R1 and R4==R2 JPIF R4,GZ,J0 ; R4 > 0 implies R1 > R2, so skip swap COPY R1,R2 ; else R1 < R2, so swap R1 and R2 ADD R4,R1 ; tricky restoration of R2 into R1! #aftswap DATA #loop ; set up for main loop COPY DR,J0 ; re-use J0 for main loop #loop ADD R2,R3 ; main loop: multiply by repeated addition INC R1,-1 ; R1 counts down to terminate loop at 0 JPIF R1,GZ,J0 ; return to top if R1 still positive WRITE R3,DD ; else drop through to print result HALT ; ------------------------------------------------- ; End program ; -------------------------------------------------