https://github.com/akkartik/mu/blob/master/subx/072slice.subx
  1 # new data structure: a slice is an open interval of addresses [start, end)
  2 # that includes 'start' but not 'end'
  3 
  4 == code
  5 #   instruction                     effective address                                                   register    displacement    immediate
  6 # . op          subop               mod             rm32          base        index         scale       r32
  7 # . 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
  8 
  9 # main:
 10     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 11     # syscall(exit, Num-test-failures)
 12     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 13     b8/copy-to-EAX  1/imm32/exit
 14     cd/syscall  0x80/imm8
 15 
 16 slice-empty?:  # s : (address slice) -> EAX : boolean
 17     # . prolog
 18     55/push-EBP
 19     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 20     # . save registers
 21     51/push-ECX
 22     # ECX = s
 23     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 24     # if s->start == s->end return true
 25     # . EAX = s->start
 26     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy *ECX to EAX
 27     # . compare EAX with s->end
 28     39/compare                      1/mod/*+disp8   1/rm32/ECX    .           .             .           0/r32/EAX   4/disp8         .                 # compare EAX and *(ECX+4)
 29     b8/copy-to-EAX  1/imm32/true
 30     74/jump-if-equal  $slice-empty?:end/disp8
 31     b8/copy-to-EAX  0/imm32/false
 32 $slice-empty?:end:
 33     # . restore registers
 34     59/pop-to-ECX
 35     # . epilog
 36     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 37     5d/pop-to-EBP
 38     c3/return
 39 
 40 test-slice-empty-true:
 41     # . prolog
 42     55/push-EBP
 43     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 44     # var slice/ECX = {34, 34}
 45     68/push  34/imm32/end
 46     68/push  34/imm32/start
 47     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
 48     # slice-empty?(slice)
 49     # . . push args
 50     51/push-ECX
 51     # . . call
 52     e8/call  slice-empty?/disp32
 53     # . . discard args
 54     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 55     # check-ints-equal(EAX, 1, msg)
 56     # . . push args
 57     68/push  "F - test-slice-empty-true"/imm32
 58     68/push  1/imm32
 59     50/push-EAX
 60     # . . call
 61     e8/call  check-ints-equal/disp32
 62     # . . discard args
 63     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 64     # . epilog
 65     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 66     5d/pop-to-EBP
 67     c3/return
 68 
 69 test-slice-empty-false:
 70     # . prolog
 71     55/push-EBP
 72     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 73     # var slice/ECX = {34, 23}
 74     68/push  23/imm32/end
 75     68/push  34/imm32/start
 76     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
 77     # slice-empty?(slice)
 78     # . . push args
 79     51/push-ECX
 80     # . . call
 81     e8/call  slice-empty?/disp32
 82     # . . discard args
 83     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 84     # check-ints-equal(EAX, 0, msg)
 85     # . . push args
 86     68/push  "F - test-slice-empty-false"/imm32
 87     68/push  0/imm32
 88     50/push-EAX
 89     # . . call
 90     e8/call  check-ints-equal/disp32
 91     # . . discard args
 92     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 93     # . epilog
 94     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 95     5d/pop-to-EBP
 96     c3/return
 97 
 98 slice-equal?:  # s : (address slice), p : (address string) -> EAX : boolean
 99     # . prolog
100     55/push-EBP
101     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
102     # . save registers
103     51/push-ECX
104     52/push-EDX
105     53/push-EBX
106     56/push-ESI
107     # ESI = s
108     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
109     # curr/EDX = s->start
110     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           2/r32/EDX   .               .                 # copy *ESI to EDX
111     # max/ESI = s->end
112     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           6/r32/ESI   4/disp8         .                 # copy *(ESI+4) to ESI
113     # EBX = p
114     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
115     # EAX = s->end - s->start
116     89/copy                         3/mod/direct    0/rm32/EAX    .           .             .           6/r32/ESI   .               .                 # copy ESI to EAX
117     29/subtract                     3/mod/direct    0/rm32/EAX    .           .             .           2/r32/EDX   .               .                 # subtract EDX from EAX
118     # if (EAX != p->length) return false;
119     39/compare                      0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # compare *EBX and EAX
120     75/jump-if-not-equal  $slice-equal?:false/disp8
121     # skip p->length
122     81          0/subop/add         3/mod/direct    3/rm32/EBX    .           .             .           .           .               4/imm32           # add to EBX
123     # EAX = ECX = 0
124     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
125     31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
126 $slice-equal?:loop:
127     # if (curr >= max) return true
128     39/compare                      3/mod/direct    2/rm32/EDX    .           .             .           6/r32/ESI   .               .                 # compare EDX and ESI
129     7d/jump-if-greater-or-equal  $slice-equal?:true/disp8
130     # AL = *p
131     8a/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           0/r32/AL    .               .                 # copy byte at *EBX to AL
132     # CL = *curr
133     8a/copy-byte                    0/mod/indirect  2/rm32/EDX    .           .             .           1/r32/CL    .               .                 # copy byte at *EDX to CL
134     # if (EAX != ECX) return false
135     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX and ECX
136     75/jump-if-not-equal  $slice-equal?:false/disp8
137     # ++p
138     43/increment-EBX
139     # ++curr
140     42/increment-EDX
141     eb/jump $slice-equal?:loop/disp8
142 $slice-equal?:false:
143     b8/copy-to-EAX  0/imm32
144     eb/jump  $slice-equal?:end/disp8
145 $slice-equal?:true:
146     b8/copy-to-EAX  1/imm32
147 $slice-equal?:end:
148     # . restore registers
149     5e/pop-to-ESI
150     5b/pop-to-EBX
151     5a/pop-to-EDX
152     59/pop-to-ECX
153     # . epilog
154     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
155     5d/pop-to-EBP
156     c3/return
157 
158 test-slice-equal:
159     # - slice-equal?(slice("Abc"), "Abc") == 1
160     # . prolog
161     55/push-EBP
162     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
163     # var slice/ECX
164     68/push  _test-slice-data-3/imm32/end
165     68/push  _test-slice-data-0/imm32/start
166     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
167     # EAX = slice-equal?(ECX, "Abc")
168     # . . push args
169     68/push  "Abc"/imm32
170     51/push-ECX
171     # . . call
172     e8/call  slice-equal?/disp32
173     # . . discard args
174     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
175     # check-ints-equal(EAX, 1, msg)
176     # . . push args
177     68/push  "F - test-slice-equal"/imm32
178     68/push  1/imm32
179     50/push-EAX
180     # . . call
181     e8/call  check-ints-equal/disp32
182     # . . discard args
183     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
184     # . epilog
185     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
186     5d/pop-to-EBP
187     c3/return
188 
189 test-slice-equal-false:
190     # - slice-equal?(slice("bcd"), "Abc") == 0
191     # . prolog
192     55/push-EBP
193     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
194     # var slice/ECX
195     68/push  _test-slice-data-4/imm32/end
196     68/push  _test-slice-data-1/imm32/start
197     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
198     # EAX = slice-equal?(ECX, "Abc")
199     # . . push args
200     68/push  "Abc"/imm32
201     51/push-ECX
202     # . . call
203     e8/call  slice-equal?/disp32
204     # . . discard args
205     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
206     # check-ints-equal(EAX, 0, msg)
207     # . . push args
208     68/push  "F - test-slice-equal-false"/imm32
209     68/push  0/imm32
210     50/push-EAX
211     # . . call
212     e8/call  check-ints-equal/disp32
213     # . . discard args
214     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
215     # . epilog
216     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
217     5d/pop-to-EBP
218     c3/return
219 
220 test-slice-equal-too-long:
221     # - slice-equal?(slice("Abcd"), "Abc") == 0
222     # . prolog
223     55/push-EBP
224     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
225     # var slice/ECX
226     68/push  _test-slice-data-4/imm32/end
227     68/push  _test-slice-data-0/imm32/start
228     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
229     # EAX = slice-equal?(ECX, "Abc")
230     # . . push args
231     68/push  "Abc"/imm32
232     51/push-ECX
233     # . . call
234     e8/call  slice-equal?/disp32
235     # . . discard args
236     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
237     # check-ints-equal(EAX, 0, msg)
238     # . . push args
239     68/push  "F - test-slice-equal-too-long"/imm32
240     68/push  0/imm32
241     50/push-EAX
242     # . . call
243     e8/call  check-ints-equal/disp32
244     # . . discard args
245     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
246     # . epilog
247     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
248     5d/pop-to-EBP
249     c3/return
250 
251 test-slice-equal-too-short:
252     # - slice-equal?(slice("A"), "Abc") == 0
253     # . prolog
254     55/push-EBP
255     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
256     # var slice/ECX
257     68/push  _test-slice-data-1/imm32/end
258     68/push  _test-slice-data-0/imm32/start
259     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
260     # EAX = slice-equal?(ECX, "Abc")
261     # . . push args
262     68/push  "Abc"/imm32
263     51/push-ECX
264     # . . call
265     e8/call  slice-equal?/disp32
266     # . . discard args
267     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
268     # check-ints-equal(EAX, 0, msg)
269     # . . push args
270     68/push  "F - test-slice-equal-too-short"/imm32
271     68/push  0/imm32
272     50/push-EAX
273     # . . call
274     e8/call  check-ints-equal/disp32
275     # . . discard args
276     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
277     # . epilog
278     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
279     5d/pop-to-EBP
280     c3/return
281 
282 test-slice-equal-empty:
283     # - slice-equal?(slice(""), "Abc") == 0
284     # . prolog
285     55/push-EBP
286     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
287     # var slice/ECX
288     68/push  _test-slice-data-0/imm32/end
289     68/push  _test-slice-data-0/imm32/start
290     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
291     # EAX = slice-equal?(ECX, "Abc")
292     # . . push args
293     68/push  "Abc"/imm32
294     51/push-ECX
295     # . . call
296     e8/call  slice-equal?/disp32
297     # . . discard args
298     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
299     # check-ints-equal(EAX, 0, msg)
300     # . . push args
301     68/push  "F - test-slice-equal-empty"/imm32
302     68/push  0/imm32
303     50/push-EAX
304     # . . call
305     e8/call  check-ints-equal/disp32
306     # . . discard args
307     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
308     # . epilog
309     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
310     5d/pop-to-EBP
311     c3/return
312 
313 test-slice-equal-with-empty:
314     # - slice-equal?(slice("Ab"), "") == 0
315     # . prolog
316     55/push-EBP
317     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
318     # var slice/ECX
319     68/push  _test-slice-data-2/imm32/end
320     68/push  _test-slice-data-0/imm32/start
321     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
322     # EAX = slice-equal?(ECX, "")
323     # . . push args
324     68/push  ""/imm32
325     51/push-ECX
326     # . . call
327     e8/call  slice-equal?/disp32
328     # . . discard args
329     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
330     # check-ints-equal(EAX, 0, msg)
331     # . . push args
332     68/push  "F - test-slice-equal-with-empty"/imm32
333     68/push  0/imm32
334     50/push-EAX
335     # . . call
336     e8/call  check-ints-equal/disp32
337     # . . discard args
338     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
339     # . epilog
340     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
341     5d/pop-to-EBP
342     c3/return
343 
344 test-slice-equal-empty-with-empty:
345     # - slice-equal?(slice(""), "") == 1
346     # . prolog
347     55/push-EBP
348     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
349     # var slice/ECX
350     68/push  _test-slice-data-0/imm32/end
351     68/push  _test-slice-data-0/imm32/start
352     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
353     # EAX = slice-equal?(ECX, "")
354     # . . push args
355     68/push  ""/imm32
356     51/push-ECX
357     # . . call
358     e8/call  slice-equal?/disp32
359     # . . discard args
360     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
361     # check-ints-equal(EAX, 1, msg)
362     # . . push args
363     68/push  "F - test-slice-equal-empty-with-empty"/imm32
364     68/push  1/imm32
365     50/push-EAX
366     # . . call
367     e8/call  check-ints-equal/disp32
368     # . . discard args
369     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
370     # . epilog
371     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
372     5d/pop-to-EBP
373     c3/return
374 
375 write-slice:  # out : (address buffered-file), s : (address slice)
376     # . prolog
377     55/push-EBP
378     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
379     # . save registers
380     50/push-EAX
381     51/push-ECX
382     52/push-EDX
383     53/push-EBX
384     56/push-ESI
385     57/push-EDI
386     # ESI = s
387     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
388     # curr/ECX = s->start
389     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           1/r32/ECX   .               .                 # copy *ESI to ECX
390     # max/ESI = s->end
391     8b/copy                         1/mod/*+disp8   6/rm32/ESI    .           .             .           6/r32/ESI   4/disp8         .                 # copy *(ESI+4) to ESI
392     # EDI = f
393     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
394     # EDX = f->length
395     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   0xc/disp8       .                 # copy *(EDI+12) to EDX
396     # EBX = f->write
397     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy *(EDI+4) to EBX
398 $write-slice:loop:
399     # if (curr >= max) break
400     39/compare                      3/mod/direct    1/rm32/ECX    .           .             .           6/r32/ESI   .               .                 # compare ECX with ESI
401     7d/jump-if-greater-or-equal  $write-slice:loop-end/disp8
402     # if (f->write >= f->length) flush and clear f's stream
403     39/compare                      3/mod/direct    3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # compare EBX with EDX
404     7c/jump-if-lesser  $write-slice:to-stream/disp8
405     # . persist f->write
406     89/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy EBX to *(EDI+4)
407     # . flush(f)
408     # . . push args
409     57/push-EDI
410     # . . call
411     e8/call  flush/disp32
412     # . . discard args
413     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
414     # . clear-stream(stream = f+4)
415     # . . push args
416     8d/copy-address                 1/mod/*+disp8   7/rm32/EDI    .           .             .           0/r32/EAX   4/disp8         .                 # copy EDI+4 to EAX
417     50/push-EAX
418     # . . call
419     e8/call  clear-stream/disp32
420     # . . discard args
421     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
422     # . f->write must now be 0; update its cache at EBX
423     31/xor                          3/mod/direct    3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # clear EBX
424 $write-slice:to-stream:
425     # f->data[f->write] = *in
426     # . AL = *in
427     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
428     8a/copy-byte                    0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/AL    .               .                 # copy byte at *ECX to AL
429     # . f->data[f->write] = AL
430     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  3/index/EBX   .           0/r32/AL    0x10/disp8      .                 # copy AL to *(EDI+EBX+16)
431     # ++f->write
432     43/increment-EBX
433     # ++in
434     41/increment-ECX
435     eb/jump  $write-slice:loop/disp8
436 $write-slice:loop-end:
437     # persist necessary variables from registers
438     89/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           3/r32/EBX   4/disp8         .                 # copy EBX to *(EDI+4)
439 $write-slice:end:
440     # . restore registers
441     5f/pop-to-EDI
442     5e/pop-to-ESI
443     5b/pop-to-EBX
444     5a/pop-to-EDX
445     59/pop-to-ECX
446     58/pop-to-EAX
447     # . epilog
448     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
449     5d/pop-to-EBP
450     c3/return
451 
452 test-write-slice:
453     # . prolog
454     55/push-EBP
455     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
456     # setup
457     # . clear-stream(_test-stream)
458     # . . push args
459     68/push  _test-stream/imm32
460     # . . call
461     e8/call  clear-stream/disp32
462     # . . discard args
463     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
464     # . clear-stream(_test-buffered-file+4)
465     # . . push args
466     b8/copy-to-EAX  _test-buffered-file/imm32
467     05/add-to-EAX  4/imm32
468     50/push-EAX
469     # . . call
470     e8/call  clear-stream/disp32
471     # . . discard args
472     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
473     # var slice/ECX = "Abc"
474     68/push  _test-slice-data-3/imm32/end
475     68/push  _test-slice-data-0/imm32/start
476     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
477     # write-slice(_test-buffered-file, slice)
478     # . . push args
479     51/push-ECX
480     68/push  _test-buffered-file/imm32
481     # . . call
482     e8/call  write-slice/disp32
483     # . . discard args
484     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
485     # flush(_test-buffered-file)
486     # . . push args
487     68/push  _test-buffered-file/imm32
488     # . . call
489     e8/call  flush/disp32
490     # . . discard args
491     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
492     # check-stream-equal(_test-stream, "Abc", msg)
493     # . . push args
494     68/push  "F - test-write-slice"/imm32
495     68/push  "Abc"/imm32
496     68/push  _test-stream/imm32
497     # . . call
498     e8/call  check-stream-equal/disp32
499     # . . discard args
500     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
501     # . epilog
502     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
503     5d/pop-to-EBP
504     c3/return
505 
506 # write an entire stream's contents to a buffered-file
507 # ways to do this:
508 #   - construct a 'maximal slice' and pass it to write-slice
509 #   - flush the buffered-file and pass the stream directly to its fd (disabling buffering)
510 # we'll go with the first way for now
511 write-stream-buffered:  # f : (address buffered-file), s : (address stream)
512     # . prolog
513     55/push-EBP
514     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
515     # . save registers
516     50/push-EAX
517     56/push-ESI
518     # ESI = s
519     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
520     # var slice/ECX = {s->data, s->data + s->write}
521     # . push s->data + s->write
522     8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/EAX   .               .                 # copy *ESI to EAX
523     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
524     50/push-EAX
525     # . push s->data
526     8d/copy-address                 1/mod/*+disp8   6/rm32/ESI    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy ESI+12 to EAX
527     50/push-EAX
528     # . ECX = ESP
529     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
530     # write-slice(f, slice)
531     # . . push args
532     51/push-ECX
533     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
534     # . . call
535     e8/call  write-slice/disp32
536     # . . discard args
537     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
538 $write-stream-buffered:end:
539     # . restore registers
540     5e/pop-to-ESI
541     58/pop-to-EAX
542     # . epilog
543     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
544     5d/pop-to-EBP
545     c3/return
546 
547 test-write-stream-buffered:
548     # . prolog
549     55/push-EBP
550     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
551     # setup
552     # . clear-stream(_test-stream)
553     # . . push args
554     68/push  _test-stream/imm32
555     # . . call
556     e8/call  clear-stream/disp32
557     # . . discard args
558     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
559     # . clear-stream(_test-buffered-file+4)
560     # . . push args
561     b8/copy-to-EAX  _test-buffered-file/imm32
562     05/add-to-EAX  4/imm32
563     50/push-EAX
564     # . . call
565     e8/call  clear-stream/disp32
566     # . . discard args
567     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
568     # . clear-stream(_test-tmp-stream)
569     # . . push args
570     68/push  _test-tmp-stream/imm32
571     # . . call
572     e8/call  clear-stream/disp32
573     # . . discard args
574     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
575     # initialize input
576     # . write(_test-tmp-stream, "abcd")
577     # . . push args
578     68/push  "abcd"/imm32
579     68/push  _test-tmp-stream/imm32
580     # . . call
581     e8/call  write/disp32
582     # . . discard args
583     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
584     # perform the write-stream-buffered
585     # . write-stream-buffered(_test-buffered-file, _test-tmp-stream)
586     # . . push args
587     68/push  _test-tmp-stream/imm32
588     68/push  _test-buffered-file/imm32
589     # . . call
590     e8/call  write-stream-buffered/disp32
591     # . . discard args
592     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
593     # check that the write happened as expected
594     # . flush(_test-buffered-file)
595     # . . push args
596     68/push  _test-buffered-file/imm32
597     # . . call
598     e8/call  flush/disp32
599     # . . discard args
600     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
601     # . check-stream-equal(_test-stream, "abcd", msg)
602     # . . push args
603     68/push  "F - test-write-stream-buffered"/imm32
604     68/push  "abcd"/imm32
605     68/push  _test-stream/imm32
606     # . . call
607     e8/call  check-stream-equal/disp32
608     # . . discard args
609     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
610     # . epilog
611     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
612     5d/pop-to-EBP
613     c3/return
614 
615 == data
616 
617 _test-slice-data-0:
618     41/A
619 _test-slice-data-1:
620     62/b
621 _test-slice-data-2:
622     63/c
623 _test-slice-data-3:
624     64/d
625 _test-slice-data-4:
626 
627 # . _. vim:nowrap:textwidth=0