

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


createArray: addiu $sp, $sp, -80 #14 local variables, 4 word arg. build, #$ra, pad above $ra add $t0, $zero, $zero #init i to 0 addi $t3, $zero, 12 #$t3 holds value to check i against to #determine end of loop addi $t2, $sp, 32 #t2 starts out holding location of a[0] loop: sw $t0, 0($t2) #store i into array addi $t0, $t0, 1 #increment i addi $t2, $t2, 4 #$t2 points to next place in the array bne $t0, $t3, loop #branch if i < 12 sw $ra, 16($sp) #need to call sum sw $a0, 80($sp) #store our argument in slot provided by our caller addi $a0, $sp, 32 #put starting location of a[0] into $a lw $a1, 80($sp) #put arg0 of createArray into $a1 for sum jal sum sw $v0, 28($sp) #store return in b (not really necessary) #return from sum is also our return, leave in $v lw $ra, 16($sp) addiu $sp, $sp, 80 jr $ra
sum: addiu $sp, $sp, -32 #2 local variables, 4 word arg. build, #$ra, pad above $ra add $t0, $zero, $zero #init i to 0 add $t1, $zero, $zero #init total to 0 sw $t1, 28($sp) #store in total's mem. slot sw $ra, 16($sp) loop2: sw $t0, 24($sp) #save i across function call sw $a0, 32($sp) #save arg0 (mem loc. of element of arr[]) in slot #allotted by our caller sw $a1, 36($sp) #save arg1 (n) in slot allotted by our caller lw $a0, 28($sp) #put total into arg lw $a1, 32($sp) #put mem loc of next element of arr into arg lw $a1, 0($a1) #put contents of that mem loc into arg jal addThese sw $v0, 28($sp) #store result into total lw $a0, 32($sp) #restore array pointer addi $a0, $a0, 4 #add 4 to array pointer lw $t0, 24($sp) #restore i addi $t0, $t0, 1 #add 1 to i lw $a1, 36($sp) #restore n addi $t2, $a1, 1 #add 1 to n bne $t0, $t2, loop2 #if i <= n branch to loop lw $v0, 28($sp) #put total into return reg. $v lw $ra, 16($sp) addiu $sp, $sp, 32 jr $ra
addThese: #simple leaf procedure, no stack frame necessary add $v0, $a0, $a jr $ra