# multiply subroutine example for SPIM .data msg: .asciiz "The product is " .text # this is program code .align 2 # instructions must be on word boundaries .globl main # main is a global label main: # multiply 128 * 256 addi $a0,$zero,128 addi $a1,$zero,256 jal multiply move $s0,$v0 li $v0,4 la $a0, msg syscall li $v0,1 move $a0,$s0 syscall li $v0,10 syscall # --------------------------------- # mult subroutine needs some registers # so we save $t0 first multiply: sub $sp,$sp,4 # make room for $t0 sw $t0,0($sp) # put t0 on the stack # start with $t0 = 0 add $t0,$zero,$zero mult_loop: # loop on a1 beq $a1,$zero,mult_eol add $t0,$t0,$a0 sub $a1,$a1,1 j mult_loop mult_eol: # put the result in $v0 add $v0,$t0,$zero # restore $t0 lw $t0,0($sp) add $sp,$sp,4 jr $ra