Lab8.s
########################################################### # Program Description # CS315 Lab 8 - Stack # # 1. Use create_array to create a dynamic array of words # (note: create_array calls read_array, main does not # call read_array) # 2. Use print_array to print the array # 3. Use sum_array to calculate the sum of the array # 4. Print the sum # # This is the last one! Thanks all for reading and following me! # ########################################################### # Register Usage # $t0 # $t1 # $t2 # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 ########################################################### .data
########################################################### .text main: jal create_array # jumps to create_array subprogram jal print_array # jumps to print_array subprogram jal sum_array # jumps to sum_array subprogram lw $a0, 0($sp) # load sum to print li $v0, 1 # system call code for print int syscall addiu $sp, $sp, 12 # Unallocate 3 words mainEnd: li $v0, 10 syscall # Halt ###########################################################
########################################################### # Subprogram Description # Ask the user for an array length, dynamically alocate # an array of words with the given length # Call read_array to fill the array
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp+0 Base address (OUT) # $sp+4 Array length (OUT) # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 - Base address # $t1 - Length # $t2 # $t3 # $t4 # $t5 # $t6 # $t7 # $t8 # $t9 ########################################################### .data Array_length: .asciiz "Enter a length for your array: "
########################################################### .text create_array: # Ask the user for an array length # Allocate space for the array # Call read_array to fill the array addiu $sp, $sp, -4 # Allocate 1 word sw $ra, 0($sp) # Store the return address li $v0, 4 # system call code for print string la $a0, Array_length # load address of Array_length into $a0 syscall # print Array_length message li $v0, 5 # system call code for read integer syscall # read in the value of length int $v0 move $t0, $zero # clear $t0 to be added to add $t0, $v0, 0 # move $v0 to $t0 by adding
mul $a0, $v0, 4 # set memory to allocate based on input li $v0, 9 # system call code to allocate memory syscall # allocate memory for the array addiu $sp, $sp, -8 # Allocate 2 words sw $v0, 0($sp) # Store the array address sw $t0, 4($sp) # Store the array length jal read_array # jumps to read_array subprogram lw $t1, 4($sp) # Load the array length lw $t0, 0($sp) # Load the array address addiu $sp, $sp, 8 # Unallocate 2 words lw $ra, 0($sp) # Load the return address addiu $sp, $sp, 4 # Unallocate 1 word addiu $sp, $sp, -8 # Allocate 2 words sw $t0, 0($sp) # Store the array address sw $t1, 4($sp) # Store the array length jr $ra # jumps to previous program ###########################################################
########################################################### # Subprogram Description # Reads words into an array until the array is full
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp+0 Base address (IN) # $sp+4 Array length (IN) # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 - Array Address # $t1 - Array Length # $t2 - Index # $t3 - Offset adress to store values # $t4 - Offset for address # $t5 - Input value # $t6 # $t7 # $t8 # $t9 ########################################################### .data Prompt_value: .asciiz "Please input value: "
########################################################### .text read_array: # Read values into an array until the array is full lw $t0, 0($sp) # Load the array address lw $t1, 4($sp) # Load the array length li $t2, 0 # set starting index to 0 loop: bge $t2, $t1, end_loop # breaks loop when last index is passed mul $t4, $t2, 4 # sets offset to store the value add $t3, $t4, $t0 # sets the address to store the value
li $v0, 4 # system call code for print string la $a0, Prompt_value # load address of Prompt_value into $a0 syscall # print Prompt_value message li $v0, 5 # system call code for read integer syscall # read in the value of value int $v0 move $t5, $v0 # move value $v0 to $t5 sw $t5, ($t3) # store the value in the array add $t2, $t2, 1 # increment loop by 1 b loop # move to the top of the loop end_loop: jr $ra # Jumps to previous program ###########################################################
########################################################### # Subprogram Description # Prints words from an array, separated by spaces
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp+0 Base address (IN) # $sp+4 Array length (IN) # $sp+8 # $sp+12 ########################################################### # Register Usage # $t0 - Array Length # $t1 - Array Address # $t2 - Index # $t3 - Index Address # $t4 - Offset # $t5 - The value to be stored # $t6 - Last index check # $t7 # $t8 # $t9 ########################################################### .data Start_array: .asciiz "Your array: [" Comma: .asciiz ", " End_array: .asciiz "]\n" ########################################################### .text print_array: # Print all values from an array lw $t1, 0($sp) # Load the array address lw $t0, 4($sp) # Load the array length li $v0, 4 # system call code for print string la $a0, Start_array # load address of Start_array into $a0 syscall # print Start_array message
add $t6, $t0, -1 # -1 from for array length for clean print
li $t2, 0 # set starting index to 0 print_loop: bge $t2, $t0, end_print # breaks loop when last index is passed mul $t4, $t2, 4 # sets offset to laod the value add $t3, $t4, $t1 # sets the address to print the value from
li $v0, 1 # system call code for print integer lw $a0, ($t3) # load the value at address ($t3) syscall # print the value at index $t2 beq $t2, $t6, end_print # breaks loop when last index is passed
li $v0, 4 # system call code for print string la $a0, Comma # load address of Comma into $a0 syscall # print Commma message
add $t2, $t2, 1 # increment loop by 1 b print_loop # move to the top of the loop end_print: li $v0, 4 # system call code for print string la $a0, End_array # load address of End_array into $a0 syscall # print End_array message jr $ra # Jumps to previous program ###########################################################
########################################################### # Subprogram Description # Calculates the sum of an array #
########################################################### # Arguments In and Out of subprogram # # $a0 # $a1 # $a2 # $a3 # $v0 # $v1 # $sp+0 Base address (IN) # $sp+4 Array length (IN) # $sp+8 Array sum (OUT) # $sp+12 ########################################################### # Register Usage # $t0 - Array Address # $t1 - Array Length # $t2 - Index # $t3 - Address of value to add # $t4 - Offset to get value address # $t5 - Value loaded # $t6 - Sum # $t7 # $t8 # $t9 ########################################################### .data Array_sum: .asciiz "Sum of your array: " ########################################################### .text sum_array: # Sum of all values from the array lw $t0, 0($sp) # Load the array address lw $t1, 4($sp) # Load the array length li $v0, 4 # system call code for print string la $a0, Array_sum # load address of Start_array into $a0 syscall # print Start_array message
move $t2, $0 # set starting index to 0 move $t6, $0 # set starting sum to 0 sum_loop: bge $t2, $t1, end_sum # breaks loop when last index is passed mul $t4, $t2, 4 # sets offset to load the value add $t3, $t4, $t0 # sets the address to add the value from
lw $t5, ($t3) # load the value at address ($t3)
add $t6, $t6, $t5 # adds loaded int to the sum
add $t2, $t2, 1 # increment loop by 1 b sum_loop # move to the top of the loop end_sum: addiu $sp, $sp, -4 # Allocate 1 word sw $t6, 0($sp) # Store the sum
jr $ra # Jumps to previous program ###########################################################










