CMPS-2240 Lab 5
Write a Calculator

Eddie Rangel
Department of Computer and Electrical Engineering and Computer Science
California State University, Bakersfield

Notice:
Due:

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

Files Needed:
calc.s at calc.s at http://cs.csubak.edu/~eddie/cmps2240/code/lab5/calc.s
gcd.s at http://cs.csubak.edu/~eddie/cmps2240/code/lab5/gcd.s
sample.s at http://cs.csubak.edu/~eddie/cmps2240/code/lab5/sample.s

Get the files: cp /home/stu/eddie/public_html/cmps2240/code/lab5/* .

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:

Currently calc.s prompts the user to input two integers, sums the integers and displays the result like this:

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

Part One

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.

$ spim -f calc.s 11 + 12
23

$ spim -f calc.s 22 - 12
10

$ spim -f calc.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).

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.


Step Two

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
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 calc.s 22 - 12
10
EVEN

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

Your program should now output EVEN or ODD.

What to turn in

Your instructor will look for your /2240/5/calc.s program on Odin.