articles/assembly.md: add x86-64 factorial assembly
This commit is contained in:
parent
75186390a0
commit
08e7f3361d
1 changed files with 26 additions and 1 deletions
|
@ -65,8 +65,33 @@ cc -nostdlib -o hello hello.o
|
||||||
```
|
```
|
||||||
|
|
||||||
### Factorial (x86-64, Linux)
|
### Factorial (x86-64, Linux)
|
||||||
|
Returns the calculated factorial as exit code (which is 255 max.)
|
||||||
|
|
||||||
```nasm
|
```nasm
|
||||||
|
default rel
|
||||||
|
|
||||||
|
section .text
|
||||||
|
global _start
|
||||||
|
|
||||||
|
; Compute the factorial of `rdi`, saving the result in `rax`
|
||||||
|
fact:
|
||||||
|
mov eax, 1 ; register holding result
|
||||||
|
.l:
|
||||||
|
mul rdi ; rax *= rdi
|
||||||
|
dec rdi ; decrement rdi
|
||||||
|
test rdi, rdi ; check if rdi = 0
|
||||||
|
jne .l ; loop again if rdi != 0
|
||||||
|
ret
|
||||||
|
|
||||||
|
_start:
|
||||||
|
; x86 registers are sliced, here we assign to edi (32 bit slice)
|
||||||
|
; which gets extended to rdi later...
|
||||||
|
mov edi, 5
|
||||||
|
call fact
|
||||||
|
; exit syscall
|
||||||
|
mov rdi, rax
|
||||||
|
mov eax, 0x3c
|
||||||
|
syscall
|
||||||
```
|
```
|
||||||
TODO
|
|
||||||
|
|
||||||
TODO examples for other architectures
|
TODO examples for other architectures
|
||||||
|
|
Loading…
Add table
Reference in a new issue