summaryrefslogtreecommitdiff
path: root/factorial.asm
blob: 612d5e911e53c8ca4e368681861226844d3d7490 (plain)
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
format ELF64

extrn	printf
extrn	scanf
extrn	puts
public	main

section '.text' executable

main:	push	rbp
	mov	rbp,rsp
	
	mov	edi,prompt
	call	puts

	sub	rsp,16
	
	mov	rsi,rsp
	mov	edi,scan
	xor	eax,eax
	call	scanf

	mov	rax,[rsp]
	
	add	rsp,16
	
	mov	rsi,rax
	lea	rcx,[rax-1]
top:	mul	rcx
	sub	rcx,1
	jnz	top
	
print:	mov	edi,msg
	mov	rdx,rax
	xor	eax,eax
	call	printf
	
	leave
	ret
	

section '.data' writable

msg	db	"Factorial of %d is %d",0xA,0
scan	db	"%d",0
prompt	db	"Enter an unsigned integer.",0