In: Computer Science
1) Download the file Assign2.asm. Read it and make sure it can be compiled and executed in Mars.
Assign2.asm
.data
Array: .word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27
string1: .asciiz "Please input the flag: \n"
string2: .asciiz "Sorted Result: \n"
# Tranfer the C code of selection sort to MIPS code. Please do not modify the existing code.
.text
main:
# write your code here to ask the user to input a flag (int number)
la $t0, Array
li $t1, 0
li $t7,11 # array length n=11
mul $t7, $t7, 4 # 4*n, end of inner loop
subi $t8, $t7, 4 # 4*(n-1), end of outer loop
OuterLoop:
add $t2, $t1, 4 # i is in $t1 and j is in $t2
# write your code here for Selection Sort with flag checking
# write your code here to print the sorted result
# exit
addi $v0, $zero, 10
syscall
2) Write code to finish all the tasks listed in Assign2.asm. In Assign2.asm, an array ‘a’of ‘n’=11integers are given at the beginning (make sure do not change these integers):
43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27
The finished Assign2.asm should be filled with your MIPS code in the specified space to implement the given C code for Selection Sort:
for (int i=0; i<n-1;i++) {
for(int j=j+1; j<n;j++) {
if a[i] > a[j] {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
This above code will generate the sorted result from minimum to maximum.
3) Your code will ask the user to input a flag (int number). If the flag is 1, your code outputs the sorting result from minimum to maximum; otherwise, your code outputs the sorting result from maximum to a minimum.
Here is an example output:
--------------------------------------------
Please input the flag: # user inputs 1
Sorted Result:
-27
-7
-5
11
12
13
14
43
64
70
71
---------------------------------------------
Please input the flag: # user inputs 0
Sorted Result:
71
70
64
43
14
13
12
11
-5
-7
Note: MIIPS code
CODE A)
.data
    Array: .word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27
    string1: .asciiz "Please input the flag: \n"
    string2: .asciiz "Sorted Result: \n"
.text
main:
    # priniting string1
    li $v0, 4
    la $a0, string1
    syscall
      
    # takiing flag input
    li $v0, 5
    syscall
    # moving input in temporary register t3 
    move $t3,$v0
        
    bne $t3, 1, EX # if user input is not 1 then Exit
    
    la $t0, Array
    li $t1, 0
    li $t7,11 # array length n=11
    mul $t7, $t7, 4 # 4*n, end of inner loop
    subi $t8, $t7, 4 # 4*(n-1), end of outer loop
 
    
OuterLoop:
    addi $t2, $t1, 4 # i is in $t1 and j is in $t2
    # Min to Max
        
ASCEND:
        lw $a0,Array($t1)       # value of a[i]
    lw $a1,Array($t2)    # value of a[j]
    ble $a0,$a1, EL    # if(a[i]>a[j])
    move $t4, $a0        # temp(t4) = a[i]
    sw $a1,Array($t1)        # a[i] = a[j]
        sw $t4,Array($t2)        # a[j] = temp(t4)
        
EL  :
    addi $t2,$t2, 4        # increment j (j++)
    bne $t2,44, ASCEND    # Branch if(j<n)
    move $a1,$zero
    addi $t1,$t1, 4        # increment i (i++)
    bne $t1,40, OuterLoop    # Branch if(i<n-1)
    move $a0,$zero
    # printing string2
    li $v0, 4
    la $a0, string2
    syscall
        li $t5 , 0        # counter for Printing(k=0) 
PRINT:
    lw  $t4, Array($t5)       # Loading a[k] in temporary register t4
    
    # Printing a[k]
    li $v0, 1               
    move  $a0 ,$t4
    syscall 
    # Printing a newline 
    li $v0, 11
    la $a0, '\n'
    syscall
    
    addi $t5,$t5, 4          # Incrementing k (k++) 
    bne $t5,$t7, PRINT    # Branch if(k<n)
    move $a0,$zero
EX :     
    addi $v0, $zero, 10    # exit
    syscall
ScreenShots :
Coding Terminal :



Output :


CODE B)
.data
    Array: .word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27
    string1: .asciiz "Please input the flag: \n"
    string2: .asciiz "Sorted Result: \n"
.text
main:
    # priniting string1
    li $v0, 4
    la $a0, string1
    syscall
    
    # takiing flag input
    li $v0, 5
    syscall
    # moving input in temporary register t3 
    move $t3,$v0
    la $t0, Array
    li $t1, 0
    li $t7,11 # array length n=11
    mul $t7, $t7, 4 # 4*n, end of inner loop
    subi $t8, $t7, 4 # 4*(n-1), end of outer loop
 
    
OuterLoop:
    addi $t2, $t1, 4 # i is in $t1 and j is in $t2
        
    # Min to Max
        
ASCEND:
    bne $t3, 1, DESCEND # if user input is not 1 then Exit
    move $a0,$zero
    lw $a0,Array($t1)       # value of a[i]
    lw $a1,Array($t2)    # value of a[j]
    ble $a0,$a1, EL    # if(a[i]>a[j])
    move $t4, $a0        # temp(t4) = a[i]
    sw $a1,Array($t1)        # a[i] = a[j]
        sw $t4,Array($t2)        # a[j] = temp(t4)
        
    # Max to Min
    
DESCEND:
    beq  $t3, 1, EL  # if user input is 1 then Else part  otherwise execute DESCEND
    lw $a0,Array($t1)       # value of a[i]
    lw $a1,Array($t2)    # value of a[j]
    bge $a0,$a1, EL    # if(a[i]<a[j])
    move $t4, $a0        # temp(t4) = a[i]
    sw $a1,Array($t1)        # a[i] = a[j]
        sw $t4,Array($t2)        # a[j] = temp(t4)
    
EL  :
    addi $t2,$t2, 4        # increment j (j++)
    bne $t2,44, ASCEND    # Branch if(j<n)
    move $a1,$zero
    addi $t1,$t1, 4        # increment i (i++)
    bne $t1,40, OuterLoop    # Branch if(i<n-1)
    move $a0,$zero
       
    # printing string2
    li $v0, 4
    la $a0, string2
    syscall
        
    li $t5 , 0        # counter for Printing(k=0) 
PRINT:
    lw  $t4, Array($t5)       # Loading a[k] in temporary register t4
    
    # Printing a[k]
    li $v0, 1               
    move  $a0 ,$t4
    syscall 
    
    # Printing a newline 
    li $v0, 11
    la $a0, '\n'
    syscall
    
    addi $t5,$t5, 4          # Incrementing k (k++) 
    bne $t5,$t7, PRINT    # Branch if(k<n)
    move $a0,$zero
EX :     
    addi $v0, $zero, 10    # exit
    syscall
ScreenShots :
Coding Terminal :




Output :


Explanation :
Note:
My coding terminal looks like this. I am using MARS 4.5 (please don't change the default setting of the assembler !!!).
My JDK is SE JDK 15.