Use MARS to write and simulate a 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. Download the template file “P4_template.asm” provided on Blackboard. Add your code to this file. Do not modify any of the code provided in the file. 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. You may use any instruction from the MIPS Core Instruction Set (see MIPS Reference Card). You may use the load address (la) pseudo-instruction.
Here is the p4 file .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.
Below is required code. Please note that print_array function is to just check the output of code on console, you may not add it to your assignment. Let me know if you have any problem or doubt. Thank you.
======================================================================
code asked in question
======================================================================
.text
.globl main
main:
la $a0, intA # load the starting
address of the integer array into $a0
# specify location of element to swap
addi $a1, $zero, 2
addi $a2, $zero, 6
# call swap function
jal swap
# exit program
addi $v0, $zero, 10
syscall
swap:
# save registers $t1, $t2, $s1, $s2
addi $sp, $sp, -16
sw $t1, 0($sp)
sw $t2, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
# words are stored in 4 bytes so mutiply $a1 and
$a2 by 4
# to get byte offset from starting adress of
array
# store result in $s1 and $s2
mul $s1, $a1, 4
mul $s2, $a2, 4
# store number at index $a1 in $t1
# calculate offset of address in $s1
add $s1, $a0, $s1
lw $t1, 0($s1)
# store number at index $a2 in $t2
# calculate offset of address in $s2
add $s2, $a0, $s2
lw $t2, 0($s2)
# swap the numbers store in $t1 and $t2
# store $t2 at address of $s1
sw $t2, 0($s1)
# store $t1 at address
of $s2
sw $t1, 0($s2)
# load register back
lw $t1, 0($sp)
lw $t2, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
# return to main
jr $ra
.data 0x10010000
# Specify an array of integers here.
intA: .word 1 2 3 4 5 6 7 8 9 10
======================================================================
BELOW CODE IS TO PRINT OUTPUT FOR TESTING SWAP FUNCTION
======================================================================
.text
.globl main
main:
la $a0, intA # load the starting
address of the integer array into $a0
# print original array
jal print_array
# specify location of element to swap
la $a0, intA
addi $a1, $zero, 2
addi $a2, $zero, 6
# call swap function
jal swap
# print updated array
jal print_array
# exit program
addi $v0, $zero, 10
syscall
swap:
# save registers $t1, $t2, $s1, $s2
addi $sp, $sp, -16
sw $t1, 0($sp)
sw $t2, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
# words are stored in 4 bytes so mutiply $a1 and
$a2 by 4
# to get byte offset from starting adress of
array
# store result in $s1 and $s2
mul $s1, $a1, 4
mul $s2, $a2, 4
# store number at index $a1 in $t1
# calculate offset of address in $s1
add $s1, $a0, $s1
lw $t1, 0($s1)
# store number at index $a2 in $t2
# calculate offset of address in $s2
add $s2, $a0, $s2
lw $t2, 0($s2)
# swap the numbers store in $t1 and $t2
# store $t2 at address of $s1
sw $t2, 0($s1)
# store $t1 at address
of $s2
sw $t1, 0($s2)
# load register back
lw $t1, 0($sp)
lw $t2, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
# return to main
jr $ra
.data 0x10010000
# Specify an array of integers here.
intA: .word 1 2 3 4 5 6 7 8 9 10
###################################################################
## below code is for just console output you may not require it
in
## yout assignment
.data
space: .asciiz " "
endl: .asciiz "\n"
.text
print_array:
add $s0, $a0, $zero
# print first 10 elements of array
addi $t0, $zero, 9
addi $t1, $zero, 0
loop: # multiply by 4 to get byte offset for
words
mul $t2, $t1, 4
add $t2, $s0, $t2
lw $a0, 0($t2)
# print number
addi $v0, $zero, 1
syscall
# print space
addi $v0, $zero, 4
la $a0, space
syscall
# increase loop counter
addi $t1, $t1, 1
# check loop condition and loop again
ble $t1, $t0, loop
# print newline
addi $v0, $zero, 4
la $a0, endl
syscall
# return to main
jr $ra
======================================================================
Get Answers For Free
Most questions answered within 1 hours.