# sums.asm MIPS file -- example of function from lecture # # 3 February 1999, Lin Jensen # # Function sum to find the sum of an array, # print out the sum of each of 2 arrays, by calling twice # # Subroutine printsum: prints the calculated value nicely. # # The main program: # printsum (sum (arr1, count1)) # printsum (sum (arr2, count2)) # #========================== TEXT Segment =============================== .globl __start .text #---------------- function sum (*array, count) -------------------------- # * means a pointer is expected (C syntax) # ---- input arguments: # $a0 pointer to an array of words (address) # $a1 number of elements in the array (value) # ---- returns: # $v0 sum of all the words indicated # # ---- temporary registers used: # $t0 point to each array element # $t1 count down # $t2 each element value sum: move $t0, $a0 # use to point to successive array elements move $t1, $a1 # countdown in $t1 = count li $v0, 0 # sum = 0 sumloop: sub $t1, 1 # countdown decremented (-1) bltz $t1, sumend # EXIT loop when countdown becomes negative lw $t2, ($t0) add $v0, $v0,$t2 # sum = sum + array(element) add $t0, 4 # point to next array element (4 bytes = 1 word) j sumloop sumend: #final sum now in $v0 jr $ra # return from function call ($ra ---> PC) # -------- subroutine printsum, prints ("Sum is ", $a0) printsum: move $t0, $a0 # save it for printing ($t0 is working copy of argument) la $a0, str # print "Sum is " li $v0, 4 syscall li $t1, 8 # as hexadecimal, 8 digits la $t3, hexval # point to storage location for hex digits hexloop: sub $t1, 1 # countdown bltz $t1, endhex # while counter >= 0 rol $t0, $t0, 4 # 4 bits from high to low end of word andi $t2, $t0, 0x0f # mask off 4 lower bits ori $t2, $t2, 0x30 # make it ASCII digit ble $t2, '9', numeric #if hex value A..F, then add $t2, 7 # add 7, since there are 7 characters numeric: # between '9' and 'A' sb $t2, ($t3) # store hex digit in hexval add $t3, 1 # point to next string position j hexloop endhex: la $a0, hexline #hex value and end the line li $v0, 4 syscall jr $ra # return from function call ($ra ---> PC) #---------------- main program, execution starts here ------------------ __start: # sum (&arr1, count1) # &arr1 means the address is passed (C syntax) # set up function arguments la $a0, arr1 lw $a1, count1 jal sum #call function sum (PC+4 ---> $ra) # result now in $v0 move $a0, $v0 # ... becomes argument to jal printsum # print ("sum is ", $a0) # -------------------------- Call it again --------- # sum (&arr2, count2) # &arr1 means the address is passed (C syntax) # set up function arguments la $a0, arr2 lw $a1, count2 jal sum #call function sum (PC+4 ---> $ra) # result now in $v0 move $a0, $v0 # ... becomes argument to jal printsum # subroutine to print the result li $v0, 10 syscall # end program #------------------DATA SEGMENT ----------------------- .data str: .asciiz "Sum is " hexline: .ascii " 0x" hexval: .ascii "00000000" # printsum will fill in eoln: .asciiz "\n" arr1: .word 1,2,3 count1: .word 3 arr2: .word 111, 222, 54321, -99, 365, 32767,0,0,87650 count2: .word 9 # # end of program