https://github.com/akkartik/mu/blob/master/subx/066allocate.subx
  1 # Helper to dynamically allocate memory on the heap.
  2 #
  3 # We'd like to be able to write tests for functions that allocate memory,
  4 # making assertions on the precise addresses used. To achieve this we'll pass
  5 # in an *allocation descriptor* to allocate from.
  6 #
  7 # Allocation descriptors are also useful outside of tests. Assembly and machine
  8 # code are of necessity unsafe languages, and one of the most insidious kinds
  9 # of bugs unsafe languages expose us to are dangling pointers to memory that
 10 # has been freed and potentially even reused for something totally different.
 11 # To reduce the odds of such "use after free" errors, SubX programs tend to not
 12 # reclaim and reuse dynamically allocated memory. (Running out of memory is far
 13 # easier to debug.) Long-running programs that want to reuse memory are mostly
 14 # on their own to be careful. However, they do get one bit of help: they can
 15 # carve out chunks of memory and then allocate from them manually using this
 16 # very same 'allocate' helper. They just need a new allocation descriptor for
 17 # their book-keeping.
 18 
 19 == data
 20 
 21 # The 'global' allocation descriptor. Pass this into 'allocate' to claim a
 22 # hitherto unused bit of memory.
 23 Heap:
 24     Start-of-heap/imm32  # curr
 25     00 00 00 0b  # limit = 0x0b000000; keep sync'd with DATA_SEGMENT + SEGMENT_ALIGNMENT
 26 
 27 == code
 28 #   instruction                     effective address                                                   register    displacement    immediate
 29 # . op          subop               mod             rm32          base        index         scale       r32
 30 # . 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
 31 
 32 # main:
 33     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 34     # syscall(exit, Num-test-failures)
 35     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 36     b8/copy-to-EAX  1/imm32/exit
 37     cd/syscall  0x80/imm8
 38 
 39 # Claim the next 'n' bytes of memory starting at ad->curr and update ad->curr.
 40 # If there isn't enough memory before ad->limit, return 0 and leave 'ad' unmodified.
 41 allocate:  # ad : (address allocation-descriptor), n : int -> address-or-null/EAX
 42     # . prolog
 43     55/push-EBP
 44     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 45     # . save registers
 46     51/push-ECX
 47     52/push-EDX
 48     # ECX = ad
 49     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EBP+8) to ECX
 50     # save ad->curr
 51     8b/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           0/r32/EAX   .               .                 # copy *ECX to EAX
 52     # check if there's enough space
 53     # . EDX = ad->curr + n
 54     89/copy                         3/mod/direct    2/rm32/EDX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EDX
 55     03/add                          1/mod/*+disp8   5/rm32/EBP    .           .             .           2/r32/EDX   0xc/disp8       .                 # add *(EBP+12) to EDX
 56     3b/compare                      1/mod/*+disp8   1/rm32/ECX    .           .             .           2/r32/EDX   4/disp8         .                 # compare EDX with *(ECX+4)
 57     7c/jump-if-lesser  $allocate:commit/disp8
 58     # return null if not
 59     b8/copy-to-EAX  0/imm32
 60     eb/jump  $allocate:end/disp8
 61 $allocate:commit:
 62     # update ad->curr
 63     89/copy                         0/mod/indirect  1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # copy EDX to *ECX
 64 $allocate:end:
 65     # . restore registers
 66     5a/pop-to-EDX
 67     59/pop-to-ECX
 68     # . epilog
 69     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 70     5d/pop-to-EBP
 71     c3/return
 72 
 73 test-allocate-success:
 74     # . prolog
 75     55/push-EBP
 76     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 77     # var ad/ECX : (address allocation-descriptor) = {11, 15}
 78     68/push  0xf/imm32/limit
 79     68/push  0xb/imm32/curr
 80     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
 81     # EAX = allocate(ad, 3)
 82     # . . push args
 83     68/push  3/imm32
 84     51/push-ECX
 85     # . . call
 86     e8/call  allocate/disp32
 87     # . . discard args
 88     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 89     # check-ints-equal(EAX, 11, msg)
 90     # . . push args
 91     68/push  "F - test-allocate-success: returns current pointer of allocation descriptor"/imm32
 92     68/push  0xb/imm32
 93     50/push-EAX
 94     # . . call
 95     e8/call  check-ints-equal/disp32
 96     # . . discard args
 97     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 98     # check-ints-equal(ad->curr, 14, msg)
 99     # . . push args
100     68/push  "F - test-allocate-success: updates allocation descriptor"/imm32
101     68/push  0xe/imm32
102     ff          6/subop/push        0/mod/indirect  1/rm32/ECX    .           .             .           .           .               .                 # push *ECX
103     # . . call
104     e8/call  check-ints-equal/disp32
105     # . . discard args
106     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
107     # . epilog
108     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
109     5d/pop-to-EBP
110     c3/return
111 
112 test-allocate-failure:
113     # . prolog
114     55/push-EBP
115     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
116     # var ad/ECX : (address allocation-descriptor) = {11, 15}
117     68/push  0xf/imm32/limit
118     68/push  0xb/imm32/curr
119     89/copy                         3/mod/direct    1/rm32/ECX    .           .             .           4/r32/ESP   .               .                 # copy ESP to ECX
120     # EAX = allocate(ad, 6)
121     # . . push args
122     68/push  6/imm32
123     51/push-ECX
124     # . . call
125     e8/call  allocate/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
128     # check-ints-equal(EAX, 0, msg)
129     # . . push args
130     68/push  "F - test-allocate-failure: returns null"/imm32
131     68/push  0/imm32
132     50/push-EAX
133     # . . call
134     e8/call  check-ints-equal/disp32
135     # . . discard args
136     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
137     # no change to ad->curr
138     # . check-ints-equal(ad->curr, 11)
139     # . . push args
140     68/push  "F - test-allocate-failure: updates allocation descriptor"/imm32
141     68/push  0xb/imm32
142     ff          6/subop/push        0/mod/indirect  1/rm32/ECX    .           .             .           .           .               .                 # push *ECX
143     # . . call
144     e8/call  check-ints-equal/disp32
145     # . . discard args
146     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
147     # . epilog
148     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
149     5d/pop-to-EBP
150     c3/return
151 
152 # . . vim:nowrap:textwidth=0