https://github.com/akkartik/mu/blob/master/subx/069slice.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) -> bool/EAX
 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) -> bool/EAX
 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 = false
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 == data
376 
377 _test-slice-data-0:
378     41/A
379 _test-slice-data-1:
380     62/b
381 _test-slice-data-2:
382     63/c
383 _test-slice-data-3:
384     64/d
385 _test-slice-data-4:
386 
387 # . _. vim:nowrap:textwidth=0