Question: Part 2 ( 6 5 points ) You are required to write a RISC - V program that has at least six functions: main, do

Part 2(65 points)
You are required to write a RISC-V program that has at least six functions: main,
do_math, do_add, do_subtract, atoi, itoa. You are allowed to define
additional functions as needed.
The main function does the following:
1. Print the following prompt message:
Please enter an integer:
2. Read an integer from user.
3. Print the following prompt message:
Please enter an operator (+,-):
4. Read a character from user.
5. Print the following prompt message:
Please enter an integer:
6. Read an integer from user.
7. Call do_math. Pass the two integers and the operator using registers.
8. Return 0 from main using jr (do not use the 'exit' syscall).
The do_math function takes as argument three registers and does the following:
1. If the operator register contains '+', call do_add passing the two integers in
registers and receiving the return values in two registers.
2. Otherwise, if the operator register contains '-', call do_subtract passing the
two integers in registers and receiving the return values in two registers.
3. Otherwise, print the following error message, replacing OP with the character
stored in the operator register, and exit the program.
Error: invalid arithmetic operation 'OP'.
4. Print the following message:
X OP Y = Z.
Where Z is the arithmetical result of the operation OP conducted on the two
integers input from the user, X and Y.
If the operation results in an integer overflow, you should print the message:
Error: arithmetic overflow.
The do_add function takes as argument two integers in registers and does the following:
1. Add the two integers without using the 'add*' or 'sub*' RISC-V instruction. This
restriction applies to any functions that do_add calls. You may use addi to adjust
the stack pointer if necessary for making function calls. You will likely need to
use the 'or', 'and', 'xor', 'sll', and 'srl' instructions.
2. Return the result in a register. If there is an arithmetic overflow, return an error
condition identifying that an overflow occurred in another register.
The do_sub function takes as argument two integers in registers and does the following:
1. Subtract the second integer from the first without using the 'sub*' or 'add*' RISC-
V instructions. You may however use addi to adjust the stack pointer. You should
take the 2's complement of the second integer, and then call do_add. Note: to take
the 2's complement you should use 'nor', 'ori', and a call to do_add, thus there will
be two calls to do_add!
2. Return the result in a register. If there is an arithmetic overflow, return an error
condition identifying that an overflow occurred in another register.
The atoi function converts the initial part of the given string into an integer value. It
has the prototype:
int atoi(const char *nptr);
It returns the integer value.
The itoa function converts the given integer to a string in base 10. It has the prototype
char *itoa(int num, char *ptr);
It returns the resulting null-terminated string (identical to ptr).
Part 3(25 points)
Test cases to validate the program in Part 2. You need to create unit tests for each
function that validate its behavior independent from main. You need to create integration
tests that validate the behavior of the entire program. Be sure to test for correct inputs,
corner cases, and invalid inputs

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!