about summary refs log tree commit diff stats
path: root/subx/056write.subx
diff options
context:
space:
mode:
Diffstat (limited to 'subx/056write.subx')
-rw-r--r--subx/056write.subx107
1 files changed, 27 insertions, 80 deletions
diff --git a/subx/056write.subx b/subx/056write.subx
index f8e32fb2..c9303b56 100644
--- a/subx/056write.subx
+++ b/subx/056write.subx
@@ -33,7 +33,6 @@ Test-ostream:
 # 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
 
 # main:  (manual test if this is the last file loaded)
-#?   e8/call  test-write-appends/disp32
   e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
   # syscall(exit, Num-test-failures)
   8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           1/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
@@ -44,12 +43,6 @@ write:  # f : fd or (address stream), s : (address array byte) -> <void>
   # 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
-  56/push-ESI
   # if (f < 0x08000000) _write(f, s), return  # f can't be a user-mode address, so treat it as a kernel file descriptor
   81          7/subop/compare     1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           8/disp8         0x08000000/imm32  # compare *(EBP+8)
   7f/jump-if-greater  $write:else/disp8
@@ -63,84 +56,38 @@ write:  # f : fd or (address stream), s : (address array byte) -> <void>
   eb/jump  $write:end/disp8
 $write:else:
   # otherwise, treat 'f' as a stream to append to
-  #
-  # pseudocode:
-  #   destend = &f.data[t.length]
-  #   oldw = f.write
-  #   if s.length == 0 return
-  #   f.write += s.length
-  #   dest = &f.data[oldw]
-  #   srcend = &s.data[s.length]
-  #   src = &s.data[0]
-  #   while true:
-  #     if src >= srcend break
-  #     if dest >= destend break  # for now silently ignore filled up ostream buffer
-  #     *dest = *src
-  #     ++src
-  #     ++dest
-  #
-  # key registers to set up for the loop:
-  #   EAX/dest, ECX/destend, EBX/src, ESI/srcend
-  # we save EDX for byte operations (has to be one of the first 4 registers)
-  #
-  # register setup before the loop:
-  #   EAX = *(EBP+8)    # f
-  #   EBX = *(EBP+12)   # s
-  #   ECX = *(EAX+4)    # f.length
-  #   ECX = EAX+8+ECX  # destend = &f.data[f.length]
-  #   ESI = *EAX        # oldw = f.write
-  #   EDX = *EBX        # s.length
-  #   *EAX = *EAX + EDX # update f.write  (allowed to go past f.length)
-  #                     # do this here just because it's convenient
-  #   EAX = EAX+8+ESI  # dest = &f.data[oldw]
-  #   ESI = EBX+4+EDX   # srcend = &s.data[s.length]
-  #   EBX = EBX+4       # src = &s.data[0]
-  #
-  # EAX = f
-  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
-  # EBX = s
-  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
-  # ECX = f.length
-  8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy *(EAX+4) to ECX
-  # ECX/destend = &f.data[f.length]
-  8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   8/disp8         .                 # copy EAX+ECX+8 to ECX
-  # ESI/oldw = f.write
-  8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           6/r32/ESI   .               .                 # copy *EAX to ESI
-  # EDX = s.length
-  8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # copy *EBX to EDX
-  # if EDX == 0 return
-  81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               0/imm32           # compare EDX
-  74/jump-if-equal  $write:end/disp8
-  # f.write += s.length
-  01/add                          0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/EDX   .               .                 # add EDX to *EAX
-  # EAX/dest = &f.data[oldw]
-  8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  6/index/ESI   .           0/r32/EAX   8/disp8         .                 # copy EAX+ESI+8 to EAX
-  # ESI/srcend = &s.data[s.length]
-  8d/copy-address                 1/mod/*+disp8   4/rm32/sib    3/base/EBX  2/index/EDX   .           6/r32/ESI   4/disp8         .                 # copy EBX+EDX+4 to ESI
-  # EBX/src = &s.data[0]
-  81          0/subop/add         3/mod/direct    3/rm32/EBX    .           .             .           .           .               4/imm32           # add to EBX
-  # while (true)
-$write:loop:
-  # if EBX/src >= ESI/srcend break
-  39/compare                      3/mod/direct    3/rm32/EBX    .           .             .           6/r32/ESI   .               .                 # compare EBX with ESI
-  7d/jump-if-greater-or-equal  $write:end/disp8
-  # if EAX/dest >= ECX/destend break  (for now silently ignore filled up ostream buffer)
-  39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
-  7d/jump-if-greater-or-equal  $write:end/disp8
-  # copy one byte
-  8a/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/DL    .               .                 # copy byte at *EBX to DL
-  88/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/DL    .               .                 # copy byte at DL to *EAX
-  # updates
-  40/increment-EAX
-  43/increment-EBX
-  eb/jump  $write:loop/disp8
-$write:end:
+  # save registers
+  50/push-EAX
+  51/push-ECX
+  52/push-EDX
+  53/push-EBX
+  # ECX = f
+  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
+  # EDX = f.write
+  8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy *ECX to EDX
+  # EBX = f.length
+  8b/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           3/r32/EBX   4/disp8         .                 # copy *(ECX+4) to EBX
+  # EAX = _append(&f.data[f.write], &f.data[f.length], s)
+    # push s
+  ff          6/subop/push        1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           .           0xc/disp8       .                 # push *(EBP+12)
+    # push &f.data[f.length]
+  8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  3/index/EBX   .           3/r32/EBX   8/disp8         .                 # copy ECX+EBX+8 to EBX
+  53/push-EBX
+    # push &f.data[f.write]
+  8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  2/index/EDX   .           3/r32/EBX   8/disp8         .                 # copy ECX+EBX+8 to EBX
+  53/push-EBX
+    # call
+  e8/call  _append/disp32
+    # discard args
+  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+  # f.write += EAX
+  01/add                          0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # add EAX to *ECX
   # restore registers
-  5e/pop-to-ESI
   5b/pop-to-EBX
   5a/pop-to-EDX
   59/pop-to-ECX
   58/pop-to-EAX
+$write:end:
   # epilog
   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
   5d/pop-to-EBP