https://github.com/akkartik/mu/blob/master/121new-stream.subx
  1 # Helper to allocate a stream on the heap.
  2 
  3 == code
  4 #   instruction                     effective address                                                   register    displacement    immediate
  5 # . op          subop               mod             rm32          base        index         scale       r32
  6 # . 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
  7 
  8 new-stream:  # ad: (addr allocation-descriptor), length: int, elemsize: int, out: (addr handle stream _)
  9     # . prologue
 10     55/push-ebp
 11     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 12     # . save registers
 13     50/push-eax
 14     52/push-edx
 15     # var size/edx: int = elemsize*length (clobbering eax)
 16     # . eax = elemsize
 17     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x10/disp8      .                 # copy *(ebp+16) to eax
 18     # . eax *= length
 19     31/xor                          3/mod/direct    2/rm32/edx    .           .             .           2/r32/edx   .               .                 # clear edx
 20     f7          4/subop/multiply    1/mod/*+disp8   5/rm32/ebp    .           .                                     0xc/disp8       .                 # multiply *(ebp+12) into edx:eax
 21     # . if overflow abort
 22     81          7/subop/compare     3/mod/direct    2/rm32/edx    .           .             .           .           .               0/imm32           # compare edx
 23     75/jump-if-!=  $new-stream:abort/disp8
 24     # . edx = elemsize*length
 25     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           0/r32/eax   .               .                 # copy eax to edx
 26     # var n/eax: int = size + 12 (for read, write and size)
 27     05/add-to-eax  0xc/imm32
 28     # allocate(ad, n, out)
 29     # . . push args
 30     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0x14/disp8      .                 # push *(ebp+20)
 31     50/push-eax
 32     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 33     # . . call
 34     e8/call  allocate/disp32
 35     # . . discard args
 36     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 37     # eax = out->payload
 38     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0x14/disp8      .                 # copy *(ebp+20) to eax
 39     8b/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           0/r32/eax   4/disp8         .                 # copy *(eax+4) to eax
 40     # skip payload->allocid
 41     05/add-to-eax  4/imm32
 42     # eax->size = size
 43     89/copy                         1/mod/*+disp8   0/rm32/eax    .           .             .           2/r32/edx   8/disp8         .                 # copy edx to *(eax+8)
 44     # clear-stream(eax)
 45     # . . push args
 46     50/push-eax
 47     # . . call
 48     e8/call  clear-stream/disp32
 49     # . . discard args
 50     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 51 $new-stream:end:
 52     # . restore registers
 53     5a/pop-to-edx
 54     58/pop-to-eax
 55     # . epilogue
 56     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 57     5d/pop-to-ebp
 58     c3/return
 59 
 60 $new-stream:abort:
 61     # . _write(2/stderr, error)
 62     # . . push args
 63     68/push  "new-stream: size too large\n"/imm32
 64     68/push  2/imm32/s class="w">  0:space/names:new-counter <- copy outer  # setup outer space; it *must* come from 'new-counter'
  n/space:1 <- add n/space:1, x
]

def main [
  local-scope
  # counter A
  a:space <- new-counter 34
  # counter B
  b:space <- new-counter 23
  # increment both by 2 but in different ways
  increment-counter a, 1
  b-value:num <- increment-counter b, 2
  a-value:num <- increment-counter a, 1
  # check results
  $print [Contents of counters], 10/newline
  $print [a: ], a-value, [ b: ], b-value,  10/newline
]
ineNr">108 # check-ints-equal(eax, edx, msg) 109 # . . push args 110 68/push "F - test-new-stream: returns current pointer of allocation descriptor"/imm32 111 52/push-edx 112 50/push-eax 113 # . . call 114 e8/call check-ints-equal/disp32 115 # . . discard args 116 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp 117 # skip payload->allocid 118 05/add-to-eax 4/imm32 119 # check-ints-equal(eax->size, 6, msg) 120 # . . push args 121 68/push "F - test-new-stream: sets size correctly"/imm32 122 68/push 6/imm32 123 ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . . 8/disp8 # push *(eax+8) 124 # . . call 125 e8/call check-ints-equal/disp32 126 # . . discard args 127 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp 128 # the rest is delegated to clear-stream() so we won't bother checking it 129 # . reclaim locals 130 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp 131 # . epilogue 132 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 133 5d/pop-to-ebp 134 c3/return 135 136 # . . vim:nowrap:textwidth=0