about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--baremetal/310copy-bytes.subx58
1 files changed, 58 insertions, 0 deletions
diff --git a/baremetal/310copy-bytes.subx b/baremetal/310copy-bytes.subx
new file mode 100644
index 00000000..26fc807e
--- /dev/null
+++ b/baremetal/310copy-bytes.subx
@@ -0,0 +1,58 @@
+# Some helpers for copying non-overlapping regions of memory.
+# Really only intended to be called from code generated by mu.subx.
+
+== code
+
+copy-bytes:  # src: (addr byte), dest: (addr byte), size: int
+    # pseudocode:
+    #   curr-src/esi = src
+    #   curr-dest/edi = dest
+    #   i/ecx = 0
+    #   while true
+    #     if (i >= size) break
+    #     *curr-dest = *curr-src
+    #     ++curr-src
+    #     ++curr-dest
+    #     ++i
+    #
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+    52/push-edx
+    56/push-esi
+    57/push-edi
+    # curr-src/esi = src
+    8b/-> *(ebp+8) 6/r32/esi
+    # curr-dest/edi = dest
+    8b/-> *(ebp+0xc) 7/r32/edi
+    # var i/ecx: int = 0
+    b9/copy-to-ecx 0/imm32
+    # edx = size
+    8b/-> *(ebp+0x10) 2/r32/edx
+    {
+      # if (i >= size) break
+      39/compare %ecx 2/r32/edx
+      7d/jump-if->=  break/disp8
+      # *curr-dest = *curr-src
+      8a/byte-> *esi 0/r32/AL
+      88/byte<- *edi 0/r32/AL
+      # update
+      46/increment-esi
+      47/increment-edi
+      41/increment-ecx
+      eb/jump loop/disp8
+    }
+$copy-bytes:end:
+    # . restore registers
+    5f/pop-to-edi
+    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