forked from sonictk/asm_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial_no_recursion.asm
More file actions
60 lines (41 loc) · 802 Bytes
/
factorial_no_recursion.asm
File metadata and controls
60 lines (41 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
default rel
bits 64
segment .data
fmt db "%d", 0xd, 0xa, 0
segment .text
global main
global factorial
extern _CRT_INIT
extern ExitProcess
extern printf
factorial:
push rbp
mov rbp, rsp
sub rsp, 32
test ecx, ecx ; n
jz .zero
mov ebx, 1 ; counter c
mov eax, 1 ; result
inc ecx
.for_loop:
cmp ebx, ecx
je .end_loop
mul ebx ; multiply ebx * eax and store in eax
inc ebx ; ++c
jmp .for_loop
.zero:
mov eax, 1
.end_loop:
leave
ret
main:
push rbp
mov rbp, rsp
sub rsp, 32
mov rcx, 5
call factorial
lea rcx, [fmt]
mov rdx, rax
call printf
xor rax, rax
call ExitProcess