about summary refs log tree commit diff stats
path: root/examples/ex3.subx
diff options
context:
space:
mode:
Diffstat (limited to 'examples/ex3.subx')
-rw-r--r--examples/ex3.subx39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/ex3.subx b/examples/ex3.subx
new file mode 100644
index 00000000..1d52e87f
--- /dev/null
+++ b/examples/ex3.subx
@@ -0,0 +1,39 @@
+# Add the first 10 numbers, and return the result in the exit code.
+#
+# To run (from the subx directory):
+#   $ ./subx translate examples/ex3.subx -o examples/ex3
+#   $ ./subx run examples/ex3
+# Expected result:
+#   $ echo $?
+#   55
+
+== code 0x09000000
+#   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:
+    # syscall(exit, EBX)
+    b8/copy-to-EAX  1/imm32/exit
+    cd/syscall  0x80/imm8
+
+== data 0x0a000000
+
+# . . vim:nowrap:textwidth=0