diff options
author | Kartik Agaram <vc@akkartik.com> | 2020-01-01 17:23:29 -0800 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2020-01-01 17:23:29 -0800 |
commit | 113bae7311b63e55e55160970718d19bfeeb56c3 (patch) | |
tree | d2468b58470e173329c7ef6e8da496c2f768b037 /examples | |
parent | 1b050736eea04323e09e5e8e7499c33cee1899d0 (diff) | |
download | mu-113bae7311b63e55e55160970718d19bfeeb56c3.tar.gz |
5856
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Readme.md | 6 | ||||
-rwxr-xr-x | examples/ex1 | bin | 197 -> 0 bytes | |||
-rw-r--r-- | examples/ex1.subx | 18 | ||||
-rwxr-xr-x | examples/ex10 | bin | 264 -> 0 bytes | |||
-rw-r--r-- | examples/ex10.subx | 69 | ||||
-rwxr-xr-x | examples/ex11 | bin | 1178 -> 0 bytes | |||
-rw-r--r-- | examples/ex11.subx | 355 | ||||
-rwxr-xr-x | examples/ex12 | bin | 234 -> 0 bytes | |||
-rw-r--r-- | examples/ex12.subx | 43 | ||||
-rwxr-xr-x | examples/ex2 | bin | 198 -> 0 bytes | |||
-rw-r--r-- | examples/ex2.subx | 20 | ||||
-rwxr-xr-x | examples/ex3 | bin | 215 -> 0 bytes | |||
-rw-r--r-- | examples/ex3.subx | 36 | ||||
-rwxr-xr-x | examples/ex4 | bin | 236 -> 0 bytes | |||
-rw-r--r-- | examples/ex4.subx | 39 | ||||
-rwxr-xr-x | examples/ex5 | bin | 238 -> 0 bytes | |||
-rw-r--r-- | examples/ex5.subx | 41 | ||||
-rwxr-xr-x | examples/ex6 | bin | 232 -> 0 bytes | |||
-rw-r--r-- | examples/ex6.subx | 35 | ||||
-rwxr-xr-x | examples/ex7 | bin | 366 -> 0 bytes | |||
-rw-r--r-- | examples/ex7.subx | 98 | ||||
-rwxr-xr-x | examples/ex8 | bin | 234 -> 0 bytes | |||
-rw-r--r-- | examples/ex8.subx | 60 | ||||
-rwxr-xr-x | examples/ex9 | bin | 228 -> 0 bytes | |||
-rw-r--r-- | examples/ex9.subx | 52 |
25 files changed, 0 insertions, 872 deletions
diff --git a/examples/Readme.md b/examples/Readme.md deleted file mode 100644 index d4bb1ff5..00000000 --- a/examples/Readme.md +++ /dev/null @@ -1,6 +0,0 @@ -Small example programs, each with a simple pedagogical goal. - -They also help to validate SubX instruction semantics against native x86 -hardware. For example, loading a single byte to a register would for some time -clear the rest of the register. This behavior was internally consistent with -unit tests. It took running an example binary natively to catch the discrepancy. diff --git a/examples/ex1 b/examples/ex1 deleted file mode 100755 index 5f17bc66..00000000 --- a/examples/ex1 +++ /dev/null Binary files differdiff --git a/examples/ex1.subx b/examples/ex1.subx deleted file mode 100644 index 5558d310..00000000 --- a/examples/ex1.subx +++ /dev/null @@ -1,18 +0,0 @@ -# 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/examples/ex10 b/examples/ex10 deleted file mode 100755 index cb8973fe..00000000 --- a/examples/ex10 +++ /dev/null Binary files differdiff --git a/examples/ex10.subx b/examples/ex10.subx deleted file mode 100644 index c091fe52..00000000 --- a/examples/ex10.subx +++ /dev/null @@ -1,69 +0,0 @@ -# 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/examples/ex11 b/examples/ex11 deleted file mode 100755 index 1cb1aa5c..00000000 --- a/examples/ex11 +++ /dev/null Binary files differdiff --git a/examples/ex11.subx b/examples/ex11.subx deleted file mode 100644 index b235927d..00000000 --- a/examples/ex11.subx +++ /dev/null @@ -1,355 +0,0 @@ -# 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/examples/ex12 b/examples/ex12 deleted file mode 100755 index 80e5deeb..00000000 --- a/examples/ex12 +++ /dev/null Binary files differdiff --git a/examples/ex12.subx b/examples/ex12.subx deleted file mode 100644 index 9c67f909..00000000 --- a/examples/ex12.subx +++ /dev/null @@ -1,43 +0,0 @@ -# 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/examples/ex2 b/examples/ex2 deleted file mode 100755 index 5e0c1be1..00000000 --- a/examples/ex2 +++ /dev/null Binary files differdiff --git a/examples/ex2.subx b/examples/ex2.subx deleted file mode 100644 index 8df4c62c..00000000 --- a/examples/ex2.subx +++ /dev/null @@ -1,20 +0,0 @@ -# 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/examples/ex3 b/examples/ex3 deleted file mode 100755 index af787c11..00000000 --- a/examples/ex3 +++ /dev/null Binary files differdiff --git a/examples/ex3.subx b/examples/ex3.subx deleted file mode 100644 index f6f8c17d..00000000 --- a/examples/ex3.subx +++ /dev/null @@ -1,36 +0,0 @@ -# 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/examples/ex4 b/examples/ex4 deleted file mode 100755 index 35ade1ee..00000000 --- a/examples/ex4 +++ /dev/null Binary files differdiff --git a/examples/ex4.subx b/examples/ex4.subx deleted file mode 100644 index 78a97433..00000000 --- a/examples/ex4.subx +++ /dev/null @@ -1,39 +0,0 @@ -# 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/examples/ex5 b/examples/ex5 deleted file mode 100755 index 805cadc3..00000000 --- a/examples/ex5 +++ /dev/null Binary files differdiff --git a/examples/ex5.subx b/examples/ex5.subx deleted file mode 100644 index bacef8d4..00000000 --- a/examples/ex5.subx +++ /dev/null @@ -1,41 +0,0 @@ -# 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/examples/ex6 b/examples/ex6 deleted file mode 100755 index 06fdfd7b..00000000 --- a/examples/ex6 +++ /dev/null Binary files differdiff --git a/examples/ex6.subx b/examples/ex6.subx deleted file mode 100644 index 42612578..00000000 --- a/examples/ex6.subx +++ /dev/null @@ -1,35 +0,0 @@ -# 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/examples/ex7 b/examples/ex7 deleted file mode 100755 index 0a535d45..00000000 --- a/examples/ex7 +++ /dev/null Binary files differdiff --git a/examples/ex7.subx b/examples/ex7.subx deleted file mode 100644 index 1c551cad..00000000 --- a/examples/ex7.subx +++ /dev/null @@ -1,98 +0,0 @@ -# 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/examples/ex8 b/examples/ex8 deleted file mode 100755 index 78f1773d..00000000 --- a/examples/ex8 +++ /dev/null Binary files differdiff --git a/examples/ex8.subx b/examples/ex8.subx deleted file mode 100644 index 0e44a883..00000000 --- a/examples/ex8.subx +++ /dev/null @@ -1,60 +0,0 @@ -# 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/examples/ex9 b/examples/ex9 deleted file mode 100755 index 5140cf68..00000000 --- a/examples/ex9 +++ /dev/null Binary files differdiff --git a/examples/ex9.subx b/examples/ex9.subx deleted file mode 100644 index 443e443e..00000000 --- a/examples/ex9.subx +++ /dev/null @@ -1,52 +0,0 @@ -# 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 |