summaryrefslogtreecommitdiff
path: root/3/6_control/23_reverse_engineer_loop.c
diff options
context:
space:
mode:
authorMike Vink <mike@pionative.com>2024-05-22 08:49:29 +0200
committerMike Vink <mike@pionative.com>2024-05-22 08:49:29 +0200
commit51169f5f9ab178a4ddfe9dac461405a71c9c0f94 (patch)
tree0b6bb0c6c31ee27361b28e2c5993f362c1cc95e2 /3/6_control/23_reverse_engineer_loop.c
parent77f19e4a89d8dec97930c5e237139734c5fb3365 (diff)
organise
Diffstat (limited to '3/6_control/23_reverse_engineer_loop.c')
-rw-r--r--3/6_control/23_reverse_engineer_loop.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/3/6_control/23_reverse_engineer_loop.c b/3/6_control/23_reverse_engineer_loop.c
new file mode 100644
index 0000000..922b317
--- /dev/null
+++ b/3/6_control/23_reverse_engineer_loop.c
@@ -0,0 +1,28 @@
+short dw_loop(short x)
+{
+ short y = x/9;
+ short *p = &x;
+ short n = 4*x;
+ do {
+ x += y;
+ (*p) += 5;
+ n += 2;
+ } while (n > 0);
+ return x;
+}
+
+// x in %rdi
+// dw_loop:
+// movq %rdi, %rdx # %rbx <- x
+// movq %rdi, %rcx # %rcx <- x
+// idivq $9, %rcx # %rcx (y) <- x / 9
+// leaq (,%rdi,4), %rdx # %rdx (n) <- x * 4
+// .L2:
+// leaq 5(%rbx,%rcx), %rcx # %rbx (x) <- 5 + %rbx (x) + %rcx (y)
+// subq $1, %rdx # %rdx (n) <- n - 1
+// testq %rdx, %rdx # n > 0 ?
+// jg .L2 # if n > 0, goto .L2
+// rep; ret
+//
+// A. x in rbx, y in rcx, n in rdx
+// B. The pointer is never used besides incrementing the value at the address. So it is replaced by a incrementing instruction in the loop.