In: Computer Science
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function.
The main function should:
• Pass the starting address of the array in $a0.
• Pass the indices of the two elements to swap in $a1 and $a2.
• Preserve (i.e. push onto the stack) any T registers that it uses.
• Call the Swap function.
The Swap function should:
• Swap the specified elements in the array.
• Preserve any S registers it uses.
• It does not return a value to main.
.text
.globl main
main: la $t0, intA # load the starting address of the integer array into $t0
# Specify your name and the date in the comments
above.
# Insert the code for the main function
here.
nop
# put breakpoint at this line to end program
without warning/error.
swap:
# insert the code for the Swap function
here.
.data 0x10010000
intA: .word # Specify an
array of integers here.
Greetings!!
Code:
.data
intA: .word 0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19
.text
#MAIN STARTS HERE
main:
la $a0,intA #load address of the array
li $a1,4 #index of 1st element
li $a2,7 #index of 2nd element
#FUNCTION CALL
jal swap
#FUNCTION RETURN TO THIS POINT
#STANDARD TERMINATION
li $v0,10 #parameter
syscall #system call for terminating the execution
#MAIN ENDS HERE
#FUNCTION DEFINITION
swap:
mul $t0,$a1,4 #calculate the offset of 1st element
add $t1,$a0,$t0 #add with the base of array so that the address of 1st element is in t1
lw $t2,0($t1) #load 1st number to t2
mul $t0,$a2,4 #calculate the offset of 2nd element
add $t3,$a0,$t0 #add with the base of array so that the address of 2nd element is in t3
lw $t4,0($t3) #load 2nd number to t4
sw $t4,0($t1) #store 2nd number to the address of 1st number
sw $t2,0($t3) #store 1st number to the address of 2nd number
jr $ra #return to main
#FUNCTION ENDS HERE
Output screenshot:
Hope this helps
Thank You