# MIPS implementation of reverse.c # Data segment contents .data prompt1: .asciiz "Enter number of elements: " prompt2: .asciiz "Enter elements one per line:\n" newline: .asciiz "\n" # Make sure the rest of the word size variables start on a byte # address that is a multiple of 4 .align 2 list: .space 400 # int list[100] # Our code .text # i is associated with $s0 # n with $s1 # address of list will be in $s7 main: # Load array address into $s7 la $s7, list # Prompt for number of elements # # address of prompt1 in $a0 la $a0, prompt1 # print_string syscall number in $v0 li $v0, 4 # make the syscall syscall # Read number of elements from console, store in $s0 li $v0, 5 syscall move $s1, $v0 # Prompt for the elements la $a0, prompt2 li $v0, 4 syscall # Loop n times, reading one int on each iteration, # and storing it in list[i] # i = 0 li $s0, 0 for1: # "i < n": branch on i >= n bge $s0, $s1, for1_exit # scanf ("%d", &list[i]) # # calculate &list[i] sll $t0, $s0, 2 # $t0 = 4 * i addu $t0, $t0, $s7 # $t0 = &list[i] # # read_int li $v0, 5 # $v0 = read_int syscall # int is in $v0 # # list[i] = $v0 sw $v0, 0($t0) # i++ addi $s0, $s0, 1 # end of for1 loop body j for1 for1_exit: # Now, iterate over array from n-1 downto 0, # printing contents as we go # i = n - 1 addi $s0, $s1, -1 for2: # "i >= 0": branch on i < 0 blt $s0, $zero, for2_exit # printf ("%d\n", list[i]) # Calculate &list[i] sll $t1, $s0, 2 # $t1 = 4*i addu $t1, $t1, $s7 # $t1 = &list[i] # print list[i] lw $a0, 0($t1) # $a0 = list[i] li $v0, 1 # $v0 = print_int syscall # print "\n" la $a0, newline li $v0, 4 syscall # i-- addi $s0, $s0, -1 # done with loop body j for2 for2_exit: j $ra