https://github.com/akkartik/mu/blob/master/077slurp.subx
  1 == code
  2 #   instruction                     effective address                                                   register    displacement    immediate
  3 # . op          subop               mod             rm32          base        index         scale       r32
  4 # . 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
  5 
  6 # read all bytes from 'f' and store them into 's'
  7 # abort if 's' is too small
  8 slurp:  # f : (address buffered-file), s : (address stream byte) -> <void>
  9     # pseudocode:
 10     #   while true
 11     #     if (s->write >= s->length) abort
 12     #     if (f->read >= f->write) populate stream from file
 13     #     if (f->write == 0) break
 14     #     AL = f->data[f->read]
 15     #     s->data[s->write] = AL
 16     #     ++f->read
 17     #     ++s->write
 18     # . prolog
 19     55/push-EBP
 20     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 21     # . save registers
 22     50/push-EAX
 23     51/push-ECX
 24     52/push-EDX
 25     56/push-ESI
 26     57/push-EDI
 27     # ESI = f
 28     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
 29     # ECX = f->read
 30     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(ESI+8) to ECX
 31     # EDI = s
 32     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
 33     # EDX = s->write
 34     8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # copy *EDI to EDX
 35 $slurp:loop:
 36     # if (s->write >= s->length) abort
 37     3b/compare                      1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # compare EDX with *(EDI+8)
 38     7d/jump-if-greater-or-equal  $slurp:abort/disp8
 39     # if (f->read >= f->write) populate stream from file
 40     3b/compare                      1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   4/disp8         .                 # compare ECX with *(ESI+4)
 41     7c/jump-if-lesser  $slurp:from-stream/disp8
 42     # . clear-stream(stream = f+4)
 43     # . . push args
 44     8d/copy-address                 1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   4/disp8         .                 # copy ESI+4 to EAX
 45     50/push-EAX
 46     # . . call
 47     e8/call  clear-stream/disp32
 48     # . . discard args
 49     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 50     # . f->read must now be 0; update its cache at ECX
 51     31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
 52     # . EAX = read(f->fd, stream = f+4)
 53     # . . push args
 54     50/push-EAX
 55     ff          6/subop/push        0/mod/indirect  6/rm32/ESI    .           .             .           .           .               .                 # push *ESI
 56     # . . call
 57     e8/call  read/disp32
 58     # . . discard args
 59     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 60     # if (f->write == 0) break
 61     # since f->read was initially 0, EAX is the same as f->write
 62     # . if (EAX == 0) return true
 63     3d/compare-EAX-and  0/imm32
 64     74/jump-if-equal  $slurp:end/disp8
 65 $slurp:from-stream:
 66     # AL = f->data[f->read]
 67     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
 68     8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/ESI  1/index/ECX   .           0/r32/AL    0x10/disp8      .                 # copy byte at *(ESI+ECX+16) to AL
 69     # s->data[s->write] = AL
 70     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  2/index/EDX   .           0/r32/AL    0xc/disp8       .                 # copy AL to *(EDI+EDX+12)
 71     # ++f->read
 72     41/increment-ECX
 73     # ++s->write
 74     42/increment-EDX
 75     eb/jump  $slurp:loop/disp8
 76 $slurp:end:
 77     # save f->read
 78     89/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           1/r32/ECX   8/disp8         .                 # copy ECX to *(ESI+8)
 79     # save s->write
 80     89/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # copy EDX to *EDI
 81     # . restore registers
 82     5f/pop-to-EDI
 83     5e/pop-to-ESI
 84     5a/pop-to-EDX
 85     59/pop-to-ECX
 86     58/pop-to-EAX
 87     # . epilog
 88     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 89     5d/pop-to-EBP
 90     c3/return
 91 
 92 $slurp:abort:
 93     # . _write(2/stderr, error)
 94     # . . push args
 95     68/push  "slurp: destination too small\n"/imm32
 96     68/push  2/imm32/stderr
 97     # . . call
 98     e8/call  _write/disp32
 99     # . . discard args
100     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
101     # . syscall(exit, 1)
102     bb/copy-to-EBX  1/imm32
103     b8/copy-to-EAX  1/imm32/exit
104     cd/syscall  0x80/imm8
105     # never gets here
106 
107 test-slurp:
108     # setup
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     # . clear-stream(_test-buffered-file+4)
117     # . . push args
118     b8/copy-to-EAX  _test-buffered-file/imm32
119     05/add-to-EAX  4/imm32
120     50/push-EAX
121     # . . call
122     e8/call  clear-stream/disp32
123     # . . discard args
124     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
125     # . clear-stream(_test-tmp-stream)
126     # . . push args
127     68/push  _test-tmp-stream/imm32
128     # . . call
129     e8/call  clear-stream/disp32
130     # . . discard args
131     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
132     # write(_test-stream, "ab\ncd")
133     # . . push args
134     68/push  "ab\ncd"/imm32
135     68/push  _test-stream/imm32
136     # . . call
137     e8/call  write/disp32
138     # . . discard args
139     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
140     # read a line from _test-stream (buffered by _test-buffered-file) into _test-tmp-stream
141     # . EAX = slurp(_test-buffered-file, _test-tmp-stream)
142     # . . push args
143     68/push  _test-tmp-stream/imm32
144     68/push  _test-buffered-file/imm32
145     # . . call
146     e8/call  slurp/disp32
147     # . . discard args
148     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
149     # check-stream-equal(_test-tmp-stream, "ab\ncd", msg)
150     # . . push args
151     68/push  "F - test-slurp"/imm32
152     68/push  "ab\ncd"/imm32
153     68/push  _test-tmp-stream/imm32
154     # . . call
155     e8/call  check-stream-equal/disp32
156     # . . discard args
157     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
158     # end
159     c3/return
160 
161 # . . vim:nowrap:textwidth=0