https://github.com/akkartik/mu/blob/master/subx/057write.subx

      1 # write: like _write, but also support in-memory streams in addition to file
      2 # descriptors.
      3 #
      4 # Our first dependency-injected and testable primitive. We can pass it either
      5 # a file descriptor or an address to a stream. If a file descriptor is passed
      6 # in, we _write to it using the right syscall. If a 'fake file descriptor' or
      7 # stream is passed in, we append to the stream. This lets us redirect output
      8 # in tests and check it later.
      9 #
     10 # We assume our data segment will never begin at an address shorter than
     11 # 0x08000000, so any smaller arguments are assumed to be real file descriptors.
     12 #
     13 # A stream looks like this:
     14 #   read: int  # index at which to read next
     15 #   write: int  # index at which writes go
     16 #   data: (array byte)  # prefixed by length as usual
     17
     18 == code
     19 #   instruction                     effective address                                                   register    displacement    immediate
     20 # . op          subop               mod             rm32          base        index         scale       r32
     21 # . 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
     22
     23 # TODO: come up with a way to signal when a write to disk fails
     24 write:  # f : fd or (address stream), s : (address array byte) -> <void>
     25     # . prolog
     26     55/push-EBP
     27     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
     28     # if (f < 0x08000000) _write(f, s) and return  # f can't be a user-mode address, so treat it as a kernel file descriptor
     29     81          7/subop/compare     1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         0x08000000/imm32  # compare *(EBP+8)
     30     7d/jump-if-greater-or-equal  $write:fake/disp8
     31     # . . push args
     32     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
     33     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
     34     # . . call
     35     e8/call  _write/disp32
     36     # . . discard args
     37     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
     38     eb/jump  $write:end/disp8
     39 $write:fake:
     40     # otherwise, treat 'f' as a stream to append to
     41     # . save registers
     42     50/push-EAX
     43     51/push-ECX
     44     52/push-EDX
     45     53/push-EBX
     46     # ECX = f
     47     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
     48     # EDX = f->write
     49     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy *ECX to EDX
     50     # EBX = f->length
     51     8b/copy                         1/mod/*+disp8   1/rm32/ECX    .           .             .           3/r32/EBX   8/disp8         .                 # copy *(ECX+8) to EBX
     52     # EAX = _append-3(&f->data[f->write], &f->data[f->length], s)
     53     # . . push s
     54     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
     55     # . . push &f->data[f->length]
     56     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  3/index/EBX   .           3/r32/EBX   0xc/disp8       .                 # copy ECX+EBX+12 to EBX
     57     53/push-EBX
     58     # . . push &f->data[f->write]
     59     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    1/base/ECX  2/index/EDX   .           3/r32/EBX   0xc/disp8       .                 # copy ECX+EDX+12 to EBX
     60     53/push-EBX
     61     # . . call
     62     e8/call  _append-3/disp32
     63     # . . discard args
     64     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
     65     # f->write += EAX
     66     01/add                          0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # add EAX to *ECX
     67     # . restore registers
     68     5b/pop-to-EBX
     69     5a/pop-to-EDX
     70     59/pop-to-ECX
     71     58/pop-to-EAX
     72 $write:end:
     73     # . epilog
     74     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
     75     5d/pop-to-EBP
     76     c3/return
     77
     78 test-write-single:
     79     # clear-stream(_test-stream)
     80     # . . push args
     81     68/push  _test-stream/imm32
     82     # . . call
     83     e8/call  clear-stream/disp32
     84     # . . discard args
     85     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
     86     # write(_test-stream, "Ab")
     87     # . . push args
     88     68/push  "Ab"/imm32
     89     68/push  _test-stream/imm32
     90     # . . call
     91     e8/call  write/disp32
     92     # . . discard args
     93     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
     94     # check-ints-equal(*_test-stream->data, 41/A 62/b 00 00, msg)
     95     # . . push args
     96     68/push  "F - test-write-single"/imm32
     97     68/push  0x006241/imm32/Ab
     98     # . . push *_test-stream->data
     99     b8/copy-to-EAX  _test-stream/imm32
    100     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
    101     # . . call
    102     e8/call  check-ints-equal/disp32
    103     # . . discard args
    104     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    105     # end
    106     c3/return
    107
    108 test-write-appends:
    109     # clear-stream(_test-stream)
    110     # . . push args
    111     68/push  _test-stream/imm32
    112     # . . call
    113     e8/call  clear-stream/disp32
    114     # . . discard args
    115     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
    116     # write(_test-stream, "C")
    117     # . . push args
    118     68/push  "C"/imm32
    119     68/push  _test-stream/imm32
    120     # . . call
    121     e8/call  write/disp32
    122     # . . discard args
    123     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    124     # write(_test-stream, "D")
    125     # . . push args
    126     68/push  "D"/imm32
    127     68/push  _test-stream/imm32
    128     # . . call
    129     e8/call  write/disp32
    130     # . . discard args
    131     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    132     # check-ints-equal(*_test-stream->data, 43/C 44/D 00 00, msg)
    133     # . . push args
    134     68/push  "F - test-write-appends"/imm32
    135     68/push  0x00004443/imm32/C-D
    136     # . . push *_test-stream->data
    137     b8/copy-to-EAX  _test-stream/imm32
    138     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
    139     # . . call
    140     e8/call  check-ints-equal/disp32
    141     # . . discard args
    142     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    143     # end
    144     c3/return
    145
    146 == data
    147
    148 _test-stream:
    149     # current write index
    150     0/imm32
    151     # current read index
    152     0/imm32
    153     # length
    154     8/imm32
    155     # data
    156     00 00 00 00 00 00 00 00  # 8 bytes
    157
    158 # . . vim:nowrap:textwidth=0