Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an MIPS assembly language code to swap the two adjacent elements of an arr

ID: 3688931 • Letter: W

Question

Write an MIPS assembly language code to swap the two adjacent elements of an array of 12 numbers.
The elements of the array will be given by the user input.
The swap operation is fulfiled in a procedure. The input arguments are passed to the procedure using $a0-$a3 and the result is returned by $v0-$v1 register.

The new array is also displayed at the I/O console.
The swap operation of the array elements MUST be fulfilled in a procedure.

HINT:
Dont forget define .data and .text segments.
Allocate a space for your user defined array.
You can pass the parameter values to an procedure by $a0-$a3 and return values $v0-$v1
Define the base address of the array.
Handle with the stack related issues.
Calculate the larger index value for your computations will work properly.
Don't forger the exit system call.

Explanation / Answer

Let z be the position of element that has to be swapped and array p of numbers[]

.data # This is a section for the memory used variable here

z :       . word     8

p:        .word     51 # p[0]    /* elements being stored in array of 12 elements */

            .word    55 # p[1]

            .word    61 # p[2] :        .

            .word    66 # p[3]

......

            .word    88 # p[11]

.text #These are the swapping instructions to swap two adjacent elements

swapping:

la      $s1,z      #This is used to put address of z in $s1

lw     $s1,0($s1) #This is used to put contents of z in $s1

add   $t0,$s1,$s1`#we will start with $t0=z+z

add $t0,$t0,$t0 # now $t0=4z=2z+2z

la $s0,p   #This will put the address of p in $s0

add $t0,$s0,$t0 #This will be done now $t0=p+4z

lw   $t1,0($t0)   # this is to load the contents of p[z]

lw    $t2,4($t0)   #This is to load the contents of p[z+1] by going 4 bytes beyond p[z]

sw $t2,0($t0) #This is used to store the contents of $t2 in p[z]

sw $t1,4($t0) #This is used to store the contents of $t1 in p[z+1]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote