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.

computer science

Description


Background

 

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:

 



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


 

 

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

 

Upload your calc.s file to Blackboard.


Related Questions in computer science category