Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a MIPS assembly code to perform the four basic computations found on most

ID: 3624684 • Letter: W

Question

write a MIPS assembly code to perform the four basic computations found on most calculators: addition, subtraction, multiplication and division. The code will allow the user to input any two integers and one of the four operations, then compute and print the result. More specifically,
? The code prompts for and reads the first integer, prompts for and reads the operation, prompts for and reads the second integer, then computes the requested operation and prints the result.
? The operation may be specified via an integer value (for example, 1 = addition, 2 = subtraction, 3 =multiplication and 4 = division) or via a character (“+” = addition, “-“ = subtraction, “*” =multiplication and “/” = division).
? Multiplication must be done via the mult command and the HIGH register checked for overflow. If overflow occurred, print an error message.
? Division must be done via the div command and the HIGH register checked for a remainder. When printing the result, the remainder if nonzero must be printed as well as the quotient.
? Strings for the prompts and output headers are to be in the .data segment.
? After printing out a result the code should allow the user to input another operation. This continues until the value of -9999 is entered for the first integer. This signals the code to terminate.

Explanation / Answer

.data

    first_string:.ascii"Enter first number:"

    second_string:.ascii"Enter second number:"

    Display:.ascii" 1-Addition 2-Subtraction

                     3-Multiplication 4-Division"

    Output: " Result:"

    Error:"Invalid Opeation"

.text

main:

Loop:

     la $a0,first_string

     syscall

     # read integer into $s0

     li   $v0, 5    # system call code for read integer  

        syscall                   # call operating system

     move $t0, $v0 # value read from keyboard returned in

               #register $v0; transfer to $t0

        cmp $t0,0

        bltz Exit

      la $a0,second_string

     syscall

     # read integer into $s0

     li   $v0, 5    # system call code for read integer  

        syscall              # call operating system

     move $t1, $v0 # value read from keyboard returned

    la $a0,Display_string

     syscall

      # read integer into $s0   

     li   $v0, 5    # system call code for read integer  

        syscall# call operating system

     move $t3, $v0 # value read from keyboard returned

       bne $t3,$1,L1

       add $t2,$t1

     L1:bne $t3,$2,L1

         sub $t2,$t1

         J Outputs

    L2: bne $t3,$2,L1

        Mul $t2,$t1

        J Outputs

    L3:bne $t3,$2,L4

       Div $t2,$t1

       la $a0,Display_string

       syscall

       J Outputs

    L4:la $a0,Error

       syscall

       J Outputs

Outputs:

     la $a0,output

     syscall

     # print out result (stored in $s1)

     li   $v0, 1# system call code for printing integer = 1

     move $a0, $t2# move integer into $a0: $a0 = $t2

     syscall# call operating system to perform print

       J Loop

# exit program

          li   $v0, 10            # system call code for exit = 10

          syscall                 # call operating system