# void swap( int array[], int from, int to) { # int temp; # temp = array[from]; # array[from] = array[ to]; # array[to] = temp; # } .data array1: .space 20 # 20 bytes which is five ints array2: .space 20 # 20 bytes array3: .word 5, 6, 7, 8, -1 .text main: la $a0, array3 addi $a1, $zero, 1 addi $a2, $zero, 2 jal swap nop j end # Assume that array address is in $a0, from in $a1, and to in $a2 # Since this is a leaf method, we dont worry about the stack. swap: sll $t0, $a1, 2 # from * 4 in t0 sll $t1, $a2, 2 # to * 4 in t1 add $t2, $a0, $t0 # Address of array[from] in $t2 add $t3, $a0, $t1 # Address of array[ to] in $t3 lw $t4, 0($t2) # Value of array[from] into $t4 lw $t5, 0($t3) # Value of array[to] into $t5 sw $t4, 0($t3) # Value from $t4 into array[to] sw $t5, 0($t2) # Value from $t5 into array[from] jr $ra end: nop