about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2019-01-15 16:36:24 -0800
committerKartik Agaram <vc@akkartik.com>2019-01-15 16:36:24 -0800
commitbf58189697ff3f28861ea1f453048a38e0179199 (patch)
tree70600bf04077e16d070301d1f063920578d383f7
parent7011322ad1640067d354e5a8f68a07963f7b898e (diff)
downloadmu-bf58189697ff3f28861ea1f453048a38e0179199.tar.gz
4930
-rw-r--r--subx/064print-byte.subx2
-rwxr-xr-xsubx/apps/crenshaw2-1bin14144 -> 14148 bytes
-rwxr-xr-xsubx/apps/crenshaw2-1bbin14703 -> 14707 bytes
-rwxr-xr-xsubx/apps/factorialbin13062 -> 13066 bytes
-rwxr-xr-xsubx/apps/handlebin13855 -> 13859 bytes
-rwxr-xr-xsubx/apps/hexbin17123 -> 17127 bytes
-rw-r--r--subx/apps/pack.subx231
7 files changed, 231 insertions, 2 deletions
diff --git a/subx/064print-byte.subx b/subx/064print-byte.subx
index dacc78d9..3d362fa0 100644
--- a/subx/064print-byte.subx
+++ b/subx/064print-byte.subx
@@ -21,6 +21,7 @@ print-byte:  # f : (address buffered-file), n : int -> <void>
     # AL = convert upper nibble to hex
     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
     c1/shift    5/subop/logic-right 3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm8            # shift EAX right by 4 bits, while padding zeroes
+    25/and-EAX  0xf/imm32
     # . AL = to-hex-char(AL)
     e8/call  to-hex-char/disp32
     # write-byte(f, AL)
@@ -46,7 +47,6 @@ print-byte:  # f : (address buffered-file), n : int -> <void>
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 $print-byte:end:
     # . restore registers
-    59/pop-to-ECX
     58/pop-to-EAX
     # . epilog
     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
diff --git a/subx/apps/crenshaw2-1 b/subx/apps/crenshaw2-1
index 8010cdac..bcf1415c 100755
--- a/subx/apps/crenshaw2-1
+++ b/subx/apps/crenshaw2-1
Binary files differdiff --git a/subx/apps/crenshaw2-1b b/subx/apps/crenshaw2-1b
index 3cdb555e..29fcdc94 100755
--- a/subx/apps/crenshaw2-1b
+++ b/subx/apps/crenshaw2-1b
Binary files differdiff --git a/subx/apps/factorial b/subx/apps/factorial
index 4489b6ad..5062a5a0 100755
--- a/subx/apps/factorial
+++ b/subx/apps/factorial
Binary files differdiff --git a/subx/apps/handle b/subx/apps/handle
index 65b94caa..929e5c5a 100755
--- a/subx/apps/handle
+++ b/subx/apps/handle
Binary files differdiff --git a/subx/apps/hex b/subx/apps/hex
index 66aa0dce..d854606e 100755
--- a/subx/apps/hex
+++ b/subx/apps/hex
Binary files differdiff --git a/subx/apps/pack.subx b/subx/apps/pack.subx
index 5cd693b8..a4ad75e9 100644
--- a/subx/apps/pack.subx
+++ b/subx/apps/pack.subx
@@ -20,6 +20,11 @@
 # . 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
 
+    # for debugging: run a single test
+#?     e8/call test-emit-hex-zero-pad/disp32
+#?     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
+#?     eb/jump  $main:end/disp8
+
 # main: run tests if necessary, convert stdin if not
     # . prolog
     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
@@ -337,6 +342,230 @@ test-next-word-returns-whole-comment:
     5d/pop-to-EBP
     c3/return
 
-== data
+# print 'n' in hex in 'width' bytes in lower-endian order, with a space after every byte
+emit-hex:  # out : (address buffered-file), n : int, width : int
+    # . prolog
+    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
+    57/push-EDI
+    # EDI = out
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
+    # EBX = n
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
+    # EDX = width
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           2/r32/EDX   0x10/disp8      .                 # copy *(EBP+16) to EDX
+    # var curr/ECX = 0
+    31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
+$emit-hex:loop:
+    # if (curr >= width) break
+    39/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # compare ECX and EDX
+    7d/jump-if-greater-or-equal  $emit-hex:end/disp8
+    # print-byte(out, EBX)
+    # . . push args
+    53/push-EBX
+    57/push-EDI
+    # . . call
+    e8/call  print-byte/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # write-byte(out, ' ')
+    # . . push args
+    68/push  0x20/imm32/space
+    57/push-EDI
+    # . . call
+    e8/call  write-byte/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # EBX = EBX >> 8
+    c1/shift    5/subop/logic-right 3/mod/direct    3/rm32/EBX    .           .             .           .           .               8/imm8            # shift EBX right by 8 bits, while padding zeroes
+    # ++curr
+    41/increment-ECX
+    eb/jump  $emit-hex:loop/disp8
+$emit-hex:end:
+    # . restore registers
+    5f/pop-to-EDI
+    5b/pop-to-EBX
+    5a/pop-to-EAX
+    59/pop-to-ECX
+    58/pop-to-EAX
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+test-emit-hex-single-byte:
+    # setup
+    # . clear-stream(_test-stream)
+    # . . push args
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . clear-stream(_test-buffered-file+4)
+    # . . push args
+    b8/copy-to-EAX  _test-buffered-file/imm32
+    05/add-to-EAX  4/imm32
+    50/push-EAX
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # emit-hex(_test-buffered-file, 0xab, 1)
+    # . . push args
+    68/push  1/imm32
+    68/push  0xab/imm32
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  emit-hex/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # flush(_test-buffered-file)
+    # . . push args
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  flush/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check-ints-equal(*_test-stream->data, 'ab ', msg)
+    # . . push args
+    68/push  "F - test-emit-hex-single-byte"/imm32
+    68/push  0x206261/imm32
+    # . . push *_test-stream->data
+    b8/copy-to-EAX  _test-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
+test-emit-hex-multiple-byte:
+    # setup
+    # . clear-stream(_test-stream)
+    # . . push args
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . clear-stream(_test-buffered-file+4)
+    # . . push args
+    b8/copy-to-EAX  _test-buffered-file/imm32
+    05/add-to-EAX  4/imm32
+    50/push-EAX
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # emit-hex(_test-buffered-file, 0x1234, 2)
+    # . . push args
+    68/push  2/imm32
+    68/push  0x1234/imm32
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  emit-hex/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # flush(_test-buffered-file)
+    # . . push args
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  flush/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check(_test-stream->data == '34 12 ')
+    # . check-ints-equal(_test-stream->data[0..3], '34 1', msg)
+    # . . push args
+    68/push  "F - test-emit-hex-multiple-byte/1"/imm32
+    68/push  0x31203433/imm32
+    # . . push *_test-stream->data
+    b8/copy-to-EAX  _test-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . check-ints-equal(_test-stream->data[4..7], '2 ', msg)
+    # . . push args
+    68/push  "F - test-emit-hex-multiple-byte/2"/imm32
+    68/push  0x2032/imm32
+    # . . push *_test-stream->data
+    b8/copy-to-EAX  _test-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0x10/disp8      .                 # push *(EAX+16)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+    # . end
+    c3/return
+
+test-emit-hex-zero-pad:
+    # setup
+    # . clear-stream(_test-stream)
+    # . . push args
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # . clear-stream(_test-buffered-file+4)
+    # . . push args
+    b8/copy-to-EAX  _test-buffered-file/imm32
+    05/add-to-EAX  4/imm32
+    50/push-EAX
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # emit-hex(_test-buffered-file, 0xab, 2)
+    # . . push args
+    68/push  2/imm32
+    68/push  0xab/imm32
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  emit-hex/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # flush(_test-buffered-file)
+    # . . push args
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  flush/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # check(_test-stream->data == '00 ab')
+    # . check-ints-equal(*_test-stream->data, 'ab 0', msg)
+    # . . push args
+    68/push  "F - test-emit-hex-zero-pad/1"/imm32
+    68/push  0x30206261/imm32
+    # . . push *_test-stream->data
+    b8/copy-to-EAX  _test-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . check-ints-equal(*_test-stream->data[1], '0 ', msg)
+    # . . push args
+    68/push  "F - test-emit-hex-zero-pad/2"/imm32
+    68/push  0x2030/imm32
+    # . . push *_test-stream->data
+    b8/copy-to-EAX  _test-stream/imm32
+    ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0x10/disp8      .                 # push *(EAX+16)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
 
 # . . vim:nowrap:textwidth=0