Question

In: Computer Science

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 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.

Solutions

Expert Solution

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


Related Solutions

Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers...
Write a MIPS Assembly Language program to perform the following operations: Request and read two integers from the console (Dn and Up), Call a recursive function to compute the result int RecurseFunc( int Dn, int Up ) { if( Dn < 1 ) return 0; return Dn * Up + RecurseFunc( Dn - 1, Up + 1 ); } Print out the results
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer"...
need a code MIPS assembly language program to implement algorithms of an 8-bit integer "positive integer" to calculate the square root for an-8 bit integer using The Radix-2 SRT-Redundant and Non-Redundant Algorithm to approximate square root
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT