mirror of
https://github.com/ringtailsoftware/uvm32.git
synced 2026-06-05 22:43:39 +00:00
Fibonacci sample
This commit is contained in:
parent
f235265e3f
commit
30cf60561f
2 changed files with 75 additions and 0 deletions
27
apps/fib/Makefile
Normal file
27
apps/fib/Makefile
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
PROJECT:=fib
|
||||||
|
|
||||||
|
DOCKER_IMAGE=riscv-dev
|
||||||
|
DOCKER_CMD:=docker run --rm -v ${PWD}../../../:/data -w /data/apps/${PROJECT} ${DOCKER_IMAGE}
|
||||||
|
PREFIX:=${DOCKER_CMD} riscv64-unknown-elf-
|
||||||
|
CFLAGS+=-I../../common
|
||||||
|
CFLAGS+=-fno-stack-protector
|
||||||
|
CFLAGS+=-static-libgcc -fdata-sections -ffunction-sections
|
||||||
|
CFLAGS+=-g -Os -march=rv32ima_zicsr -mabi=ilp32 -static
|
||||||
|
LDFLAGS:= -T ../linker.ld -nostdlib -Wl,--gc-sections
|
||||||
|
LIBS:= #-lgcc # needed for softfp
|
||||||
|
|
||||||
|
SRCS=${PROJECT}.c ../crt0.s
|
||||||
|
|
||||||
|
all:
|
||||||
|
${PREFIX}gcc -o ${PROJECT}.elf ${CFLAGS} ${LDFLAGS} ${SRCS} ${LIBS}
|
||||||
|
$(PREFIX)objcopy ${PROJECT}.elf -O binary ${PROJECT}.bin
|
||||||
|
|
||||||
|
disasm: all
|
||||||
|
$(PREFIX)objdump -S -d -f ${PROJECT}.elf
|
||||||
|
|
||||||
|
test: all
|
||||||
|
../../host/host ${PWD}/${PROJECT}.bin
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f ${PROJECT}.o ${PROJECT}.elf ${PROJECT}.bin
|
||||||
|
|
||||||
48
apps/fib/fib.c
Normal file
48
apps/fib/fib.c
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
#define USE_MAIN
|
||||||
|
#include "uvm32_target.h"
|
||||||
|
|
||||||
|
// print n terms of fibonacci series
|
||||||
|
void printFib(int n) {
|
||||||
|
int prev1 = 1;
|
||||||
|
int prev2 = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
if (i > 2) {
|
||||||
|
int curr = prev1 + prev2;
|
||||||
|
prev2 = prev1;
|
||||||
|
prev1 = curr;
|
||||||
|
printd(curr);
|
||||||
|
} else if (i == 1) {
|
||||||
|
printd(prev2);
|
||||||
|
} else if (i == 2) {
|
||||||
|
printd(prev1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// print n terms of fibonacci series, recursively
|
||||||
|
void fib_recursive(int n, int prev1, int prev2) {
|
||||||
|
if (n < 3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int curr = prev1 + prev2;
|
||||||
|
printd(curr);
|
||||||
|
|
||||||
|
return fib_recursive(n - 1, prev2, curr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printFibRec(int n) {
|
||||||
|
printd(0);
|
||||||
|
printd(1);
|
||||||
|
fib_recursive(n, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(void) {
|
||||||
|
int n = 40;
|
||||||
|
println("fib() loop");
|
||||||
|
printFib(n);
|
||||||
|
println("fib() recursive");
|
||||||
|
printFibRec(n);
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue