about summary refs log tree commit diff stats
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/Readme.md13
-rwxr-xr-xapps/ex1bin0 -> 197 bytes
-rw-r--r--apps/ex1.subx18
-rwxr-xr-xapps/ex10bin0 -> 264 bytes
-rw-r--r--apps/ex10.subx69
-rwxr-xr-xapps/ex11bin0 -> 1178 bytes
-rw-r--r--apps/ex11.subx355
-rwxr-xr-xapps/ex12bin0 -> 234 bytes
-rw-r--r--apps/ex12.subx43
-rwxr-xr-xapps/ex2bin0 -> 198 bytes
-rw-r--r--apps/ex2.subx20
-rwxr-xr-xapps/ex3bin0 -> 215 bytes
-rw-r--r--apps/ex3.subx36
-rwxr-xr-xapps/ex4bin0 -> 236 bytes
-rw-r--r--apps/ex4.subx39
-rwxr-xr-xapps/ex5bin0 -> 238 bytes
-rw-r--r--apps/ex5.subx41
-rwxr-xr-xapps/ex6bin0 -> 232 bytes
-rw-r--r--apps/ex6.subx35
-rwxr-xr-xapps/ex7bin0 -> 366 bytes
-rw-r--r--apps/ex7.subx98
-rwxr-xr-xapps/ex8bin0 -> 234 bytes
-rw-r--r--apps/ex8.subx60
-rwxr-xr-xapps/ex9bin0 -> 228 bytes
-rw-r--r--apps/ex9.subx52
25 files changed, 877 insertions, 2 deletions
diff --git a/apps/Readme.md b/apps/Readme.md
index 1861d949..1e1a5685 100644
--- a/apps/Readme.md
+++ b/apps/Readme.md
@@ -1,2 +1,11 @@
-Larger programs than in the examples/ subdirectory, combining the techniques
-demonstrated there.
+Some apps written in SubX and Mu, in 3 categories:
+
+* `ex*`: small stand-alone examples that don't need any of the shared code at
+  the top-level. They each have a simple pedagogical goal. Try these first.
+
+* Code unique to phases of our build toolchain:
+  * Core SubX: `hex`, `survey`, `pack`, `dquotes`, `assort`, `tests`
+  * Syntax sugar for SubX: `sigils`, `calls`, `braces`
+  * More ambitious translator for a memory-safe language (in progress): `mu`
+
+* Miscellaneous test programs.
diff --git a/apps/ex1 b/apps/ex1
new file mode 100755
index 00000000..5f17bc66
--- /dev/null
+++ b/apps/ex1
Binary files differdiff --git a/apps/ex1.subx b/apps/ex1.subx
new file mode 100644
index 00000000..5558d310
--- /dev/null
+++ b/apps/ex1.subx
@@ -0,0 +1,18 @@
+# First program: same as https://www.muppetlabs.com/~breadbox/software/tiny/teensy.html
+# Just return 42.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex1.2.subx -o examples/ex1
+#   $ ./subx run examples/ex1
+# Expected result:
+#   $ echo $?
+#   42
+
+== code
+
+Entry:
+# exit(42)
+bb/copy-to-ebx  2a/imm32  # 42 in hex
+e8/call  syscall_exit/disp32
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex10 b/apps/ex10
new file mode 100755
index 00000000..cb8973fe
--- /dev/null
+++ b/apps/ex10
Binary files differdiff --git a/apps/ex10.subx b/apps/ex10.subx
new file mode 100644
index 00000000..c091fe52
--- /dev/null
+++ b/apps/ex10.subx
@@ -0,0 +1,69 @@
+# String comparison: return 1 iff the two args passed in at the commandline are equal.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex10.subx -o examples/ex10
+#   $ ./subx run examples/ex10 abc abd
+# Expected result:
+#   $ echo $?
+#   0  # false
+
+== 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:  # return argv-equal(argv[1], argv[2])
+#       At the start of a SubX program:
+#         argc: *esp
+#         argv[0]: *(esp+4)
+#         argv[1]: *(esp+8)
+#         ...
+    # . prologue
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # argv-equal(argv[1], argv[2])
+    # . . push argv[2]
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
+    # . . push argv[1]
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
+    # . . call
+    e8/call argv-equal/disp32
+    # exit(eax)
+    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
+    e8/call  syscall_exit/disp32
+
+# compare two null-terminated ascii strings
+# reason for the name: the only place we should have null-terminated ascii strings is from commandline args
+argv-equal:  # (s1, s2) : null-terminated ascii strings -> eax : boolean
+    # initialize s1 (ecx) and s2 (edx)
+    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           1/r32/ecx   4/disp8         .                 # copy *(esp+4) to ecx
+    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           2/r32/edx   8/disp8         .                 # copy *(esp+8) to edx
+$argv-equal:loop:
+    # c1/eax, c2/ebx = *s1, *s2
+    b8/copy-to-eax  0/imm32
+    8a/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/AL    .               .                 # copy byte at *ecx to AL
+    bb/copy-to-ebx  0/imm32
+    8a/copy-byte                    0/mod/indirect  2/rm32/edx    .           .             .           3/r32/BL    .               .                 # copy byte at *edx to BL
+    # if (c1 == 0) break
+    3d/compare-eax-and  0/imm32
+    74/jump-if-equal  $argv-equal:break/disp8
+    # if (c1 != c2) return false
+    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
+    75/jump-if-not-equal  $argv-equal:false/disp8
+    # ++s1, ++s2
+    41/increment-ecx
+    42/increment-edx
+    # end while
+    eb/jump  $argv-equal:loop/disp8
+$argv-equal:break:
+    # if (c2 == 0) return true
+    81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0/imm32           # compare ebx
+    75/jump-if-not-equal  $argv-equal:false/disp8
+$argv-equal:success:
+    b8/copy-to-eax  1/imm32
+    c3/return
+    # return false
+$argv-equal:false:
+    b8/copy-to-eax  0/imm32
+    c3/return
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex11 b/apps/ex11
new file mode 100755
index 00000000..1cb1aa5c
--- /dev/null
+++ b/apps/ex11
Binary files differdiff --git a/apps/ex11.subx b/apps/ex11.subx
new file mode 100644
index 00000000..b235927d
--- /dev/null
+++ b/apps/ex11.subx
@@ -0,0 +1,355 @@
+# Null-terminated vs length-prefixed ascii strings.
+#
+# By default we create strings with a 4-byte length prefix rather than a null suffix.
+# However we still need null-prefixed strings when interacting with the Linux
+# kernel in a few places. This layer implements a function for comparing
+# a null-terminated 'kernel string' with a length-prefixed 'SubX string'.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex11.subx -o examples/ex11
+#   $ ./subx run examples/ex11  # runs a series of tests
+#   ......  # all tests pass
+#
+# (We can't yet run the tests when given a "test" commandline argument,
+# because checking for it would require the function being tested! Breakage
+# would cause tests to not run, rather than to fail as we'd like.)
+
+== 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:  # run all tests
+    e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
+    # exit(eax)
+    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
+    e8/call  syscall_exit/disp32
+
+# compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
+# reason for the name: the only place we should have null-terminated ascii strings is from commandline args
+kernel-string-equal?:  # s : null-terminated ascii string, benchmark : length-prefixed ascii string -> eax : boolean
+    # pseudocode:
+    #   n = benchmark->length
+    #   s1 = s
+    #   s2 = benchmark->data
+    #   i = 0
+    #   while i < n
+    #     c1 = *s1
+    #     c2 = *s2
+    #     if (c1 == 0) return false
+    #     if (c1 != c2) return false
+    #     ++s1, ++s2, ++i
+    #   return *s1 == 0
+    #
+    # registers:
+    #   i: ecx
+    #   n: edx
+    #   s1: edi
+    #   s2: esi
+    #   c1: eax
+    #   c2: ebx
+    #
+    # . prologue
+    55/push-ebp
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # . save registers
+    51/push-ecx
+    52/push-edx
+    53/push-ebx
+    56/push-esi
+    57/push-edi
+    # s1/edi = s
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
+    # n/edx = benchmark->length
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
+    8b/copy                         0/mod/indirect  2/rm32/edx    .           .             .           2/r32/edx   .               .                 # copy *edx to edx
+    # s2/esi = benchmark->data
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
+    81          0/subop/add         3/mod/direct    6/rm32/esi    .           .             .           .           .               4/imm32           # add to esi
+    # i/ecx = c1/eax = c2/ebx = 0
+    b9/copy-to-ecx  0/imm32/exit
+    b8/copy-to-eax  0/imm32
+    bb/copy-to-ebx  0/imm32
+$kernel-string-equal?:loop:
+    # if (i >= n) break
+    39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           2/r32/edx   .               .                 # compare ecx with edx
+    7d/jump-if-greater-or-equal  $kernel-string-equal?:break/disp8
+    # c1 = *s1
+    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           0/r32/AL    .               .                 # copy byte at *edi to AL
+    # c2 = *s2
+    8a/copy-byte                    0/mod/indirect  6/rm32/esi    .           .             .           3/r32/BL    .               .                 # copy byte at *esi to BL
+    # if (c1 == 0) return false
+    3d/compare-eax-and  0/imm32
+    74/jump-if-equal  $kernel-string-equal?:false/disp8
+    # if (c1 != c2) return false
+    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
+    75/jump-if-not-equal  $kernel-string-equal?:false/disp8
+    # ++i
+    41/increment-ecx
+    # ++s1
+    47/increment-edi
+    # ++s2
+    46/increment-esi
+    eb/jump  $kernel-string-equal?:loop/disp8
+$kernel-string-equal?:break:
+    # return *s1 == 0
+    8a/copy-byte                    0/mod/indirect  7/rm32/edi    .           .             .           0/r32/AL    .               .                 # copy byte at *edi to AL
+    3d/compare-eax-and  0/imm32
+    75/jump-if-not-equal  $kernel-string-equal?:false/disp8
+$kernel-string-equal?:true:
+    b8/copy-to-eax  1/imm32
+    eb/jump  $kernel-string-equal?:end/disp8
+$kernel-string-equal?:false:
+    b8/copy-to-eax  0/imm32
+$kernel-string-equal?:end:
+    # . restore registers
+    5f/pop-to-edi
+    5e/pop-to-esi
+    5b/pop-to-ebx
+    5a/pop-to-edx
+    59/pop-to-ecx
+    # . epilogue
+    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
+    5d/pop-to-ebp
+    c3/return
+
+# - tests
+
+test-compare-null-kernel-string-with-empty-array:
+    # eax = kernel-string-equal?(Null-kernel-string, "")
+    # . . push args
+    68/push  ""/imm32
+    68/push  Null-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 1, msg)
+    # . . push args
+    68/push  "F - test-compare-null-kernel-string-with-empty-array"/imm32
+    68/push  1/imm32/true
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-null-kernel-string-with-non-empty-array:
+    # eax = kernel-string-equal?(Null-kernel-string, "Abc")
+    # . . push args
+    68/push  "Abc"/imm32
+    68/push  Null-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 0, msg)
+    # . . push args
+    68/push  "F - test-compare-null-kernel-string-with-non-empty-array"/imm32
+    68/push  0/imm32/false
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-kernel-string-with-equal-array:
+    # eax = kernel-string-equal?(_test-Abc-kernel-string, "Abc")
+    # . . push args
+    68/push  "Abc"/imm32
+    68/push  _test-Abc-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 1, msg)
+    # . . push args
+    68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
+    68/push  1/imm32/true
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-kernel-string-with-inequal-array:
+    # eax = kernel-string-equal?(_test-Abc-kernel-string, "Adc")
+    # . . push args
+    68/push  "Adc"/imm32
+    68/push  _test-Abc-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 0, msg)
+    # . . push args
+    68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
+    68/push  0/imm32/false
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-kernel-string-with-empty-array:
+    # eax = kernel-string-equal?(_test-Abc-kernel-string, "")
+    # . . push args
+    68/push  ""/imm32
+    68/push  _test-Abc-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 0, msg)
+    # . . push args
+    68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
+    68/push  0/imm32/false
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-kernel-string-with-shorter-array:
+    # eax = kernel-string-equal?(_test-Abc-kernel-string, "Ab")
+    # . . push args
+    68/push  "Ab"/imm32
+    68/push  _test-Abc-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 0, msg)
+    # . . push args
+    68/push  "F - test-compare-kernel-string-with-shorter-array"/imm32
+    68/push  0/imm32/false
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+test-compare-kernel-string-with-longer-array:
+    # eax = kernel-string-equal?(_test-Abc-kernel-string, "Abcd")
+    # . . push args
+    68/push  "Abcd"/imm32
+    68/push  _test-Abc-kernel-string/imm32
+    # . . call
+    e8/call  kernel-string-equal?/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # check-ints-equal(eax, 0, msg)
+    # . . push args
+    68/push  "F - test-compare-kernel-string-with-longer-array"/imm32
+    68/push  0/imm32/false
+    50/push-eax
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
+    c3/return
+
+# - helpers
+
+# print msg to stderr if a != b, otherwise print "."
+check-ints-equal:  # (a : int, b : int, msg : (address array byte)) -> boolean
+    # . prologue
+    55/push-ebp
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # . save registers
+    51/push-ecx
+    53/push-ebx
+    # load args into eax, ebx and ecx
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   0xc/disp8       .                 # copy *(ebp+12) to ebx
+    # if (eax == b/ebx) print('.') and return
+    39/compare                      3/mod/direct    0/rm32/eax    .           .             .           3/r32/ebx   .               .                 # compare eax and ebx
+    75/jump-if-unequal  $check-ints-equal:else/disp8
+    # . write-stderr('.')
+    # . . push args
+    68/push  "."/imm32
+    # . . call
+    e8/call  write-stderr/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+    # . return
+    eb/jump  $check-ints-equal:end/disp8
+    # otherwise print(msg)
+$check-ints-equal:else:
+    # copy msg into ecx
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   0x10/disp8       .                # copy *(ebp+16) to ecx
+    # print(ecx)
+    # . . push args
+    51/push-ecx
+    # . . call
+    e8/call  write-stderr/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+    # print newline
+    # . . push args
+    68/push  Newline/imm32
+    # . . call
+    e8/call  write-stderr/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+$check-ints-equal:end:
+    # . restore registers
+    5b/pop-to-ebx
+    59/pop-to-ecx
+    # end
+    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
+    5d/pop-to-ebp
+    c3/return
+
+write-stderr:  # s : (address array byte) -> <void>
+    # . prologue
+    55/push-ebp
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+    52/push-edx
+    53/push-ebx
+    # syscall(write, 2/stderr, (data) s+4, (size) *s)
+    # . . fd = 2 (stderr)
+    bb/copy-to-ebx  2/imm32
+    # . . x = s+4
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
+    81          0/subop/add         3/mod/direct    1/rm32/ecx    .           .             .           .           .               4/imm32           # add to ecx
+    # . . size = *s
+    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   8/disp8         .                 # copy *(ebp+8) to edx
+    8b/copy                         0/mod/indirect  2/rm32/edx    .           .             .           2/r32/edx   .               .                 # copy *edx to edx
+    # . . syscall
+    e8/call  syscall_write/disp32
+    # . restore registers
+    5b/pop-to-ebx
+    5a/pop-to-edx
+    59/pop-to-ecx
+    58/pop-to-eax
+    # . end
+    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
+    5d/pop-to-ebp
+    c3/return
+
+== data
+
+Newline:
+    # size
+    1/imm32
+    # data
+    0a/newline
+
+# for kernel-string-equal tests
+Null-kernel-string:
+    00/null
+
+_test-Abc-kernel-string:
+    41/A 62/b 63/c 00/null
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex12 b/apps/ex12
new file mode 100755
index 00000000..80e5deeb
--- /dev/null
+++ b/apps/ex12
Binary files differdiff --git a/apps/ex12.subx b/apps/ex12.subx
new file mode 100644
index 00000000..9c67f909
--- /dev/null
+++ b/apps/ex12.subx
@@ -0,0 +1,43 @@
+# Example showing mmap syscall.
+# Create a new segment using mmap, save the address, write to it.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex12.subx -o examples/ex12
+#   $ ./subx run examples/ex12
+# You shouldn't get a segmentation fault.
+
+== 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:
+    # mmap(Mmap-new-segment->len)
+    bb/copy-to-ebx  Mmap-new-segment/imm32
+    e8/call  syscall_mmap/disp32
+
+    # write to *eax to check that we have access to the newly-allocated segment
+    c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0x34/imm32        # copy to *eax
+
+    # exit(eax)
+    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
+    e8/call  syscall_exit/disp32
+
+== data
+
+# various constants used here were found in the Linux sources (search for file mman-common.h)
+Mmap-new-segment:  # type mmap_arg_struct
+    # addr
+    0/imm32
+    # len
+    0x100/imm32
+    # protection flags
+    3/imm32  # PROT_READ | PROT_WRITE
+    # sharing flags
+    0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
+    # fd
+    -1/imm32  # since MAP_ANONYMOUS is specified
+    # offset
+    0/imm32  # since MAP_ANONYMOUS is specified
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex2 b/apps/ex2
new file mode 100755
index 00000000..5e0c1be1
--- /dev/null
+++ b/apps/ex2
Binary files differdiff --git a/apps/ex2.subx b/apps/ex2.subx
new file mode 100644
index 00000000..8df4c62c
--- /dev/null
+++ b/apps/ex2.subx
@@ -0,0 +1,20 @@
+# Add 1 and 1, and return the result in the exit code.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex2.subx -o examples/ex2
+#   $ ./subx run examples/ex2
+# Expected result:
+#   $ echo $?
+#   2
+
+== code
+
+Entry:
+# ebx = 1
+bb/copy-to-ebx  1/imm32
+# increment ebx
+43/increment-ebx
+# exit(ebx)
+e8/call  syscall_exit/disp32
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex3 b/apps/ex3
new file mode 100755
index 00000000..af787c11
--- /dev/null
+++ b/apps/ex3
Binary files differdiff --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
diff --git a/apps/ex4 b/apps/ex4
new file mode 100755
index 00000000..35ade1ee
--- /dev/null
+++ b/apps/ex4
Binary files differdiff --git a/apps/ex4.subx b/apps/ex4.subx
new file mode 100644
index 00000000..78a97433
--- /dev/null
+++ b/apps/ex4.subx
@@ -0,0 +1,39 @@
+# Read a character from stdin, save it to a global, write it to stdout.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex4.subx -o examples/ex4
+#   $ ./subx run examples/ex4
+
+== data
+
+# the global variable we save to
+X:
+    0/imm32  # space for read() to write to
+
+== code
+
+Entry:
+# read(stdin, X, 1)
+# . fd = 0 (stdin)
+bb/copy-to-ebx  0/imm32
+# . data = X (location to write result to)
+b9/copy-to-ecx  X/imm32
+# . size = 1 character
+ba/copy-to-edx  1/imm32
+# . syscall
+e8/call  syscall_read/disp32
+
+# write(stdout, X, 1)
+# . fd = 1 (stdout)
+bb/copy-to-ebx  1/imm32
+# . initialize X (location to read from)
+b9/copy-to-ecx  X/imm32
+# . size = 1 character
+ba/copy-to-edx  1/imm32
+# . syscall
+e8/call  syscall_write/disp32
+
+# exit(ebx)
+e8/call  syscall_exit/disp32
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex5 b/apps/ex5
new file mode 100755
index 00000000..805cadc3
--- /dev/null
+++ b/apps/ex5
Binary files differdiff --git a/apps/ex5.subx b/apps/ex5.subx
new file mode 100644
index 00000000..bacef8d4
--- /dev/null
+++ b/apps/ex5.subx
@@ -0,0 +1,41 @@
+# Read a character from stdin, save it to a local on the stack, write it to stdout.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex5.subx -o examples/ex5
+#   $ ./subx run examples/ex5
+
+== 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:
+
+    # allocate x on the stack
+    81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # subtract from esp
+
+    # read(stdin, x, 1)
+    # . fd = 0 (stdin)
+    bb/copy-to-ebx  0/imm32
+    # . data = x (location to write result to)
+    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              1/r32/ecx   4/disp8         .                 # copy esp+4 to ecx
+    # . size = 1 character
+    ba/copy-to-edx  1/imm32
+    # . syscall
+    e8/call  syscall_read/disp32
+
+    # syscall(write, stdout, x, 1)
+    # . fd = 1 (stdout)
+    bb/copy-to-ebx  1/imm32
+    # . data = x (location to read from)
+    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              1/r32/ecx   4/disp8         .                 # copy esp+4 to ecx
+    # . size = 1 character
+    ba/copy-to-edx  1/imm32
+    # . syscall
+    b8/copy-to-eax  4/imm32/write
+    cd/syscall  0x80/imm8
+
+    # exit(ebx)
+    e8/call  syscall_exit/disp32
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex6 b/apps/ex6
new file mode 100755
index 00000000..06fdfd7b
--- /dev/null
+++ b/apps/ex6
Binary files differdiff --git a/apps/ex6.subx b/apps/ex6.subx
new file mode 100644
index 00000000..42612578
--- /dev/null
+++ b/apps/ex6.subx
@@ -0,0 +1,35 @@
+# Print out a (global variable) string to stdout.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex6.subx -o examples/ex6
+#   $ ./subx run examples/ex6
+#   Hello, world!
+
+== code
+
+# . 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:
+    # write(stdout, X, Size)
+    # . fd = 1 (stdout)
+    bb/copy-to-ebx  1/imm32
+    # . initialize X (location to write result to)
+    b9/copy-to-ecx  X/imm32
+    # . initialize Size
+    8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           2/r32/edx   Size/disp32     .                 # copy *Size to edx
+    # . syscall
+    e8/call  syscall_write/disp32
+
+    # exit(ebx)
+    e8/call  syscall_exit/disp32
+
+== data
+
+Size:  # size of string
+    0x0e/imm32  # 14
+X:  # string to print
+    48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a       00
+#   H  e  l  l  o  ,  ␣  w  o  r  l  d  !  newline  null
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex7 b/apps/ex7
new file mode 100755
index 00000000..0a535d45
--- /dev/null
+++ b/apps/ex7
Binary files differdiff --git a/apps/ex7.subx b/apps/ex7.subx
new file mode 100644
index 00000000..1c551cad
--- /dev/null
+++ b/apps/ex7.subx
@@ -0,0 +1,98 @@
+# Example showing file syscalls.
+#
+# Create a file, open it for writing, write a character to it, close it, open
+# it for reading, read a character from it, close it, delete it, and return
+# the character read.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex7.subx -o examples/ex7
+#   $ ./subx run examples/ex7
+# Expected result:
+#   $ echo $?
+#   97
+
+== 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:
+    # creat(Filename)
+    bb/copy-to-ebx  Filename/imm32
+    b9/copy-to-ecx  0x180/imm32/fixed-perms
+    e8/call  syscall_creat/disp32
+
+    # stream = open(Filename, O_WRONLY, 0)  # we can't use 'fd' because it looks like a hex byte
+    bb/copy-to-ebx  Filename/imm32
+    b9/copy-to-ecx  1/imm32/wronly
+    ba/copy-to-edx  0x180/imm32/fixed-perms
+    e8/call  syscall_open/disp32
+    # save stream
+    bb/copy-to-ebx  Stream/imm32
+    89/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to *ebx
+
+    # write(Stream, "a", 1)
+    # . load stream
+    bb/copy-to-ebx  Stream/imm32
+    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # copy *ebx to ebx
+    # .
+    b9/copy-to-ecx  A/imm32
+    ba/copy-to-edx  1/imm32/size
+    e8/call  syscall_write/disp32
+
+    # close(Stream)
+    # . load stream
+    bb/copy-to-ebx  Stream/imm32
+    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # copy *ebx to ebx
+    # .
+    e8/call  syscall_close/disp32
+
+    # stream = open(Filename, O_RDONLY, 0)
+    bb/copy-to-ebx  Filename/imm32
+    b9/copy-to-ecx  0/imm32/rdonly
+    ba/copy-to-edx  0x180/imm32/fixed-perms
+    e8/call  syscall_open/disp32
+    # . save Stream
+    bb/copy-to-ebx  Stream/imm32
+    89/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to *ebx
+
+    # read(Stream, B, 1)
+    # . load stream
+    bb/copy-to-ebx  Stream/imm32
+    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # copy *ebx to ebx
+    # .
+    b9/copy-to-ecx  B/imm32
+    ba/copy-to-edx  1/imm32/size
+    e8/call  syscall_read/disp32
+
+    # close(Stream)
+    # . load stream
+    bb/copy-to-ebx  Stream/imm32
+    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # copy *ebx to ebx
+    #
+    e8/call  syscall_close/disp32
+
+    # unlink(filename)
+    bb/copy-to-ebx  Filename/imm32
+    e8/call  syscall_unlink/disp32
+
+    # exit(b)
+    # . load b
+    bb/copy-to-ebx  B/imm32
+    8b/copy                         0/mod/indirect  3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # copy *ebx to ebx
+    #
+    e8/call  syscall_exit/disp32
+
+== data
+
+Stream:
+    0/imm32
+A:
+    61/imm32/A
+B:
+    0/imm32
+Filename:
+    2e 66 6f 6f 00 00 00 00
+#   .  f  o  o  null
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex8 b/apps/ex8
new file mode 100755
index 00000000..78f1773d
--- /dev/null
+++ b/apps/ex8
Binary files differdiff --git a/apps/ex8.subx b/apps/ex8.subx
new file mode 100644
index 00000000..0e44a883
--- /dev/null
+++ b/apps/ex8.subx
@@ -0,0 +1,60 @@
+# Example reading commandline arguments: compute length of first arg.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex8.subx -o examples/ex8
+#   $ ./subx run examples/ex8 abc de fghi
+# Expected result:
+#   $ echo $?
+#   3  # length of 'abc'
+#
+# At the start of a SubX program:
+#   argc: *esp
+#   argv[0]: *(esp+4)
+#   argv[1]: *(esp+8)
+#   ...
+# Locals start from esp-4 downwards.
+
+== 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:
+    # . prologue
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # eax = ascii-length(argv[1])
+    # . . push args
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
+    # . . call
+    e8/call  ascii-length/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
+
+    # exit(eax)
+    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
+    e8/call  syscall_exit/disp32
+
+ascii-length:  # s : (address array byte) -> n/eax
+    # edx = s
+    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none  .           2/r32/edx   4/disp8         .                 # copy *(esp+4) to edx
+    # var result/eax = 0
+    b8/copy-to-eax  0/imm32
+$ascii-length:loop:
+    # var c/ecx = *s
+    8a/copy-byte                    0/mod/*         2/rm32/edx    .           .             .           1/r32/CL    .               .                 # copy byte at *edx to CL
+    # if (c == '\0') break
+    81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0/imm32           # compare ecx
+    74/jump-if-equal  $ascii-length:end/disp8
+    # ++s
+    42/increment-edx
+    # ++result
+    40/increment-eax
+    # loop
+    eb/jump  $ascii-length:loop/disp8
+$ascii-length:end:
+    # return eax
+    c3/return
+
+== data
+
+# . . vim:nowrap:textwidth=0
diff --git a/apps/ex9 b/apps/ex9
new file mode 100755
index 00000000..5140cf68
--- /dev/null
+++ b/apps/ex9
Binary files differdiff --git a/apps/ex9.subx b/apps/ex9.subx
new file mode 100644
index 00000000..443e443e
--- /dev/null
+++ b/apps/ex9.subx
@@ -0,0 +1,52 @@
+# Example showing arg order on the stack.
+#
+# Show difference between ascii codes of first letter of first arg and first
+# letter of second arg.
+#
+# To run:
+#   $ ./subx translate init.linux examples/ex9.subx -o examples/ex9
+#   $ ./subx run examples/ex9 z x
+# Expected result:
+#   $ echo $?
+#   2
+#
+# At the start of a SubX program:
+#   argc: *esp
+#   argv[0]: *(esp+4)
+#   argv[1]: *(esp+8)
+#   ...
+# Locals start from esp-4 downwards.
+
+== 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:
+    # . prologue
+    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
+    # ascii-difference(argv[1], argv[2])
+    # . . push argv[2]
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
+    # . . push argv[1]
+    ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
+    # . . call
+    e8/call  ascii-difference/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
+    # exit(eax)
+    89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
+    e8/call  syscall_exit/disp32
+
+ascii-difference:  # (s1, s2) : null-terminated ascii strings
+    # a = first letter of s1 (ecx)
+    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              0/r32/eax   4/disp8         .                 # copy *(esp+4) to eax
+    8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           0/r32/eax   .               .                 # copy *eax to eax
+    # b = first letter of s2 (edx)
+    8b/copy                         1/mod/*+disp8   4/rm32/sib    4/base/esp  4/index/none              1/r32/ecx   8/disp8                           # copy *(esp+8) to ecx
+    8b/copy                         0/mod/indirect  1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # copy *ecx to ecx
+    # a-b
+    29/subtract                     3/mod/direct    0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # subtract ecx from eax
+    c3/return
+
+# . . vim:nowrap:textwidth=0