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