# MIPS implementation of reverse.c # Set aside memory for the prompts and the # array in the data segment. .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 4096 # int list[1024] # Our code goes in the text segment .text # i is associated with $s0 # n with $s1 # address of list will be in $s7 main: # Load array address into $s7. "la" is a pseudo-instruction # that loads the address associated with a label into # a register. la $s7, list # $s7 = address associated with list # Prompt for number of elements, using syscall 4: # print_string. la $a0, prompt1 # $a0 = address associated with prompt1 li $v0, 4 # $v0 = 4 syscall # Read number of elements from console, store in # $s0, using syscall 5: read_int. li $v0, 5 syscall move $s1, $v0 # $s1 = $v0 # Prompt user to enter the elements la $a0, prompt2 li $v0, 4 syscall # for (i = 0; i < n; i++) li $s0, 0 # i = 0 for1: # branch on i >= n bge $s0, $s1, for1_exit # bge is a # pseudo-instruction # scanf ("%d", &list[i]) # # First, 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 will be stored in $v0 sw $v0, 0($t0) # list[i] = $v0 addi $s0, $s0, 1 # i++ # end of for1 loop body, go back to the termination # test j for1 for1_exit: # Now, iterate over array from n-1 downto 0, # printing contents as we go # for (i = n - 1; i >= 0; i++) # i = n - 1 addi $s0, $s1, -1 for2: # 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: jr $ra