about summary refs log tree commit diff stats
path: root/apps/ex3.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-01-01 17:23:29 -0800
committerKartik Agaram <vc@akkartik.com>2020-01-01 17:23:29 -0800
commit113bae7311b63e55e55160970718d19bfeeb56c3 (patch)
treed2468b58470e173329c7ef6e8da496c2f768b037 /apps/ex3.subx
parent1b050736eea04323e09e5e8e7499c33cee1899d0 (diff)
downloadmu-113bae7311b63e55e55160970718d19bfeeb56c3.tar.gz
5856
Diffstat (limited to 'apps/ex3.subx')
-rw-r--r--apps/ex3.subx36
1 files changed, 36 insertions, 0 deletions
diff --git a/apps/ex3.subx b/apps/ex3.subx
new file mode 100644
index 00000000..f6f8c17d
--- /dev/null
+++ b/apps/ex3.subx
@@ -0,0 +1,36 @@
+# Add the first 10 numbers, and return the result in the exit code.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex3.subx -o examples/ex3
+#   $ ./subx run examples/ex3
+# Expected result:
+#   $ echo $?
+#   55
+
+== code
+#   instruction                     effective address                                                   register    displacement    immediate
+# . op          subop               mod             rm32          base        index         scale       r32
+# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
+
+Entry:
+    # result: ebx = 0
+    bb/copy-to-ebx  0/imm32
+    # counter: ecx = 1
+    b9/copy-to-ecx  1/imm32
+
+$loop:
+    # if (counter > 10) break
+    81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0xa/imm32         # compare ecx
+    7f/jump-if-greater  $exit/disp8
+    # result += counter
+    01/add                          3/mod/direct    3/rm32/ebx    .           .             .           1/r32/ecx   .               .                 # add ecx to ebx
+    # ++counter
+    41/increment-ecx
+    # loop
+    eb/jump  $loop/disp8
+
+$exit:
+    # exit(ebx)
+    e8/call  syscall_exit/disp32
+
+# . . vim:nowrap:textwidth=0