Question: Convert the following C program into MIPS assambly code, the code is about a simple calculator. Fist ask for user input, option 1 is for

Convert the following C program into MIPS assambly code, the code is about a simple calculator. Fist ask for user input, option 1 is for addition, option 2 is for subtraction option 3 is for multiplication. Here's the C code:
#include
int main(){
int op;
printf("Please enter option (1: add, 2: sub, 3: mul): ");
scanf("%d", &op);
int a, b;
printf("Please enter the first number: ");
scanf("%d", &a);
printf("Please enter the second number: ");
scanf("%d", &b);
int ans;
if (op ==1){
ans = a + b;
} else if (op ==2){
ans = a - b;
} else if (op ==3){
ans = a * b;
}
printf("The calculation result is: %d
", ans);
return 0;
}
test your code to see if the output is the similar to the c code given using MIPS32 simulator and using this test.h script:
#!/bin/bash
TESTCASE_ROOT="./testcase"
TESTCASE_ANSWER_ROOT="./testcase_answer"
STUDENT_ANSWER_ROOT="./student_answer"
problems=(
"factorial"
"prime"
"calculator"
"triangle"
"fibonacci"
)
test_problem(){
local prolbem=$1
local source_c_file="./${problem}.c"
local source_asm_file="./${problem}.s"
local executable_file="./$problem"
echo "Testing Problem: $problem"
if [!-f "$source_c_file" ]; then
echo "Source c file not found: $source_c_file"
return
fi
if [!-f "$source_asm_file" ]; then
echo "Source asm file not found: $source_asm_file"
return
fi
gcc "$source_c_file" -o "$executable_file"
testcase_files=($(ls $TESTCASE_ROOT | grep $problem))
for file in "${testcase_files[@]}"; do
local testcase_path="$TESTCASE_ROOT/$file"
local testcase_answer_path="$TESTCASE_ANSWER_ROOT/$file"
local student_answer_path="$STUDENT_ANSWER_ROOT/$file"
"$executable_file" <"$testcase_path" >"$testcase_answer_path"
spim -file "$source_asm_file" <"$testcase_path" | tail -n $(awk 'END {print NR}' $testcase_answer_path)>"$student_answer_path"
diff_output=$(diff $testcase_answer_path $student_answer_path)
if [-z "$diff_output" ]; then
echo "$file PASS"
else
echo "$file FAIL"
echo "$diff_output"
fi
done
rm "$executable_file"
}
mkdir -p "$TESTCASE_ANSWER_ROOT"
mkdir -p "$STUDENT_ANSWER_ROOT"
for problem in "${problems[@]}"; do
test_problem "$problem"
echo "------------------------------------"
done

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 Databases Questions!