https://github.com/akkartik/mu/blob/master/055stream.subx
 1 # streams: data structure for operating on arrays in a stateful manner
 2 #
 3 # A stream looks like this:
 4 #   write : int  # index at which writes go
 5 #   read : int  # index that we've read until
 6 #   data : (array byte)  # prefixed by length as usual
 7 #
 8 # some primitives for operating on streams:
 9 #   - clear-stream (clears everything but the data length)
10 #   - rewind-stream (resets read pointer)
11 
12 == code
13 #   instruction                     effective address                                                   register    displacement    immediate
14 # . op          subop               mod             rm32          base        index         scale       r32
15 # . 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
16 
17 clear-stream:  # f : (address stream) -> <void>
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     # EAX = f
25     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
26     # ECX = f->length
27     8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
28     # ECX = &f->data[f->length]
29     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   0xc/disp8       .                 # copy EAX+ECX+12 to ECX
30     # f->write = 0
31     c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
32     # f->read = 0
33     c7          0/subop/copy        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
34     # EAX = f->data
35     81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
36     # while (true)
37 $clear-stream:loop:
38     # if (EAX >= ECX) break
39     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
40     73/jump-if-greater-or-equal-unsigned  $clear-stream:end/disp8
41     # *EAX = 0
42     c6          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm8            # copy byte to *EAX
43     # ++EAX
44     40/increment-EAX
45     eb/jump  $clear-stream:loop/disp8
46 $clear-stream:end:
47     # . restore registers
48     59/pop-to-ECX
49     58/pop-to-EAX
50     # . epilog
51     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
52     5d/pop-to-EBP
53     c3/return
54 
55 rewind-stream:  # f : (address stream) -> <void>
56     # . prolog
57     55/push-EBP
58     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
59     # . save registers
60     50/push-EAX
61     # EAX = f
62     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
63     # f->read = 0
64     c7          0/subop/copy        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
65 $rewind-stream:end:
66     # . restore registers
67     58/pop-to-EAX
68     # . epilog
69     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
70     5d/pop-to-EBP
71     c3/return
72 
73 # . . vim:nowrap:textwidth=0