Rewrite this java program into MIPS assembly language. This program takes the ar
ID: 3725245 • Letter: R
Question
Rewrite this java program into MIPS assembly language. This program takes the array and displays it in descending order using bubblesort.
public class Bubblesort {
public static void bubbleSort( int [ ] num ){
int j;
boolean flag = true; // set flag to true to begin first pass
int temp; //holding variable
while ( flag ){
flag= false; //set flag to false awaiting a possible swap
for( j=0; j < num.length -1; j++ ){
if ( num[ j ] < num[j+1] ) // change to > for ascending sort
{
temp = num[ j ]; //swap elements
num[ j ] = num[ j+1 ];
num[ j+1 ] = temp;
flag = true; //shows a swap occurred
}
}
}
}
public static void main(String [] args) {
int intArray[] = { 10, 3, 5, 2, 11, 232, 2, 0};
bubbleSort(intArray);
for(int i: intArray){
System.out.print(i + " ");
}
System.out.println(" ");
}
}
Explanation / Answer
.data .align 4 banner: .asciiz "++++++++++++++++++++++++++++++++++ " banner1: .asciiz "| MIPS BUBBLE SORT V0.1 | " banner2: .asciiz "++++++++++++++++++++++++++++++++++ " promptIntList: .asciiz "[Integer] (0=exit) >>> " crlf: .asciiz " " space: .asciiz " " sortedLabel: .asciiz "Sorted array: " busyLabel: .asciiz "Swapping ... " .text .globl main main: add $fp, $zero, $sp; #set frame pointer move $s1, $ra # store return address li $v0, 4; # opcode for print_string la $a0, banner; # arg for print_string syscall; # syscall la $a0, banner1; # next arg syscall; # syscall la $a0, banner2; # next arg syscall; # syscall li $s0,0; # number of integers initialize PROMPTINT: li $v0, 4; # opcode for print_string la $a0, promptIntList; # argument for print_string syscall; # call print_string li $v0, 5; # opcode for read_int syscall; # call read_int beq $v0,0,bubblesort; addi $sp,$sp,-4; # decrement stack -4 sw $v0,0($sp); # store user input onto the stack addi $s0, $s0, 4; # store stack counter in s0 j PROMPTINT; # jump back to PROMPTINT; bubblesort: beq $s0,0,EXIT; # check if number of ints*4 is > 0 move $t0,$sp; # outerloop counter add $t4,$sp,$s0; # outerloop termination addi $t4,$t4,-4; bubbleLoop1: move $t1,$sp; # innerloop counter sub $t6,$t0,$sp; # inner loop end condition sub $t6,$t4,$t6; bubbleLoop2: lw $t2,0($t1); # get first int lw $t3,4($t1); # get second int slt $t5,$t2,$t3; # if n[0]Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.