https://github.com/akkartik/mu/blob/main/linux/106stream.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 size as usual
     7 #
     8 # some primitives for operating on streams:
     9 #   - clear-stream (clears everything but the data size)
    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: (addr stream byte)
    18     # . prologue
    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     # var count/ecx: int = f->size
    27     8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(eax+8) to ecx
    28     # var max/ecx: (addr byte) = &f->data[f->size]
    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     # - clear all stream data
    35     # - this isn't strictly necessary, and it can slow things down *a lot*, but better safe than sorry.
    36     # var curr/eax: (addr byte) = f->data
    37     81          0/subop/add         3/mod/direct    0/rm32/eax    .           .             .           .           .               0xc/imm32         # add to eax
    38 $clear-stream:loop:
    39     # if (curr >= max) break
    40     39/compare                      3/mod/direct    0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # compare eax with ecx
    41     73/jump-if-addr>=  $clear-stream:end/disp8
    42     # *curr = 0
    43     c6          0/subop/copy-byte   0/mod/direct    0/rm32/eax    .           .             .           .           .               0/imm8            # copy byte to *eax
    44     # ++curr
    45     40/increment-eax
    46     eb/jump  $clear-stream:loop/disp8
    47 $clear-stream:end:
    48     # . restore registers
    49     59/pop-to-ecx
    50     58/pop-to-eax
    51     # . epilogue
    52     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    53     5d/pop-to-ebp
    54     c3/return
    55
    56 rewind-stream:  # f: (addr stream byte)
    57     # . prologue
    58     55/push-ebp
    59     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    60     # . save registers
    61     50/push-eax
    62     # eax = f
    63     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .                         0/r32/eax   8/disp8         .                 # copy *(ebp+8) to eax
    64     # f->read = 0
    65     c7          0/subop/copy        1/mod/*+disp8   0/rm32/eax    .           .             .           .           4/disp8         0/imm32           # copy to *(eax+4)
    66 $rewind-stream:end:
    67     # . restore registers
    68     58/pop-to-eax
    69     # . epilogue
    70     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    71     5d/pop-to-ebp
    72     c3/return
    73
    74 # . . vim:nowrap:textwidth=0