https://github.com/akkartik/mu/blob/master/subx/070new-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 : (address allocation-descriptor), length : int, elemsize : int -> address/EAX
  9     # . prolog
 10     55/push-EBP
 11     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 12     # . save registers
 13     52/push-EDX
 14     # n = elemsize * length + 12 (for read, write and length)
 15     # . EAX = elemsize
 16     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0x10/disp8      .                 # copy *(EBP+16) to EAX
 17     # . EAX *= length
 18     31/xor                          3/mod/direct    2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # clear EDX
 19     f7          4/subop/multiply    1/mod/*+disp8   5/rm32/EBP    .           .                                     0xc/disp8       .                 # multiply *(EBP+12) into EAX
 20     # . if overflow abort
 21     81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               0/imm32           # compare EDX
 22     75/jump-if-not-equal  $new-stream:abort/disp8
 23     # . EDX = elemsize*length
 24     89/copy                         3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EDX
 25     # . EAX += 12
 26     05/add-to-EAX  0xc/imm32
 27     # allocate(ad, n)
 28     # . . push args
 29     50/push-EAX
 30     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 31     # . . call
 32     e8/call  allocate/disp32
 33     # . . discard args
 34     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 35     # EAX->length = elemsize*length
 36     89/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           2/r32/EDX   8/disp8         .                 # copy EDX to *(EAX+8)
 37     # clear-stream(EAX)
 38     # . . push args
 39     50/push-EAX
 40     # . . call
 41     e8/call  clear-stream/disp32
 42     # . . discard args
 43     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 44 $new-stream:end:
 45     # . restore registers
 46     5a/pop-to-EDX
 47     # . epilog
 48     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 49     5d/pop-to-EBP
 50     c3/return
 51 
 52 $new-stream:abort:
 53     # . _write(2/stderr, error)
 54     # . . push args
 55     68/push  "new-stream: size too large"/imm32
 56     68/push  2/imm32/stderr
 57     # . . call
 58     e8/call  _write/disp32
 59     # . . discard args
 60     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 61     # . syscall(exit, 1)
 62     bb/copy-to-EBX  1/imm32
 63     b8/copy-to-EAX  1/imm32/exit
 64     cd/syscall  0x80/imm8
 65     # never gets here
 66 
 67 test-new-stream:
 68     # . prolog
 69     55/push-EBP
 70     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 71     # var ad/ECX : (address allocation-descriptor) = allocate-region(Heap, 512)
 72     # . EAX = allocate-region(Heap, 512)
 73     # . . push args
 74     68/push  0x200/imm32
 75     68/push  Heap/imm32
 76     # . . call
 77     e8/call  allocate-region/disp32
 78     # . . discard args
 79     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 80     # . ECX = EAX
 81     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy EAX to ECX
 82     # var start/EDX = ad->curr
 83     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy *ECX to EDX
 84     # EAX = new-stream(ad, 3, 2)
 85     # . . push args
 86     68/push  2/imm32
 87     68/push  3/imm32
 88     51/push-ECX
 89     # . . call
 90     e8/call  new-stream/disp32
 91     # . . discard args
 92     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 93     # check-ints-equal(EAX, EDX, msg)
 94     # . . push args
 95     68/push  "F - test-new-stream: returns current pointer of allocation descriptor"/imm32
 96     52/push-EDX
 97     50/push-EAX
 98     # . . call
 99     e8/call  check-ints-equal/disp32
100     # . . discard args
101     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
102     # check-ints-equal(EAX->length, 6, msg)
103     # . . push args
104     68/push  "F - test-new-stream: sets length correctly"/imm32
105     68/push  6/imm32
106     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           .               8/disp8           # push *(EAX+8)
107     # . . call
108     e8/call  check-ints-equal/disp32
109     # . . discard args
110     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
111     # the rest is delegated to clear-stream() so we won't bother checking it
112     # . epilog
113     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
114     5d/pop-to-EBP
115     c3/return
116 
117 # . . vim:nowrap:textwidth=0