https://github.com/akkartik/mu/blob/main/linux/113write-stream.subx

      1 # write-stream: like write, but write streams rather than strings
      2
      3 == code
      4 #   instruction                     effective address                                                   register    displacement    immediate
      5 # . op          subop               mod             rm32          base        index         scale       r32
      6 # . 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
      7
      8 #? Entry:  # manual test
      9 #?     # write-stream(stdout, _test-stream2)
     10 #?     68/push  _test-stream2/imm32
     11 #?     68/push  1/imm32/stdout
     12 #?     e8/call write-stream/disp32
     13 #?     # syscall(exit, Num-test-failures)
     14 #?     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
     15 #?     e8/call  syscall_exit/disp32
     16
     17 write-stream:  # f: fd or (addr stream byte), s: (addr stream byte)
     18     # . prologue
     19     55/push-ebp
     20     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
     21     # if (f < 0x08000000) _write-stream(f, s), return  # f can't be a user-mode address, so treat it as a kernel file descriptor
     22     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(ebp+8)
     23     73/jump-if-addr>=  $write-stream:fake/disp8
     24     # . . push args
     25     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
     26     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
     27     # . . call
     28     e8/call  _write-stream/disp32
     29     # . . discard args
     30     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
     31     eb/jump  $write-stream:end/disp8
     32 $write-stream:fake:
     33     # otherwise, treat 'f' as a stream to append to
     34     # . save registers
     35     50/push-eax
     36     56/push-esi
     37     57/push-edi
     38     # edi = f
     39     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
     40     # esi = s
     41     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
     42     # eax = _append-4(&f->data[f->write], &f->data[f->size], &s->data[s->read], &s->data[s->write])
     43     # . . push &s->data[s->write]
     44     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           0/r32/eax   .               .                 # copy *esi to eax
     45     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
     46     50/push-eax
     47     # . . push &s->data[s->read]
     48     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy *(esi+4) to eax
     49     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy esi+eax+12 to eax
     50     50/push-eax
     51     # . . push &f->data[f->size]
     52     8b/copy                         1/mod/*+disp8   7/rm32/edi    .           .             .           0/r32/eax   8/disp8         .                 # copy *(edi+8) to eax
     53     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
     54     50/push-eax
     55     # . . push &f->data[f->write]
     56     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy *edi to eax
     57     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/edi  0/index/eax   .           0/r32/eax   0xc/disp8       .                 # copy edi+eax+12 to eax
     58     50/push-eax
     59     # . . call
     60     e8/call  _append-4/disp32
     61     # . . discard args
     62     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
     63     # f->write += eax
     64     01/add                          0/mod/indirect  7/rm32/edi    .           .             .           0/r32/eax   .               .                 # add eax to *edi
     65     # s->read += eax
     66     01/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # add eax to *(esi+4)
     67     # . restore registers
     68     5f/pop-to-edi
     69     5e/pop-to-esi
     70     58/pop-to-eax
     71 $write-stream:end:
     72     # . epilogue
     73     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
     74     5d/pop-to-ebp
     75     c3/return
     76
     77 _write-stream:  # fd: int, s: (addr stream byte)
     78     # . prologue
     79     55/push-ebp
     80     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
     81     # . save registers
     82     50/push-eax
     83     51/push-ecx
     84     52/push-edx
     85     53/push-ebx
     86     56/push-esi
     87     57/push-edi
     88     # esi = s
     89     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   0xc/disp8       .                 # copy *(ebp+12) to esi
     90     # edi = s->read
     91     8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           7/r32/edi   4/disp8         .                 # copy *(esi+4) to edi
     92     # edx = s->write
     93     8b/copy                         0/mod/indirect  6/rm32/esi    .           .             .           2/r32/edx   .               .                 # copy *esi to edx
     94     # syscall(write, fd, &s->data[s->read], s->write - s->read)
     95     # . . fd: ebx
     96     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           3/r32/ebx   8/disp8         .                 # copy *(ebp+8) to ebx
     97     # . . data: ecx = &s->data[s->read]
     98     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/esi  7/index/edi   .           1/r32/ecx   0xc/disp8       .                 # copy esi+edi+12 to ecx
     99     # . . size: edx = s->write - s->read
    100     29/subtract                     3/mod/direct    2/rm32/edx    .           .             .           7/r32/edi   .               .                 # subtract edi from edx
    101     # . . syscall
    102     e8/call  syscall_write/disp32
    103     # if (eax < 0) abort
    104     3d/compare-eax-with  0/imm32
    105     0f 8c/jump-if-<  $_write-stream:abort/disp32
    106     # s->read += eax
    107     01/add                          1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # add eax to *(esi+4)
    108     # . restore registers
    109     5f/pop-to-edi
    110     5e/pop-to-esi
    111     5b/pop-to-ebx
    112     5a/pop-to-edx
    113     59/pop-to-ecx
    114     58/pop-to-eax
    115     # . epilogue
    116     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    117     5d/pop-to-ebp
    118     c3/return
    119
    120 $_write-stream:abort:
    121     # . _write(2/stderr, error)
    122     # . . push args
    123     68/push  "_write-stream: failed to write to file\n"/imm32
    124     68/push  2/imm32/stderr
    125     # . . call
    126     e8/call  _write/disp32
    127     # . . discard args
    128     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    129     # . syscall(exit, 1)
    130     bb/copy-to-ebx  1/imm32
    131     e8/call  syscall_exit/disp32
    132     # never gets here
    133
    134 test-write-stream-single:
    135     # setup
    136     # . clear-stream(_test-stream)
    137     # . . push args
    138     68/push  _test-stream/imm32
    139     # . . call
    140     e8/call  clear-stream/disp32
    141     # . . discard args
    142     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    143     # . clear-stream(_test-stream2)
    144     # . . push args
    145     68/push  _test-stream2/imm32
    146     # . . call
    147     e8/call  clear-stream/disp32
    148     # . . discard args
    149     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    150     # . write(_test-stream2, "Ab")
    151     # . . push args
    152     68/push  "Ab"/imm32
    153     68/push  _test-stream2/imm32
    154     # . . call
    155     e8/call  write/disp32
    156     # . . discard args
    157     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    158     # write-stream(_test-stream, _test-stream2)
    159     # . . push args
    160     68/push  _test-stream2/imm32
    161     68/push  _test-stream/imm32
    162     # . . call
    163     e8/call  write-stream/disp32
    164     # . . discard args
    165     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    166     # check-stream-equal(_test-stream, "Ab", msg)
    167     # . . push args
    168     68/push  "F - test-write-stream-single"/imm32
    169     68/push  "Ab"/imm32
    170     68/push  _test-stream/imm32
    171     # . . call
    172     e8/call  check-stream-equal/disp32
    173     # . . discard args
    174     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    175     # . end
    176     c3/return
    177
    178 test-write-stream-appends:
    179     # setup
    180     # . clear-stream(_test-stream)
    181     # . . push args
    182     68/push  _test-stream/imm32
    183     # . . call
    184     e8/call  clear-stream/disp32
    185     # . . discard args
    186     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    187     # . clear-stream(_test-stream2)
    188     # . . push args
    189     68/push  _test-stream2/imm32
    190     # . . call
    191     e8/call  clear-stream/disp32
    192     # . . discard args
    193     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    194     # . write(_test-stream2, "C")
    195     # . . push args
    196     68/push  "C"/imm32
    197     68/push  _test-stream2/imm32
    198     # . . call
    199     e8/call  write/disp32
    200     # . . discard args
    201     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    202     # first write
    203     # . write-stream(_test-stream, _test-stream2)
    204     # . . push args
    205     68/push  _test-stream2/imm32
    206     68/push  _test-stream/imm32
    207     # . . call
    208     e8/call  write-stream/disp32
    209     # . . discard args
    210     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    211     # second write
    212     # . write(_test-stream2, "D")
    213     # . . push args
    214     68/push  "D"/imm32
    215     68/push  _test-stream2/imm32
    216     # . . call
    217     e8/call  write/disp32
    218     # . . discard args
    219     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    220     # . write-stream(_test-stream, _test-stream2)
    221     # . . push args
    222     68/push  _test-stream2/imm32
    223     68/push  _test-stream/imm32
    224     # . . call
    225     e8/call  write-stream/disp32
    226     # . . discard args
    227     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    228     # check-stream-equal(_test-stream, "CD", msg)
    229     # . . push args
    230     68/push  "F - test-write-stream-appends"/imm32
    231     68/push  "CD"/imm32
    232     68/push  _test-stream/imm32
    233     # . . call
    234     e8/call  check-stream-equal/disp32
    235     # . . discard args
    236     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    237     # . end
    238     c3/return
    239
    240 == data
    241
    242 _test-stream2:  # (stream byte)
    243     # current write index
    244     4/imm32
    245     # current read index
    246     1/imm32
    247     # size
    248     8/imm32
    249     # data
    250     41/A 42/B 43/C 44/D 00 00 00 00  # 8 bytes
    251
    252 # . . vim:nowrap:textwidth=0