diff options
Diffstat (limited to '3/7_procedures/35_rfun.c')
| -rw-r--r-- | 3/7_procedures/35_rfun.c | 25 |
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; +} |
