diff --git a/createfs b/createfs old mode 100644 new mode 100755 diff --git a/elfconvert b/elfconvert old mode 100644 new mode 100755 diff --git a/syscalls/Makefile b/syscalls/Makefile index e59b153..7bc758c 100644 --- a/syscalls/Makefile +++ b/syscalls/Makefile @@ -2,7 +2,7 @@ CFLAGS += -Wall -nostdlib -ffreestanding LDFLAGS += -nostdlib -ffreestanding CC = gcc -ALL: cat grep hello ls pingpong counter shell sigtest testprint syserr +ALL: cat grep hello ls pingpong counter shell sigtest testprint syserr factorial %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< diff --git a/syscalls/ece391factorial.c b/syscalls/ece391factorial.c new file mode 100644 index 0000000..a4cadfe --- /dev/null +++ b/syscalls/ece391factorial.c @@ -0,0 +1,44 @@ +#include + +#include "ece391support.h" +#include "ece391syscall.h" + +#define BUFSIZE 10 + +int main () +{ + uint8_t buf[BUFSIZE]; + uint32_t cnt, i, num; + + ece391_fdputs(1, (uint8_t*)"Enter number to calculate factorial of:\n"); + if (-1 == (cnt = ece391_read(0, buf, BUFSIZE-1)) ) { + ece391_fdputs(1, (uint8_t*)"Can't read the number from keyboard.\n"); + return 3; + } + buf[cnt] = '\0'; + + num = 0; + i = 0; + while (buf[i] >= '0' && buf[i] <= '9') + { + num *= 10; + num += (uint32_t)(buf[i] - '0'); + i++; + } + + cnt = 1; + while (num != 0) + { + cnt *= num; + num--; + } + + uint8_t out[BUFSIZE]; + ece391_itoa(cnt, out, 10); + + ece391_fdputs(1, (uint8_t*)"Factorial: "); + ece391_fdputs(1, out); + ece391_fdputs(1, (uint8_t*)"\n"); + + return 0; +} diff --git a/syscalls/to_fsdir/factorial b/syscalls/to_fsdir/factorial new file mode 100755 index 0000000..3fff04c Binary files /dev/null and b/syscalls/to_fsdir/factorial differ