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