Question

Use MARS to write and simulate a MIPS assembly language program to swap two of the...

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. 

Homework Answers

Answer #1

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

======================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use MIPS assembly language program to swap two of the integers in an integer array. The...
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...
Assembly Programming in MIPS Write a program in MIPS assembler that performs the following computations: Prompts...
Assembly Programming in MIPS Write a program in MIPS assembler that performs the following computations: Prompts the user to enter two integers and stores the smaller one in the first data memory word (with the lowest address) and the larger one in the second data memory word. Then divides the larger word by the smaller one in integer and stores the integer quotient in the third data memory word and the remainder in the fourth data memory word. Finally, it...
Write a simple ARM assembly language program that finds the mean (as an integer) of all...
Write a simple ARM assembly language program that finds the mean (as an integer) of all values of an integer array. When your program starts, you should assume that r0 contains the address of the array and r1 contains the number of integers in the array. When you program finishes, the mean should be stored in r2. You may use other registers as stack registers.
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction...
1a. Using the MIPS program below (with bugs intact), determine the instruction format for each instruction and write the decimal values of each instruction field. addi $v0, $zero, 0 loop: lw $v1, 0($a0) sw $v1, 0($a1) sll $a0, $a0, 2 add $a1, $a1, $a0 beq $v1, $zero, loop 1b. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds...
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of...
Write and test code in MIPS assembly language program to implement algorithms "The Non-Restoring Algorithm "of an 8-bit integer the user shoud insert the number then by the algo well find the square root
This is a homework assignment for Computer Architecture and some question use MIPS Assembly language. 1.    ...
This is a homework assignment for Computer Architecture and some question use MIPS Assembly language. 1.     In the following MIPS assembly code, translate all the instructions to their corresponding machine code in hexadecimal format. This code is stored in the memory from address 0x1fff0000. Loop: sw $t1, 4($s0)        addi $t1, $t1, -1    sll $t1, $t1, 2        bne $t1, $s5, Exit    addi $s0, $s0, 4          j Loop Exit: … 2.     Find the MIPS...
Write a program in Mars MIPS Assembly Language that asks the user for an 8-digit hexadecimal...
Write a program in Mars MIPS Assembly Language that asks the user for an 8-digit hexadecimal and then prints out its 32-bit binary representation, the operation of the function, the format (I, R, or J), the fields associated with the format (op, rs, rt, imm, shamt, funct), and the instruction associated with the hexadecimal. All five parts must be written as functions.
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
PLEASE COMMENT CODE AND TEST THAT IT WORKS: Write a assembly program to find the largest...
PLEASE COMMENT CODE AND TEST THAT IT WORKS: Write a assembly program to find the largest item in an array and store it in a AX. Hint: Use both Jump and loop instruction to write the program. logic: Assume that the first item of the array is the minimum and store it in AX Write a loop. Inside the loop, compare the each array item with the AX If the array item is less than the AX, update AX with...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT