Question: For project ?ve, your objective is to convert the given C++ code into MIPS assembly. Please do not modify the C++ code itself. You are
For project ?ve, your objective is to convert the given C++ code into MIPS assembly. Please do not modify the C++ code itself. You are only allowed to make modi?cations to the assembly ?le. Start writing your code below the sumOfDoubleEvenPlace: and getDigit: labels. When performing a C++ to MIPS conversion with functions, do so in the following steps:
1. Assign variables to registers. When inspecting the C++ code, any constant values(literals) may need to be assigned to temporary registers. Please check the comments in the .s ?le to see a list of pre-assigned variables to registers. 2. Initialize variables to registers. (actually put the values into the registers.) 3. Then move onto the rest of the code. 4. For functions, remember for non-leaf functions(functions that call other functions, you must saved the values of certain registers you are using to ensure correct code execution. You will save these values on the stack. Pushing values is done via storeword(sw) and popping values is done via loadword(lw). 5. Remember that $t, $v, $a, and $ra registers are assumed not to be preserved across function calls. Before you begin, please make sure you click the link on ilearn to create your GitHub repo. After created please clone this repo with the git clone repo url command.
Expected Output: Expected Value: 23 Value: 23
Expected Value: 21 Value: 21
C++ Code
#include using namespace std;
int sumOfDoubleEvenPlace(int number); int getDigit(int number);
int main(void) { int test1 = 89744563; int test2 = 98756421; int result1 = 0; int result2 = 0;
result1 = sumOfDoubleEvenPlace(test1); cout << "Expected Value: 23 Value: " << result1 << endl;
result2 = sumOfDoubleEvenPlace(test2); cout << "Expected Value: 21 Value: " << result2 << endl; } /* * Function returns the sum of the even placed * digits(after being doubled) starting from the left. * Note that the algorithm starts counting from 1 not 0. Therefore, * given the number 1234, 4 is the first digit from the left. * So the even placed digits are 3 and 1 and the odd place digits are * 4 and 2 from the left. */ int sumOfDoubleEvenPlace(int number) { int sum = 0; int digit; //Remove first odd digit number = number / 10; while (number > 0) { //Grab even placed digit digit = (number % 10); //Double the digit and pass it to getDigit, //Add result to sum sum += getDigit(digit*2); //Remove current even digit and the next odd digit. number = number/100; } return sum; }
/* getDigit returns the sum of the digits in * a 1 or 2 digit number. * if number is < 10, * then we return the number. * else we return the sum of the digits in the 2 digit * number. * For example: * 1 would return 1 * 11 would return 2 * 18 would return 9 */ int getDigit(int number) { int sum = 0; if (number < 10) { sum = number; } else { sum = number%10 + number/10; } return sum; }
MIPS Code:
.data expVal23: .asciiz "Expected Value : 23 Your Value : " expVal21: .asciiz "Expected Value : 21 Your Value : " endl: .asciiz " "
.text
# # # int getDigit(int number); # List Used Registers Here: # # # getDigit:
## # int sumOfDoubleEvenPlace(int number); # List Used Registers Here: # sum --> $s0 # digit --> $s1 # ## sumOfDoubleEvenPlace:
main: li $s0, 89744563 # int test1 = 89744563; li $s1, 98756421 # int test2 = 98756421; li $s2, 0 # int result1 = 0; li $s3, 0 # int result2 = 0;
# code for first function call
add $a0, $0, $s0 jal sumOfDoubleEvenPlace add $s2, $0, $v0
la $a0, expVal23 addi $v0, $0, 4 syscall
move $a0, $s2 addi $v0, $0, 1 syscall
la $a0, endl addi $v0, $0, 4 syscall
# code for first function call
add $a0, $0, $s1 jal sumOfDoubleEvenPlace add $s3, $0, $v0
la $a0, expVal21 addi $v0, $0, 4 syscall
move $a0, $s3 addi $v0, $0, 1 syscall
la $a0, endl addi $v0, $0, 4 syscall
li $v0, 10 syscall
Please keep the registers the same as the provided code and if it works I will upvote. Thank you very much.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
