CMPS-2240 Lab 6
Write a Calculator

Gordon Griesel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Introduction

Goals:
Parse command-line arguments.
Work with arithmetic instructions.

Resources:
MIPS Instruction Reference
MIPS Tutorial Part 5 (Ch 17 - Ch 20):
Appendix A.10 in the text
Sample programs

Get the files: cp /home/fac/gordon/public_html/2240/code/lab6/* .

It is often the case that 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:

Next, copy calc.s to mycalc.s

Note: Please finish part 1 during class.

Your current mycalc.s prompts the user to input two integers, sums the integers and displays the result like this:


$ spim -f mycalc.s
Enter an integer: 11
Enter an integer: 23
34


 Part 1

Modify mycalc.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.


$ spim -f mycalc.s 11 + 12
23

$ spim -f mycalc.s 22 - 12
10

$ spim -f mycalc.s 5 \* 20
100

Notice:
An asterisk needs to 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.

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).

Try using these in your MIPS program statements:   '-'   '+'   '*'


Part 2

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 Do not use rem command. (register usage will differ in your code):

li  $t0, 2
div $t1, $t0
mfhi $t2

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:


$ spim -f mycalc.s 22 - 12
10
EVEN

$ spim -f mycalc.s 5 \* 11
55
ODD

Your program should now output EVEN or ODD.

Do not break your program trying to make this work.

What to turn in

Your instructor will collest your /2240/6/mycalc.s program on Odin.

If you don't finish this program during Thursday's lab session, then continue to work on it in the tutoring center or at home.