factorial
This commit is contained in:
8 files changed
+116
-23
No files matched your search
@@ -0,0 +1 @@
|
||||
*.o
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
FROM ubuntu:latest
|
||||
|
||||
RUN apt-get update && apt-get install -y nasm gcc gdb vim
|
||||
|
||||
COPY main.asm /app/main.asm
|
||||
|
||||
WORKDIR /app
|
||||
RUN nasm -f elf64 main.asm -o main.o
|
||||
RUN ld main.o -o main
|
||||
|
||||
CMD ["./main"]
|
||||
@@ -0,0 +1,21 @@
|
||||
ASM = as # GNU Assembler
|
||||
LD = gcc # GCC Linker
|
||||
LD_FLAGS = -nostartfiles
|
||||
|
||||
SRC = factorial.s
|
||||
OBJ = factorial.o
|
||||
OUT = factorial
|
||||
|
||||
all: $(OUT)
|
||||
|
||||
$(OBJ): $(SRC)
|
||||
$(ASM) $(SRC) -o $(OBJ)
|
||||
|
||||
$(OUT): $(OBJ)
|
||||
$(LD) $(LD_FLAGS) $(OBJ) -o $(OUT)
|
||||
|
||||
run: all
|
||||
./$(OUT)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) $(OUT)
|
||||
@@ -1,15 +1,3 @@
|
||||
# Assembly Project Demonstration - Factorial with Recursion
|
||||
Demonstrate how functoin calls work in assembly by using
|
||||
a factorial recursion example.
|
||||
|
||||
## Build
|
||||
|
||||
```bash
|
||||
docker build -t asm .
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```bash
|
||||
docker run asm
|
||||
```
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int factorial(int n)
|
||||
{
|
||||
if (n <= 1)
|
||||
{
|
||||
return n;
|
||||
}
|
||||
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int f;
|
||||
|
||||
f = factorial(5);
|
||||
|
||||
printf("%d\n", f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
.intel_syntax noprefix # allow mov rax, 1 syntax in GAS (GNU Assembly)
|
||||
.global _start
|
||||
|
||||
# factorial
|
||||
# - rdi as input number
|
||||
# - rax as result
|
||||
factorial:
|
||||
push rbp
|
||||
mov rbp, rsp
|
||||
|
||||
cmp rdi, 1
|
||||
jle base_case # if n <= 1 return 1
|
||||
|
||||
push rdi # save n
|
||||
dec rdi # rdi = n-1
|
||||
call factorial # rax = factorial(n-1)
|
||||
|
||||
pop rdx # rdx = original n
|
||||
imul rax, rdx # rax = factorial(n-1) * n
|
||||
jmp done
|
||||
|
||||
base_case:
|
||||
mov rax, 1
|
||||
|
||||
done:
|
||||
mov rsp, rbp
|
||||
pop rbp
|
||||
ret
|
||||
|
||||
print_rax_digit:
|
||||
sub rsp, 16
|
||||
|
||||
mov rbx, 10
|
||||
|
||||
# ones digit
|
||||
xor rdx, rdx
|
||||
mov rcx, rax
|
||||
div rbx
|
||||
add dl, '0'
|
||||
mov [rsp+2], dl
|
||||
|
||||
# tens digit
|
||||
xor rdx, rdx
|
||||
mov rcx, rax
|
||||
div rbx
|
||||
add dl, '0'
|
||||
mov [rsp+1], dl
|
||||
|
||||
# hundreds digit
|
||||
add al, '0'
|
||||
mov [rsp], al
|
||||
|
||||
mov rax, 1
|
||||
mov rdi, 1
|
||||
mov rsi, rsp
|
||||
mov rdx, 3
|
||||
syscall
|
||||
|
||||
add rsp, 16
|
||||
ret
|
||||
|
||||
_start:
|
||||
mov rdi, 5 # Use 5 for our example input
|
||||
mov rax, 1 # Set starting result to 1
|
||||
|
||||
call factorial
|
||||
|
||||
call print_rax_digit
|
||||
|
||||
mov rax, 60 # syscall exit - 64 bit
|
||||
mov rdi, 0 # exit code
|
||||
syscall
|
||||
Reference in new issue
Block a user