summaryrefslogtreecommitdiff
path: root/factorial.asm
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2008-01-02 03:55:22 +0000
committerMatt Turner <mattst88@gmail.com>2008-01-02 03:55:22 +0000
commitafda6f12adf869ca9b35eae215b2aa83b87de39a (patch)
tree038c98253ec35dc56aed1d5ab7abde8deb99833e /factorial.asm
Initial import.
git-svn-id: svn://mattst88.com/svn/x86_64-assembly/trunk@1 a323887f-d61c-418c-83a5-2e06a2a01087
Diffstat (limited to 'factorial.asm')
-rw-r--r--factorial.asm46
1 files changed, 46 insertions, 0 deletions
diff --git a/factorial.asm b/factorial.asm
new file mode 100644
index 0000000..612d5e9
--- /dev/null
+++ b/factorial.asm
@@ -0,0 +1,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