In: Computer Science
------------------------------------------------------------------------------------ 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?
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