Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Convert the following C code into MIPS Assembly code, the program is about fibonacci seqeuence. Here's the code: #include int fibonacci ( int n )

Convert the following C code into MIPS Assembly code, the program is about fibonacci seqeuence. Here's the code:
#include
int fibonacci(int n){
if (n ==0){
return 0;
} else if (n ==1){
return 1;
} else {
return fibonacci(n-1)+ fibonacci(n-2);
}
}
int main(){
int n;
printf("Please input a number: ");
scanf("%d", &n);
printf("The result of fibonacci(n) is %d
", fibonacci(n));
return 0;
}
Check the code results with the following test.h script, to see whether the output is similar to that of the C code, here's the 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
You can create your own test cases. Please run it on Ubuntu (linux), test the code using MIPS32 simulator.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions