CSE 230 Spring 2011 (Section 1 and 2)
Quiz 2: March 1st, on class.  75 Mins.

(Books, notes, computer allowed)

 

Question 1 [Leaf Function]  (10 pts)

 

We are writing a MIPS assembly program to compute the sum of all the elements of an integer array.

The integer array is at location numbers, and the size of the array is at location count.

 

The following MIPS code implements this functionality. However, the sum function is missing. You have to write the sum function (starts with sum: and ends with jr $ra).
The complete program that can be run with MARS is here.

 

#data segment

.data      

      numbers:.word 2,1,5,3,10

      count: .word 5   

     

      ans1: .asciiz "sum = "

      endl: .asciiz "\n"

     

#text segment

.text

 

main:

      la $a0, numbers         # load array address

      lw $a1, count           # load number of elements    

      jal sum                 # call sum function

 

      move $a0, $v0           # call print

      jal print

 

      j finish                # finish

 

sum:

     bge $t2, $a1, done      #if t2>=$a1, goto done

     lw  $t0, 0($a0)         #load value at addr a0 to t0

      add $t1, $t1, $t0       # sum = sum + array[i]

      addi $a0, $a0, 4        # add addr by 4 to get the address of   next value in the array

      addi $t2, $t2, 1        # i = i + 1

     j sum

 

done:

      move $v0, $t1           # move result to v0

      jr   $ra                #go to return addr

 

print:

      # code to print ansl, sum and end

      # deleted for brevity

      jr $ra                  # return

 

finish:

      li $v0, 10    # exit

        syscall  


Question 2 [Recursive Function]  (10 pts)

 

We define the function SkipSum for any integer n, recursively as,

 

 

 

Thus,

SkipSum(4) = 4+2+0 = 6, and

SkipSum(5) = 5+3+1+0 = 8, and

SkipSum(-5) = 0.

 

Following is the MIPS program to compute SkipSum for a given number, without the SkipSum function. You have to write a recursive SkipSum function in MIPS assembly.

The complete program that can be run with MARS is here.

 

 

## data segment  

.data

strAskN:    .asciiz "Enter a positive integer n:"

ans1:             .asciiz "SkipSum of "

ans2:       .asciiz " is "

endl:             .asciiz "\n"

 

## text segment

.text

main:

      # Print string to request for n

      li $v0, 4               # print request

      la $a0, strAskN         #

      syscall 

 

      li $v0,5                # read integer n

      syscall

 

      move $s1, $v0           # save n

      move $a0, $s1           # call skipsum

      jal skipsum

     

      move $a0, $s1           # print the results

      move $a1, $v0          

      jal print

 

      j finish          # finish

 

SkipSum:

      addi $sp, $sp, -8       #using stack to store ra and a0(n)

      sw   $ra, 4($sp)        #store ra

      sw   $a0, 0($sp)

      lw   $a0, 0($sp)

      lw   $ra, 4($sp)

      slti $t0, 0, $a0        #if n>0, set t0 to 1

      bne  $t0, 0, recurs     #if t0 !=0, (n>0) goto recurs

      li   $v0, 0             #otherwise, set v0 = 0 (n<=0)

      addi $sp, $sp, 8       

      jr   $ra

recurs:

      addi  $a0, $a0, -2      #calculate n=n-2

      jal SkipSum             #call SkipSum(n-2)

      lw  $a0, 0($sp)         #get n

      lw  $ra, 4($sp)         #get ra

      add $v0, $v0, $a0       #SkipSum(n) = n + SkipSum(n-2)

      add $sp, $sp, 8

      jr  $ra

 

 

print:

      # code to print print ans1, n, ans2, skipsum, endl

      # deleted for brevity

      jr $ra                  # return

 

 

finish:     li    $v0,  10

      syscall