# Lab 2-1: Fibonacci Reference [林霆寬Fibonacci](https://hackmd.io/pDfcdWwSQqmt3oXsHPXueA?view) ###### tags: `CA2020` ## Rewrite Fibonacci C Code In the beginning, I used recursive C code. After I saw objdump, I found that it's too long. When we comes to execution time, non-recursive C code is much better in this case. This is because we don't need stack. So, the following C code is non-recursive Fibonacci. ```C= #include<stdio.h> void _start() { int x; int temp = 0; int n = 0; int prev = 1; if (n <= 1) x = 1; for (n = 2;n<10;n++) { temp = x; x = x + prev; prev = temp; } volatile char* tx = (volatile char*) 0x40002000; int out = x; *tx = out; } ``` ![](https://i.imgur.com/djwdh9p.png) ![](https://i.imgur.com/b6ljFvO.png) ![](https://i.imgur.com/hIvbYO7.png) ![](https://i.imgur.com/Srm4row.png) ``` riscv-none-embed-gcc -march=rv32i -mabi=ilp32 -O3 -nostdlib Fibonacci.c -o Fibonacci ``` In this code, there's no difference between -O3 and -Os.