Question

In: Computer Science

------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...

------------------------------------------------------------------------------------
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int result = 0;
        System.out.print("Enter the first number: ");
        int x = input.nextInt();
        System.out.print("Enter the second number: ");
        int y = input.nextInt();
        System.out.println("operation type for + = 0");
        System.out.println("operation type for - = 1");
        System.out.println("operation type for * = 2");
        System.out.print("Enter the operation type: ");
        int z = input.nextInt();
        if(z==0){
           result = x + y;
           System.out.println("The result is " + result);
        }else if(z==1){
            result = x - y;
            System.out.println("The result is " + result);
        }else if(z==2){
            result = x * y;
            System.out.println("The result is " + result);
        }else{
            System.out.println("Wrong input");
        }

    }
}

----------------------------------------------------------------------------------------------------------------------------------

Change Java program to MIPS

How many registers do you need to implement this program?

What system calls do you need to write this program?

Solutions

Expert Solution

Program:

.data
stmt1: .asciiz "Enter the first number: "
stmt2: .asciiz "Enter the second number: "
stmt3: .asciiz "\nEnter the operation type: "
stmt4: .asciiz "operation type for + = 0"
stmt5: .asciiz "\noperation type for - = 1"
stmt6: .asciiz "\noperation type for * = 2"
stmt7: .asciiz "Enter the operation type: "
stmt8: .asciiz "\nThe result is "
stmt9: .asciiz "\nWrong input"
newline: .asciiz "\n"
.text
.globl main
main:
        la $a0, stmt1       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        li $v0,5       #get user input for first number
        syscall
        move $t0,$v0
        
        la $a0, stmt2       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        li $v0,5       #get user input for second number
        syscall
        move $t1,$v0
        
        la $a0, stmt4       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        la $a0, stmt5       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        la $a0, stmt6       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        la $a0, stmt3       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        li $v0,5       #get user input for operation type
        syscall
        move $t2,$v0
        
        beqz $t2,sum
        beq $t2,1,difference
        beq $t2,2,product
        
        la $a0, stmt9       # load address of stmt for syscall
        li $v0, 4           # specify Print String service
        syscall
        
        j end
sum: add $t3,$t0,$t1
     j printResult
difference: sub $t3,$t0,$t1
            j printResult
product:    mul $t3,$t0,$t1
            j printResult  
printResult:    la $a0, stmt8       # load address of stmt for syscall
                li $v0, 4           # specify Print String service
                syscall
                
                move $a0,$t3
                li $v0,1
                syscall    
                j end
end:   li $v0,10
       syscall

Output:

I used 6 registers to implement the program

$a0, $v0 for input and output operations

$t0 for storing first number

$t1 for storing second number

$t2 for storing operation

$t3 for storing result

system calls needed:

4 to print string

5 to read integer

1 to print output


Related Solutions

import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new...
import java.util.Scanner; public class ZombieApocalypse{    public static void main(String[] args){    Scanner input = new Scanner(System.in);           boolean gameOver = false; int colSize = 10; int rowSize= 10; String floorTile= "."; int playerX = 0; int playerY= 0; String playerTile="@"; int exitX= colSize-1; int exitY= rowSize-1; String exitTile="# "; int zombieX=5; int zombieY=5; // Defining Second Zombie int zombie2Y= 8; int zombie2X= 3; // Defining third zombie int zombie3Y= 1; int zombie3X= 7; String zombieTile="*"; String zombie2Tile="*";...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Squaring { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num=0; String s = ""; while (true) { System.out.println("Enter an integer greater than 1: "); try { // reading input s = sc.nextLine(); // converting into int num = Integer.parseInt(s); break; } catch (Exception e) { System.out.println(s + " is not valid input."); } } // Now we have a valid number // putting into square int square = num; int count...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); SimpleDate d1 = new SimpleDate(); SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt()); System.out.println(d1); System.out.println(d2); System.out.println(d1.before(d2)); System.out.println(d2.before(d1)); } } Implement SimpleDate class in chapter6.date package with the following attributes: day, (int type,  private) month, (int type,  private) year (int type,  private) The class should have the following methods: a constructor with three parameters: year, month, and day a constructor with no parameters which initialize the...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT