summaryrefslogtreecommitdiff
path: root/3/7_procedures/35_rfun.c
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2024-06-14 09:23:32 +0200
committerMike Vink <mike@pionative.com>2024-06-14 09:23:32 +0200
commit8092f4c334db547ced59d6f439b558dad35e1ab2 (patch)
treefa462fa885efea1ec6095d015286998d632c2c3d /3/7_procedures/35_rfun.c
parentb424517a33bf61aedff29eed74a665402ab496ba (diff)
commit for once
Diffstat (limited to '3/7_procedures/35_rfun.c')
-rw-r--r--3/7_procedures/35_rfun.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/3/7_procedures/35_rfun.c b/3/7_procedures/35_rfun.c
new file mode 100644
index 0000000..f923366
--- /dev/null
+++ b/3/7_procedures/35_rfun.c
@@ -0,0 +1,25 @@
+// x in %rdi
+// rfun:
+// pushq %rbx save rbx, since we use it and in case we are called
+// movq %rdi, %rbx %rbx <- x
+// movl $0 %rax <- 0
+// testq %rdi, %rdi ZF <- %rdi == 0
+// je .L2 jump to if ZF
+// shrq $2, %rdi shift x to the lift by 2 bits (divide by four)
+// call rfun
+// addq %rbx, %rax add our calculation to the rest
+// .L2
+// popq %rbx set rbx back to the value of our caller's calculation
+// ret
+//
+// A>
+// The value x is stored in rbx, and each iteration stores (int64_t) x/4
+// B>
+long rfun(unsigned long x) {
+ if (x == 0) {
+ return 0;
+ }
+ unsigned long nx = x>>2;
+ long rv = rfun(nx);
+ return x + rv;
+}