# A solution to: read and multiply problem

      .text

      .globl      main

main:

      move $s1, $ra           # Keep the ra safe (stack is another good place)

      li    $v0,4             # output a prompt

      la    $a0, msg1

      syscall

      li    $v0,5             # input  a number

      syscall                 # User input is now in $vo

      move $t0, $v0           # Put the input into a $t0

      li    $v0,4             # output prompt again

      la    $a0, msg1

      syscall

      li    $v0,5             # input a second number

      syscall                 # User input is now in $vo

      move $a1, $v0           # Put the second input in $a1

      move $a0, $t0           # Retrieve the value saved in t0

      jal multiply            # Note that a0 and a1 contain the args

                              # so call to multiply is prepared

      move $s0, $v0           # Need to put the product someplace safe

                              # (A spot in memory is a good alternative)

      li    $v0,4             # output a label for results

      la    $a0, msg2

      syscall

      li    $v0,1             # output the answer

      move  $a0, $s0          # Saved answer is an argument to syscall

      syscall

      move $ra, $s1           # $ra was munged by the call to multiply

      j $ra                   # return

      .data

msg1: .asciiz     "\nAn integer?  "

msg2: .asciiz "\nThe product is: "

      .text

multiply:                     #multiply $a0 by $a1, Bug: a0 can't be < 0

      add $t0, $zero, $zero   # zero a counter

      add $t1, $zero, $zero   # zero an accumulator

loop:

      beq $t0, $a0, exit      # Are we done?

      addi $t0, $t0, 1        # If not,  Increment counter

      add $t1, $a1, $t1       #  and add in a1

      j loop                  # Back to top of loop

exit:

      move $v0, $t1           # Put answer in the return value register

      j $ra                   # return