summaryrefslogtreecommitdiff
path: root/PGU/OLD/chapter4/factorial.s
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /PGU/OLD/chapter4/factorial.s
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'PGU/OLD/chapter4/factorial.s')
-rw-r--r--PGU/OLD/chapter4/factorial.s73
1 files changed, 73 insertions, 0 deletions
diff --git a/PGU/OLD/chapter4/factorial.s b/PGU/OLD/chapter4/factorial.s
new file mode 100644
index 0000000..749f57e
--- /dev/null
+++ b/PGU/OLD/chapter4/factorial.s
@@ -0,0 +1,73 @@
+##################################################################
+# Calculate the factorial of a number, using recursive functions #
+##################################################################
+
+
+#
+# %rbx holds the number to factor
+#
+.section .data
+
+.section .text
+
+.globl _start
+_start:
+ pushq $5
+ call factorial2
+ addq $8, %rsp # Turn back the stack pointer
+ # Not really needed here, but good practice
+ movq %rax, %rbx
+ movq $1, %rax
+ int $0x80
+
+.type factorial, @function
+
+# This function is garbage, can be much more clean than this shit.
+
+factorial:
+ pushq %rbp # Regular initial function stuff
+ movq %rsp, %rbp # Save %rbp and reset it to %rsp
+
+ movq 16(%rbp), %rbx # Get the argument passed into %rbx
+ movq %rbx, %rcx # Copy it to %rcx and decrement it
+ decq %rcx # here. %rcx will be the next element in the
+ # factorial.
+ #
+ cmpq $1, %rcx # Compare it to 1 and exit if true, we are at
+ je exit_factorial # the end of the factorial
+
+ pushq %rbx # If not one, save the original argument in the
+ pushq %rcx # stack and push the next value as argument to
+ call factorial # the recursive call
+ popq %rcx # pop value to restore stack pointer
+ popq %rbx # restore %rbx and multiply it
+ imul %rax, %rbx # by the result
+
+exit_factorial:
+ movq %rbx, %rax # Move the result to %rax
+ movq %rbp, %rsp # And do standard clean up before
+ popq %rbp # returning.
+ ret
+
+# This is a clean version after extra thought on this
+
+.type factorial2, @function
+factorial2:
+ pushq %rbp # Basic function setup, save %rbp and
+ movq %rsp, %rbp # setup it to current %rsp
+
+ movq 16(%rbp), %rax # Get the argument passed into
+ cmpq $1, %rax #
+ je factorial2_end # We are at the last element in the factorial
+ # Just return it
+ decq %rax
+ pushq %rax
+ call factorial2
+ movq 16(%rbp), %rbx
+ imulq %rbx, %rax
+
+factorial2_end:
+ movq %rbp, %rsp
+ popq %rbp
+ ret
+