about summary refs log tree commit diff stats
path: root/310copy-bytes.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-09-26 13:41:23 -0700
committerKartik Agaram <vc@akkartik.com>2020-09-26 13:41:23 -0700
commite0bceffe08e06a30e771efe986f1f4f7c717dc95 (patch)
treee485751ca2e141a7ee777434cc6de2407af1f344 /310copy-bytes.subx
parentfca30f0f86f848c105c0dd048815f959e850eb1b (diff)
downloadmu-e0bceffe08e06a30e771efe986f1f4f7c717dc95.tar.gz
6860
Snapshot: tile currently segfaulting. I need to back up and make it easier
to debug.
Diffstat (limited to '310copy-bytes.subx')
-rw-r--r--310copy-bytes.subx58
1 files changed, 58 insertions, 0 deletions
diff --git a/310copy-bytes.subx b/310copy-bytes.subx
index f33388a9..7a90495b 100644
--- a/310copy-bytes.subx
+++ b/310copy-bytes.subx
@@ -55,3 +55,61 @@ $copy-bytes:end:
     89/<- %esp 5/r32/ebp
     5d/pop-to-ebp
     c3/return
+
+stream-to-string:  # in: (addr stream _), out: (addr handle array _)
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+    52/push-edx
+    56/push-esi
+    # esi = s
+    8b/-> *(ebp+8) 6/r32/esi
+    # var len/ecx: int = s->write - s->read
+    8b/-> *esi 1/r32/ecx
+    2b/subtract *(esi+4) 1/r32/ecx
+    # allocate
+    (allocate-array Heap %ecx *(ebp+0xc))
+    # var in/edx: (addr byte) = s->data + s->read
+    8b/-> *(esi+4) 2/r32/edx
+    8d/copy-address *(esi+edx+0xc) 2/r32/edx
+    # var dest/eax: (addr byte) = data for out
+    8b/-> *(ebp+0xc) 0/r32/eax
+    (lookup *eax *(eax+4))  # => eax
+    8d/copy-address *(eax+4) 0/r32/eax
+    #
+    (copy-bytes %edx %eax %ecx)
+$stream-to-string:end:
+    # . restore registers
+    5e/pop-to-esi
+    5a/pop-to-edx
+    59/pop-to-ecx
+    58/pop-to-eax
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return
+
+test-stream-to-string:
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # setup
+    (clear-stream _test-input-stream)
+    (write _test-input-stream "abc")
+    # skip something
+    (read-byte _test-input-stream)  # => eax
+    # var out/ecx: (handle array byte)
+    68/push 0/imm32
+    68/push 0/imm32
+    89/<- %ecx 4/r32/esp
+    #
+    (stream-to-string _test-input-stream %ecx)
+    (lookup *ecx *(ecx+4))  # => eax
+    (check-strings-equal %eax "bc")
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return