It is often that you do not know in advance how many or what type of arguments are passed in from the command line. In this situation, you must parse the command line.
Your task is to write a small MIPS calculator calc.s that accepts a single arithmetic expression (+,-,*) from the command line, evaluates the expression, and displays the result. Then you will use the MIPS div operator to determine if the result of your operation is even or odd. Start by copying calc.s and gcd.s into your directory.
Currently calc.s prompts the user to input two integers, sums the integers and displays the result like this:
Modify calc.s to accept one subtraction, addition, or multiplication expression from the command line, perform the operation and display the result as in the samples below.
An asterisk must be preceeded with a backslash.
The bash interpreter will see an asterisk as a wildcard, unless escaped.
The \ is an escape character.
Since your code executes from the command line, any functions that you call (e.g., print.s or read.s) would need to be in the calc.s file. Since you do not need sophisticated output, just use syscalls for output. You do not need to include printf procedure unless you want to. You will, however, need the atoi function to convert the command line arguments into decimal values. The atoi code is in gcd.s.
The code to grab the ascii decimal equivalent of a character is in sample.s.
Have your ASCII chart handy (in the lab file on Blackboard).
Note that '-' is 45. The ascii decimal equivalent for '+' is 43. The ascii decimal equivalent for '*' is 42. Display an error condition and exit if you do NOT get a valid operator: ~(42 or 43 or 45).
You can assume your operands will be small enough that you will not need the HI and LO registers; i.e., you can use this instruction to perform multiplication:
mul rd, rs, rt
Your program should accept different command line operations.
After evaluating the expression, determine if the result is even or odd. To do this, divide the result by 2. If the remainder is 0 you know the number is even. Otherwise, the result is odd. Use the div operation as shown below (register usage will differ in your code):
li $t0, 2
div $t1, $t0
Now just check if the contents of $t2 are zero. If yes, branch to code to display the word EVEN. If not, branch to code to display the word ODD.
Your finished program should execute like this:
Your program should now output EVEN or ODD.
What to turn in
Upload your calc.s file to Blackboard.