about summary refs log tree commit diff stats
path: root/baremetal/310copy-bytes.subx
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-02-11 19:34:55 -0800
committerKartik K. Agaram <vc@akkartik.com>2021-02-11 19:34:55 -0800
commitd1bcd1421f7a0e87105499a738bae3b3a5c92de9 (patch)
tree550a6070a56dac6ed41f0a8161ee19976ee476c0 /baremetal/310copy-bytes.subx
parent884c92984fd1341eed3725263434534dd915a795 (diff)
downloadmu-d1bcd1421f7a0e87105499a738bae3b3a5c92de9.tar.gz
7723 - baremetal: rendering string values
Diffstat (limited to 'baremetal/310copy-bytes.subx')
-rw-r--r--baremetal/310copy-bytes.subx99
1 files changed, 99 insertions, 0 deletions
diff --git a/baremetal/310copy-bytes.subx b/baremetal/310copy-bytes.subx
index 26fc807e..2586a53f 100644
--- a/baremetal/310copy-bytes.subx
+++ b/baremetal/310copy-bytes.subx
@@ -56,3 +56,102 @@ $copy-bytes:end:
     89/<- %esp 5/r32/ebp
     5d/pop-to-ebp
     c3/return
+
+stream-to-array:  # 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-array: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-array:
+    # . 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-array _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
+
+# like stream-to-array but ignore surrounding quotes
+# we might do other stuff here later
+unquote-stream-to-array:  # 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 - 2
+    8b/-> *esi 1/r32/ecx
+    2b/subtract *(esi+4) 1/r32/ecx
+    81 7/subop/compare %ecx 2/imm32
+    7c/jump-if-< $unquote-stream-to-array:end/disp8
+    81 5/subop/subtract %ecx 2/imm32
+    # allocate
+    (allocate-array Heap %ecx *(ebp+0xc))
+    # var in/edx: (addr byte) = s->data + s->read + 1
+    8b/-> *(esi+4) 2/r32/edx
+    8d/copy-address *(esi+edx+0xd) 2/r32/edx  # Stream-data + 1
+    # 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)
+$unquote-stream-to-array: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