Thursday, February 3, 10pm
Submit the contents of your repository via Gradescope. See Deliverables below for what to submit.
This is an individual assignment.
Please see Assignment 2 on Canvas for the Github Classroom link.
This assignment asks you to implement a simple command line calculator in assembly. The provided starter code retrieves the command line arguments, you have to fill in the rest of the code.
Your task is to complete the main
function (under the
label main
) in
calculator.s
so that it implements the following
functionality:
The calculator can be invoked on the command line as follows:
$ ./calculator <operation> <number1> <number2>
The four valid operations are:
+
: add-
: subtract*
: multiply/
: divideYou may assume that the first argument is always a single character (which might or might not be a valid operation) and that the 2nd and 3rd argument are valid long integers.
When the program is invoked with a valid operation and 2 valid signed long integers, the result of the corresponding operation should be printed to the terminal. An error message should be printed if the operation cannot be performed. There should be only one line of output and that line should only contain a single long integer or an error message.
A sample of the output from your program should look something like the following.
$ ./calculator - 50 51
-1
To make it more clear:
$ ./calculator - 50 51
-1
^ ^ ^ ^ ^--- long2
| | | +------ long1
| | +-------- subtract '-'
| +--------------- name of executable
+---------------------- long1 - long2
If the operation is not valid (i.e., not one of +
,
-
, *
, or /
), the error message
should be Unknown operation
. Other error messages are up to
you.
Here are a few more examples of running your program:
$ ./calculator + 12 43
55
$ ./calculator "*" 33 78
2574
$ ./calculator / 3 11
0
$ ./calculator ^ 57 3
Unknown operation
Note that you need to quote the asterisk "*"
on the
command line to prevent the shell from interpreting it as a special
character (https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm).
We have provided a very basic Makefile for
you. You can compile your program simply by running make
on
the command line. Running make clean
should help you
prepare your working directory for committing to git and for
submission.
Modify the file calculator.s
and commit it to your
repository.
Do not include any executables, object files, or any other binary, intermediate or hidden files.
Finally, go to our Gradescope and submit your repository (or a ZIP archive which can be downloaded from Github)
Make sure you understand the provided starter code, in particular where we store the arguments for you.
Each argument is a string, that is the value is actually a memory address pointing to the first character of the string in memory.
You will need to use the atol
C function to convert
the two strings into the corresponding long integers. You may need to
call other C functions as well.
Pay close attention to the calling conventions and the use of registers when calling C functions.
The instruction for division is used differently from the other arithmetic instructions. You will need to do some research and reading.
Use examples from the lectures and the lab to help you get unstuck and ask questions.