Question

**URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input...

**URGENT** Please help with this MIPS program?

(calculator program that uses values from a user's input file and solves them; needs order of operations)

Write a MIPS Assembly Language program to read hexadecimal values and operations (one per line) from a file. The file name should be requested and read in from the console. Once the ascii number has been read in, convert it to internal, binary, storage (decimal). The input format may be either fixed-format, 32-bit, twos compliment values, or variable length signed magnitude (your choice). After conversion, the value should be printed to the console.

The operations supported are + (addition), - (subtraction), * (multiplication), / (division), % (modulus), = (assignment, equals), s (memory save), r (memory recall), and z (memory clear, zero) where there is one and only one memory location. The memory location should be initialized to zero. The value to be displayed is the last value read or the last value computed depending on whether the previous line was a value or operation, respectively.

Operands, operations and their results shall be output to the console. Memory operations take effect immediately based on the most recent entry or calculation. Unless parentheses are supported, operations occur when a second operation or the end-of-file is encountered.

This program needs to simulate the operation of the Microsoft calculator in programmer mode with hex input and dword (32-bit) element size. This is to include error handling.

All data and operations must be explicitly supplied. No automatic generation of either.

The Sample Output is only a demonstration of output. The program must, at minimum, provide output of intermediate and final values as would the Microsoft Calculator.

Support parentheses using the stack. Order of precedence must also be supported.

Input file:

0000000A
*
0000000A
*
00000016
/
00000007
=
s
00000014
*
00000014
*
00000016
/
00000007
-
r
=
Sample Output:

Input: 10
Input: *
Input: 10
Result: 10 * 10 = 100
Input: *
Input: 22
Result: 100 * 22 = 2200
Input: /
Input: 7
Input: =
Result: 2200 / 7 = 314
Input: s
Save: 314
Input: 20
Input: *
Input: 20
Result: 20 * 20 = 400
Input: *
Input: 22
Result: 400 * 22 = 8800
Input: /
Input: 7
Result: 8800 / 7 = 1257
Input: -
Input: r
Read: 314
Input: =
Result: 1257 - 314 = 943
**Thank you in advance!

(use MARS)

I need help with the code for this calculator program

**I need help getting the sample output from the input
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I'm trying to finish up this MIPS calculator, super basic, my first mips program. It doesn't have to handle overflow or anything like that, just has to work on small, positive numbers.

I've not checked my algorithms for multiply and divide, because I am just trying to get add working.

I cannot for the life of me figure out why the ints will not read in and also I'm getting a memory out of bounds when I call lb $a0, op to display the operator for output and don't understand why.

I'm new to this so anything is probably helpful. Thanks in advance.

        .data
                        # const string for welcome
welc:       .asciiz "Welcome to SPIM Calculator 1.0!\n" 
p_int:      .asciiz "\nPlease give an integer: "
p_op:       .asciiz "\nPlease give an operator: "
i_err:      .asciiz "\nInput Incorrect, bad operator!\n"
again_str:  .asciiz "Another calculation? (y/n)"
rmndr:      .asciiz " r: "
new_line:   .asciiz "\n"

int1:   .word 1         # space to hold int 1
int2:   .word 1         # space to hold int 2
raw_in: .space 1        # space to hold raw input
op:     .space 1        # space to hold operator char
a_char: .space 1        # space to hold again char

out:    .word 1         # space to hold output
remain: .word 1         # space to hold remainder

#operator constants
c_plus: .ascii "+"      # const for +
c_min:  .asciiz "-"     # const for -
c_mult: .asciiz "*"     # const for *
c_divi: .asciiz "/"     # const for /
c_eq:   .asciiz "="     # const for =
c_no:   .asciiz "n"     # const for n

        .text
        .globl main
main:   li $v0, 4               # syscall 4, print string
        la $a0, welc            # give argument: string
        syscall                 # actually print string

calc:   la $t6, remain          # load remainder variable
        move $t6, $zero         # store 0 in remainder (reset)

        li $v0, 4               # syscall 4, print string
        la $a0, p_int           # give argument: string
        syscall                 # actually print string

        li $v0, 5               # tell syscall we want to read int 1
        syscall                 # actually read in int 1
        la $s1, int1            # load int1 into $s1
        move $s1, $v0           # copy the integer from $v0 to int1

        li $v0, 4               # syscall 4, print string
        la $a0, p_int           # give argument: string
        syscall                 # actually print string

        li $v0, 5               # tell syscall we want to read int 2
        syscall                 # actually read in int 2
        la $s2, int2            # give $s2 the address to hold int 2
        sw $v0, 0($s1)          # copy the integer from $v0 to $s2

        li $v0, 4               # syscall 4, print string
        la $a0, p_op            # give argument: string
        syscall                 # actually print string

        li $v0, 8               # tell syscall we want to read operator
        la $a0, op              # give $a0 the address to hold the operator
        syscall                 # actually read in operator

        lb $t0, op              # load the first byte of op
        li $t1, '+'             # load const for plus
        li $t2, '-'             # load const for minus
        li $t3, '*'             # load const for multiplying
        li $t4, '/'             # load const for dividing

        la $s0, out             # load out to $s0

        beq $t0, $t1, plus      # we're adding
        beq $t0, $t2, minus     # we're subtracting
        beq $t0, $t3, multi     # we're multiplying
        beq $t0, $t4, divi      # we're dividing
        # else
        j error                 # incorrect input

plus:   add $s0, $s1, $s2       # add our ints, store in $t0
        j print

minus:  sub $s0, $s1, $s2       # subtract our ints, store in $t0
        j print

multi:  slt $t1, $t2, $s2       # if our counter is less than int2, set $t1 to 1
        beq $t1, $zero, print   # if we've reached int2, we're done
        add $s0, $s1, $s1       # add int1 and int1, store in out
        j multi                 # loop

divi:   la $t0 remain           # load remainder into $t0
        move $t0, $s1           # set remainder to dividend
        add $s0, $zero, $zero   # set out to 0, just in case
loop:   slt $t1, $t0, $s2       # if remainder is less than divisor, set 1
        beq $t1, $zero, print   # if we're done branch to done
        sub $t0, $t0, $s2       # sub divisor from remainder, store in remainder
        addi $s0, $s0, 1        # increment quotient by 1
        j loop                  # loop

print:  li $v0, 1               # tell syscall we want to print int
        la $a0, int1            # give syscall int1 to print
        syscall                 # actually print int
        li $v0, 4               # tell syscall we want to print string
        lb $a0, op              # tell syscall we want to print operator
        syscall                 # actually print string
        li $v0, 1               # tell syscall we want to print int
        la $a0, int2            # give syscall int2 to print
        syscall                 # actually print int
        li $v0, 4               # tell syscall we want to print string
        la $a0, c_eq            # tell syscall we want to print operator
        syscall                 # actually print string
        li $v0, 1               # tell syscall we want to print integer
        la $a0, out             # give syscall our output
        syscall                 # actually print int
        la $t0, remain          # load remainder
        beq $t0, $zero, again   # if we have no remainder, finish printing
        li $v0, 4               # tell syscall we want to print string
        la $a0, rmndr           # tell syscall we want to print remainder string
        syscall                 # print "r: "
        li $v0, 1               # tell syscall we want to print int
        la $a0, remain          # give syscall our remainder to print
        syscall                 # print remainder

again:  li $v0, 4               # tell syscall we want to print string
        la $a0, new_line        # tell syscall to print new line
        syscall
        la $a0, again_str       # load prompt for again string for syscall
        syscall
        li $v0, 8               # tell syscall we want to read string
        la $a0, a_char          # tell syscall to put it in $a0
        syscall
        lb $t0, a_char
        li $t1, 'n'             # get n char so we can compare
        beq $t0, $t1, exit      # if we are done, exit
        #else loop
        j calc                  # jump to beginning



error:  li $v0, 4               # tell syscall we want to print string
        la $a0, i_err           # give syscall what to print
        syscall                 # actually print
        j again                 # go to prompt for retry

exit:   li $v0, 10              # exit code
        syscall                 #exit!

Please give positive rating. Thank you.

Add a comment
Know the answer?
Add Answer to:
**URGENT** Please help with this MIPS program? (calculator program that uses values from a user's input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instr

    Please write a MIPS program to print the first thirty numbers in the Fibonacci sequence in which each number is the sum of the two preceding ones, starting from 0 and 1. Note that each number in the Fibonacci sequence should be calculated using MIPS instructions. After that please run your MIPS program using SPIM software to display the result in the output (console) window. Save your execution result in a log file of SPIM.What to submit:  1. Your MIPS...

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

  • using mips for assembly language WPte a program that asks the user for 2 numbers. the program should then add the 2 numbers bit by bit, using boolean operators no arithmetic operations ex: add, ad...

    using mips for assembly language WPte a program that asks the user for 2 numbers. the program should then add the 2 numbers bit by bit, using boolean operators no arithmetic operations ex: add, addi, sub, mult, div or arrays are permitted in this project. forming a new variable (in a register) with the solution. That solution is to be output bit by bit using the function that was written in class do not use syscall with a value 10...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • 1. Write and debug a MIPS program that performs the following operations . Prompt for and...

    1. Write and debug a MIPS program that performs the following operations . Prompt for and input three integers "a", "b and "c" using syscalls (3 points) Print all numbers starting from "a" upto "b" (including, but not exceeding "b") at an increment equal to value of"c"(5 points) If ba, then no numbers are printed, and a message is printed to inform it to the user. (3 points) Print the number and average of the generated numbers printed. (3 points)...

  • Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so...

    Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so the computer tells the user to enter ints only if the user enters in floating point numbers or other characters? Write a method called evenNumbers that accepts a Scanner reading input from a file containing a series of integers, and report various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT