1 # Helper to allocate a stream on the heap.
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
8 new-stream: # ad: (addr allocation-descriptor), length: int, elemsize: int, out: (addr handle stream _)
11 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
15 # var size/edx: int = elemsize*length (clobbering eax)
17 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax
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
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)
30 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x14/disp8 . # push *(ebp+20)
32 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
34 e8/call allocate/disp32
36 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
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
43 89/copy 1/mod/*+disp8 0/rm32/eax . . . 2/r32/edx 8/disp8 . # copy edx to *(eax+8)
48 e8/call clear-stream/disp32
50 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
56 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
61 (abort "new-stream: size too large")
67 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
68 # var ad/ecx: allocation-descriptor containing 16 bytes
69 # . var end/ecx: (addr byte)
71 # . var start/edx: (addr byte) = end - 32
72 81 5/subop/subtract %esp 0x20/imm32
77 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx
78 # var start/edx = ad->curr
79 8b/copy 0/mod/indirect 1/rm32/ecx . . . 2/r32/edx . . # copy *ecx to edx
80 # var h/ebx: (handle stream byte)
83 89/copy 3/mod/direct 3/rm32/ebx . . . 4/r32/esp . . # copy esp to ebx
84 # new-stream(ad, 3, 2, h)
91 e8/call new-stream/disp32
93 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp
95 8b/copy 1/mod/*+disp8 3/rm32/ebx . . . 0/r32/eax 4/disp8 . # copy *(ebx+4) to eax
96 # check-ints-equal(eax, edx, msg)
98 68/push "F - test-new-stream: returns current pointer of allocation descriptor"/imm32
102 e8/call check-ints-equal/disp32
104 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
105 # skip payload->allocid
106 05/add-to-eax 4/imm32
107 # check-ints-equal(eax->size, 6, msg)
109 68/push "F - test-new-stream: sets size correctly"/imm32
111 ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . . 8/disp8 # push *(eax+8)
113 e8/call check-ints-equal/disp32
115 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
116 # the rest is delegated to clear-stream() so we won't bother checking it
118 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x30/imm32 # add to esp
120 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
124 # . . vim:nowrap:textwidth=0