Question

In: Computer Science

Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS)...

Coding Problem 1: In this program, you are asked to write a program in assembly (MIPS) which works as a simple calculator. The program will get two integer numbers, and based on the requested operation, the result should be shown to the user. a. The program should print a meaningful phrase for each input, and the result. i. “Enter the first number” ii. “Enter the second number” iii. “Enter the operation type” iv. “The result is” b. The user should enter 0, 1, and 2 to tell the program the types of operation add, sub, and multiply, respectively. c. How many registers do you need to implement this program? d. What system calls do you need to write this program?

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .

Also attached image for syscall we need and register we used inside program.

Program Plan :

  1. prompt user for first number ,get user input stored in $v0 and move into $t0 (first number)
  2. prompt user for second number ,get user input stored in $v0 and move into $t1 (second number)
  3. prompt for operation type (0=add,1=sub,2=multiply)
  4. operaton type stored in $v0 now check
  5. using branch operaton if $v0==0 go to addition label
  6. using branch operaton if $v0==1 go to subtract label
  7. using branch operaton if $v0==2 go to multiply label
  8. else jump to exit label
  9. inside addition label store addition in $t2 =$t0+$t1
  10. inside subtract label store subtract in $t2 =$t0-$t1
  11. inside multiply label store multiply in $t2 =$t0*$t1
  12. after them jump to print label
  13. inside print label print result string and result of operation by moving $t2 to $a0
  14. now exit or terminate program .

Program :

.data
   firstNumber : .asciiz "Enter the first number: "
   secondNumber: .asciiz "Enter the second number: "
   operationType: .asciiz "Enter the operation type(0=add,1=sub,2=multiply ): "
   result : .asciiz "the result is :"
  
  
.text
   main:
  
       la $a0,firstNumber #$a0=address of first number
       li $v0,4 #syscall 4 to print string
       syscall
      
       #read user input integer using syscall 5
       li $v0,5 #user input stored in $v0
       syscall
      
       addi $t0,$v0,0 #move first number in $t0=$v0+0
      
       la $a0,secondNumber #$a0=address of second number
       li $v0,4 #syscall 4 to print string
       syscall
      
       #read user input integer using syscall 5
       li $v0,5 #user input stored in $v0
       syscall
      
       addi $t1,$v0,0 #move second number in $t1=$v0+0
      
      
       la $a0,operationType #$a0=address of operation type
       li $v0,4 #syscall 4
       syscall
      
       #read user input in $v0 using syscall 5
       #0 -> add
       #1 -> subtract
       #2 -> multiply
       li $v0,5 #syscall 5
       syscall
      
      
       beq $v0,0,addition #if operation type $v0==0 go to addition label
       beq $v0,1,subtract #if operation type $v0==1 go to subtract label
       beq $v0,2,multiply #if operation type $v0==2 go to multiply label
       j exit #if invalid value jump to exit label(operation type not 0,1 and 2)

addition: #addition label
   add $t2,$t0,$t1 #t2=$t0+$t1 ,result=firstnumber + secondnumber
   j print #jump to print label
  
subtract: #subtract label
   sub $t2,$t0,$t1 #t2=$t0-$t1 ,result=firstnumber - secondnumber
   j print #jump to print label

multiply: #multiply label
   mul $t2,$t0,$t1 #t2=$t0*$t1 ,result=firstnumber * secondnumber
   j print #jump to print label
  
print: #print label
   la $a0,result #$a0=address of result
   li $v0,4 #syscall 4
   syscall
  
   addi $a0,$t2,0 #move result in $a0 =$t2
   li $v0,1 #syscall 1
   syscall
  
   exit:#exit label
    li $v0,10 #syscall 10 to terminate program
    syscall
      

Output :

Register and syscall used in program :

Please up vote ,comment if any query .


Related Solutions

Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle....
Assembly Language Coding Using MARS (MIPS) 1)Write a program that prints your name in a Triangle. (NAME = John Doe) 2)Write a Program that intializes X to 10, Y to 20 and Z to -50, adds X and Y and Z and prints the following the value of each variable, for example value of x is 10 as well as the result of the addition.
I have to do the following MIPS coding assignment. Question 1 Write a MIPS program that...
I have to do the following MIPS coding assignment. Question 1 Write a MIPS program that meets the following requirements (NOTE: your program statements should follow the order of the requirements and each requirement should correspond to only one assembly instruction): Loads the hexadecimal equivalent of 23710 into register 13 using an immediate bitwise OR instruction Loads the hexadecimal equivalent of 183410 into register 17 using an immediate bitwise OR instruction Performs the bitwise AND operation on the operands stored...
Write a MIPS assembly program that prompts the user first for a string, then for a...
Write a MIPS assembly program that prompts the user first for a string, then for a character. The program should then search the string for the character and print out the number of times the character appears in the string. You can use as many restrictions as you want, just be sure to list them. You must allocate space in the .data segment of your program for the user string.
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user toinput an...
MIPS Assembly LanguageWrite a MIPS assembly language program that asks the user to input an integer and then prints out a string that shows how that integer should be encoded using 16 bits. Your program should handle both positive and negative valued inputs. Your program should also print out an error message if the given input cannot be expressed as a 16 bit signed integer.As an example, if the input is 12, your program should output “0000000000001100”. If the input...
Write a program in MIPS assembly language to perform the calculation of the following equation and...
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:    f = 5x + 3y + z Assumptions: - Registers can be used to represent variables x, y, z, and f - Initialize x, y, and z to values of your choice. f can be initialized to zero. - Use comments to specify your register usage and explain your logic
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write a mips assembly language program that asks the user to enter an unsigned number and...
Write a mips assembly language program that asks the user to enter an unsigned number and read it. Then swap the bits at odd positions with those at even positions and display the resulting number. For example, if the user enters the number 9, which has binary representation of 1001, then bit 0 is swapped with bit 1, and bit 2 is swapped with bit 3, resulting in the binary number 0110. Thus, the program should display 6.
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results).
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results). (Make sure you use comments next to each line to describe what actions you are taking in your code. )...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT