# # bad_mult.asm # Jeff Ondich, 1 April 2014 # # MIPS has multiplication instructions built in, so there's no # need to implement multiplication ourselves. That said, learning # how subroutines are structured is worth writing something simple, # and a bad implementation of multiplication fits the bill. # # Pay particular attention to the roles of the a-registers, # v-registers, and t-registers, as well as the instructions # jal and jr. # # Also: # -- What happens if factor1 is negative? # -- What happens if factor2 is negative? # -- What happens if factor1 * factor2 >= 2^32 ? # .data f1: .word 5 f2: .word 6 .text # The main program main: # Load factors into registers a0 and a1 la $a0, f1 lw $a0, 0($a0) la $a1, f2 lw $a1, 0($a1) # Invoke the multiplication subroutine jal mult # Move the product into a0 and print add $a0, $zero, $v0 li $v0, 1 syscall # Exit li $v0, 10 syscall # The multiplication subroutine mult: li $t0, 0 mult $a0, $a1 mfhi $t6 mflo $t7 loop: beqz $a0, end add $t0, $t0, $a1 addi $a0, $a0, -1 b loop end: add $v0, $zero, $t0 jr $ra