Question

In: Computer Science

1) Download the file Assign2.asm. Read it and make sure it can be compiled and executed...

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

Solutions

Expert Solution

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 :

  • Answers to the first two questions are given directly in Code A) code.
  • Answer to Q.2 is about taking input and MIPS code for the sorting algorithm. It is given in ASCEND and the incrementers i and j in the ELSE part. of Code A). I have also printed the array in sorted form.
  • The answer to Q.3 is Code B and the output.

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.


Related Solutions

Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
Please answer the following questions and show all work and make sure I can read your...
Please answer the following questions and show all work and make sure I can read your writing please. 1) Ethan went on a 10 day fishing trip. The number of small mouth bass caught and released by Ethan each day was as follows. Day 1 2 3 4 5 6 7 8 9 10 # of Fish 9 24 8 9 5 8 9 10 8 10 A. Find the mean, median, and mode B. Find the range, variance, and...
1. Read the article to make sure you understand what it is talking about.   2. Write...
1. Read the article to make sure you understand what it is talking about.   2. Write a paragraph (200 words) summarizing the article. Be sure you have factual information included in your summary. Make sure you state where you found your article (which newspaper / online source), the author and the date of the article.   3. Write a paragraph (300 words) with your opinion on the article.   What did you think about the article? If the article made you feel...
File permissions let us control who can: a. change how files are executed only b. create...
File permissions let us control who can: a. change how files are executed only b. create files only c. delete files only d. read, write, and execute a file
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for...
The Programming Language is C++ PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!! Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the...
Java Programming For this assignment, you should modify only the User.java file. Make sure that the InsufficientFundsException.java file is in the same folder as your User.java File. To test this program you will need to create your own "main" function which can import the User, create an instance and call its methods. One of the methods will throw an exception in a particular circumstance. This is a good reference for how throwing exceptions works in java. I would encourage you...
You have three questions to answer for this assignment. Read over the questions and make sure...
You have three questions to answer for this assignment. Read over the questions and make sure to answer all parts. Each answer needs to be a minimum of 250 words. Use supply and demand analysis to explain why the quantity of word processing software exchanged increases from one year to the next. Some people will pay a higher price for brand name goods. For example, some people buy Rolls Royces and Rolex watches to impress others. Does knowingly paying higher...
Important: If you're going to write make sure your writing is neat and easy to read....
Important: If you're going to write make sure your writing is neat and easy to read. Please use print (not cursive). Thank you Each student accepted into UVU is assigned a number between 0 to 1. Students assigned a number bellow 0.6 are given the Charles C. Rich scholarship. What are two ways to identify the effect of the Charles C. Rich Scholarship? What do you need to assume for each of these two strategies? Why would the assumption be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT