about summary refs log tree commit diff stats
path: root/subx/apps
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-12-06 11:01:23 -0800
committerKartik Agaram <vc@akkartik.com>2018-12-06 11:01:23 -0800
commite9909be37441c463404098d53b9e16c3724243bf (patch)
treeb0c9f19f8a04c6f0a7ae32da40ef6a0b47e8f6c2 /subx/apps
parenta966881b6ef57de52cbf3001ed71bb66f0c77c2a (diff)
downloadmu-e9909be37441c463404098d53b9e16c3724243bf.tar.gz
4849
Diffstat (limited to 'subx/apps')
-rw-r--r--subx/apps/hexbin9456 -> 9733 bytes
-rw-r--r--subx/apps/hex.subx91
2 files changed, 90 insertions, 1 deletions
diff --git a/subx/apps/hex b/subx/apps/hex
index d8d27ab4..299151cc 100644
--- a/subx/apps/hex
+++ b/subx/apps/hex
Binary files differdiff --git a/subx/apps/hex.subx b/subx/apps/hex.subx
index c5d66900..4bc56fcf 100644
--- a/subx/apps/hex.subx
+++ b/subx/apps/hex.subx
@@ -42,6 +42,7 @@
     # . run-tests()
 #?     e8/call test-hex-below-0/disp32
 #?     e8/call test-scan-next-byte/disp32
+#?     e8/call test-scan-next-byte-handles-eof/disp32
 #?     e8/call test-scan-next-byte-skips-comment/disp32
     e8/call  run-tests/disp32
     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
@@ -118,11 +119,11 @@ scan-next-byte:  # in : (address buffered-file), err : (address buffered-file),
     # pseudocode:
     #   repeatedly
     #     EAX = read-byte(in)
+    #     if EAX == 0xffffffff return EAX
     #     if is-hex-lowercase-byte?(EAX) return EAX
     #     if EAX == 0x20 continue
     #     if EAX == '#' skip-until-newline(in)
     #     else error-byte(ed, err, "unexpected byte: " EAX)
-    #   return 0xffffffff
     #
     # . prolog
     55/push-EBP
@@ -136,6 +137,9 @@ $scan-next-byte:loop:
     e8/call  read-byte/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # if (EAX == 0xffffffff) return EAX
+    3d/compare-with-EAX  0xffffffff/imm32
+    74/jump-if-equal  $scan-next-byte:end/disp8
     # if is-hex-lowercase-byte?(EAX) return EAX
     # . save EAX for now
     50/push-EAX
@@ -796,6 +800,91 @@ $test-scan-next-byte-reads-final-byte:end:
     5d/pop-to-EBP
     c3/return
 
+test-scan-next-byte-handles-eof:
+    # - check that the right sentinel value is returned when there's no data remaining to be read
+    # This test uses exit-descriptors. Use EBP for setting up local variables.
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # clear all streams
+    # . 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
+    # . clear-stream(_test-error-stream)
+    # . . push args
+    68/push  _test-error-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
+    # leave '_test-stream' empty
+    # initialize exit-descriptor 'ed' for the call to 'scan-next-byte' below
+    # . var ed/ECX : exit-descriptor
+    81          5/subop/subtract    3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # subtract from ESP
+    8d/copy-address                 0/mod/indirect  4/rm32/sib    4/base/ESP  4/index/none  .           1/r32/ECX   .               .                 # copy ESP to ECX
+    # . tailor-exit-descriptor(ed, 12)
+    # . . push args
+    68/push  0xc/imm32/nbytes-of-args-for-scan-next-byte
+    51/push-ECX/ed
+    # . . call
+    e8/call  tailor-exit-descriptor/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # EAX = scan-next-byte(_test-buffered-file, _test-error-stream, ed)
+    # . . push args
+    51/push-ECX/ed
+    68/push  _test-error-stream/imm32
+    68/push  _test-buffered-file/imm32
+    # . . call
+    e8/call  scan-next-byte/disp32
+    # registers except ESP may be clobbered at this point
+    # pop args to scan-next-bytes
+    # . . discard first 2 args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . . restore ed
+    59/pop-to-ECX
+    # check that scan-next-byte didn't abort
+    # . check-ints-equal(ed->value, 0, msg)
+    # . . push args
+    68/push  "F - test-scan-next-byte-handles-eof: unexpected abort"/imm32
+    68/push  0/imm32
+    # . . push ed->value
+    ff          6/subop/push        1/mod/*+disp8   1/rm32/ECX    .           .             .           .           4/disp8         .                 # push *(ECX+4)
+    # . . call
+    e8/call  check-ints-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # return if abort
+    81          7/subop/compare     1/mod/*+disp8   1/rm32/ECX    .           .             .           .           4/disp8         0/imm32           # compare *(ECX+4)
+    75/jump-if-not-equal  $test-scan-next-byte-handles-eof:end/disp8
+    # check-ints-equal(EAX, 0xffffffff/eof, msg)
+    # . . push args
+    68/push  "F - test-scan-next-byte-handles-eof"/imm32
+    68/push  0xffffffff/imm32/eof
+    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
+$test-scan-next-byte-handles-eof:end:
+    # . epilog
+    # don't restore ESP from EBP; manually reclaim locals
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    5d/pop-to-EBP
+    c3/return
+
 is-hex-lowercase-byte?:  # c : byte -> bool/EAX
     # . prolog
     55/push-EBP