Skip to content

Commit

Permalink
Add Lab-1: Fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Jul 30, 2014
1 parent b6990f0 commit e9b4707
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lab-1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CC = arm-linux-gnueabihf-gcc
CFLAGS = -O1 -Wall
LDFLAGS = -fno-stack-protector

objects = fibseq.o fib.o

default: fibseq

.PHONY: default clean

fibseq: $(objects)
$(CC) $(LDFLAGS) -o $@ $^

fib.o: fib.s
fibseq.o: fibseq.c

%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<

%.o: %.s
$(CC) -c $(CFLAGS) -o $@ $<

clean:
rm -f $(objects) fibseq

qemu: fibseq
qemu-arm -L /usr/arm-linux-gnueabihf ./fibseq
43 changes: 43 additions & 0 deletions lab-1/fib.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.syntax unified
.arch armv7-a
.text
.align 2
.thumb
.thumb_func

.global fibonacci
.type fibonacci, function

fibonacci:
@ ADD/MODIFY CODE BELOW
@ PROLOG
push {r3, r4, r5, lr}

@ R4 = R0 - 0 (update flags)
@ if(R0 <= 0) goto .L3 (which returns 0)

@ Compare R4 wtih 1
@ If R4 == 1 goto .L4 (which returns 1)

@ R0 = R4 - 1
@ Recursive call to fibonacci with R4 - 1 as parameter

@ R5 = R0
@ R0 = R4 - 2
@ Recursive call to fibonacci with R4 - 2 as parameter

@ R0 = R5 + R0 (update flags)

pop {r3, r4, r5, pc} @EPILOG

@ END CODE MODIFICATION
.L3:
mov r0, #0 @ R0 = 0
pop {r3, r4, r5, pc} @ EPILOG

.L4:
mov r0, #1 @ R0 = 1
pop {r3, r4, r5, pc} @ EPILOG

.size fibonacci, .-fibonacci
.end
15 changes: 15 additions & 0 deletions lab-1/fibseq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

extern int fibonacci(int x);

int main(int argc, char **argv)
{
int number=0;
int result=0;

printf("Please input a number:");
scanf("%d",&number);
result = fibonacci(number);
printf("The fibonacci sequence at %d is: %d\n", number, result);
}

0 comments on commit e9b4707

Please sign in to comment.