about summary refs log tree commit diff stats
path: root/linux/apps/ex3.2.mu
diff options
context:
space:
mode:
Diffstat (limited to 'linux/apps/ex3.2.mu')
-rw-r--r--linux/apps/ex3.2.mu33
1 files changed, 33 insertions, 0 deletions
diff --git a/linux/apps/ex3.2.mu b/linux/apps/ex3.2.mu
new file mode 100644
index 00000000..c5992963
--- /dev/null
+++ b/linux/apps/ex3.2.mu
@@ -0,0 +1,33 @@
+# Unnecessarily use an array to sum 1..10
+#
+# To run:
+#   $ ./translate apps/ex3.2.mu
+#   $ ./a.elf
+#   $ echo $?
+#   55
+
+fn main -> _/ebx: int {
+  # populate a
+  var a: (array int 0xb)  # 11; we waste index 0
+  var i/ecx: int <- copy 1
+  {
+    compare i, 0xb
+    break-if->=
+    var x/eax: (addr int) <- index a, i
+    copy-to *x, i
+    i <- increment
+    loop
+  }
+  # sum
+  var result/edx: int <- copy 0
+  i <- copy 1
+  {
+    compare i, 0xb
+    break-if->=
+    var x/eax: (addr int) <- index a, i
+    result <- add *x
+    i <- increment
+    loop
+  }
+  return result
+}