https://github.com/akkartik/mu/blob/master/subx/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 # main:
18     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
19     # syscall(exit, Num-test-failures)
20     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
21     b8/copy-to-EAX  1/imm32/exit
22     cd/syscall  0x80/imm8
23 
24 clear-stream:  # f : (address stream) -> <void>
25     # . prolog
26     55/push-EBP
27     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
28     # . save registers
29     50/push-EAX
30     51/push-ECX
31     # EAX = f
32     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
33     # ECX = f->length
34     8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
35     # ECX = &f->data[f->length]
36     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
37     # f->write = 0
38     c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
39     # f->read = 0
40     c7          0/subop/copy        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
41     # EAX = f->data
42     81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
43     # while (true)
44 $clear-stream:loop:
45     # if (EAX >= ECX) break
46     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
47     7d/jump-if-greater-or-equal  $clear-stream:end/disp8
48     # *EAX = 0
49     c6          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm8            # copy byte to *EAX
50     # ++EAX
51     40/increment-EAX
52     eb/jump  $clear-stream:loop/disp8
53 $clear-stream:end:
54     # . restore registers
55     59/pop-to-ECX
56     58/pop-to-EAX
57     # . epilog
58     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
59     5d/pop-to-EBP
60     c3/return
61 
62 rewind-stream:  # f : (address stream) -> <void>
63     # . prolog
64     55/push-EBP
65     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
66     # . save registers
67     50/push-EAX
68     # EAX = f
69     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
70     # f->read = 0
71     c7          0/subop/copy        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
72 $rewind-stream:end:
73     # . restore registers
74     58/pop-to-EAX
75     # . epilog
76     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
77     5d/pop-to-EBP
78     c3/return
79 
80 # . . vim:nowrap:textwidth=0