https://github.com/akkartik/mu/blob/master/083subx-widths.subx
  1 == code
  2 #   instruction                     effective address                                                   register    displacement    immediate
  3 # . op          subop               mod             rm32          base        index         scale       r32
  4 # . 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
  5 
  6 compute-width: # word : (address array byte) -> eax : int
  7     # . prologue
  8     55/push-ebp
  9     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 10     # . save registers
 11     51/push-ecx
 12     # eax = word
 13     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to ecx
 14     # ecx = word + word->length
 15     8b/copy                         0/mod/indirect  0/rm32/eax    .           .             .           1/r32/ecx   .               .                 # copy *eax to ecx
 16     8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/eax  1/index/ecx   .           1/r32/ecx   4/disp8         .                 # copy eax+ecx+4 to ecx
 17     # eax = word->data
 18     05/add-to-eax  4/imm32
 19     # var in/ecx : (address slice) = {eax, ecx}
 20     51/push-ecx
 21     50/push-eax
 22     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
 23     # return compute-width-of-slice(ecx)
 24     # . . push args
 25     51/push-ecx
 26     # . . call
 27     e8/call  compute-width-of-slice/disp32
 28     # . . discard args
 29     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 30 $compute-width:end:
 31     # . reclaim locals
 32     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 33     # . restore registers
 34     59/pop-to-ecx
 35     # . epilogue
 36     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 37     5d/pop-to-ebp
 38     c3/return
 39 
 40 compute-width-of-slice: # s : (address slice) -> eax : int
 41     # . prologue
 42     55/push-ebp
 43     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 44     # . save registers
 45     51/push-ecx
 46     # ecx = s
 47     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
 48     # if (has-metadata?(word, "imm32")) return 4
 49     # . eax = has-metadata?(word, "imm32")
 50     # . . push args
 51     68/push  "imm32"/imm32
 52     51/push-ecx
 53     # . . call
 54     e8/call  has-metadata?/disp32
 55     # . . discard args
 56     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 57     # . if (eax != 0) return 4
 58     3d/compare-eax-and  0/imm32
 59     b8/copy-to-eax  4/imm32         # ZF is set, so we can overwrite eax now
 60     75/jump-if-not-equal  $compute-width-of-slice:end/disp8
 61     # if (has-metadata?(word, "disp32")) return 4
 62     # . eax = has-metadata?(word, "disp32")
 63     # . . push args
 64     68/push  "disp32"/imm32
 65     51/push-ecx
 66     # . . call
 67     e8/call  has-metadata?/disp32
 68     # . . discard args
 69     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 70     # . if (eax != 0) return 4
 71     3d/compare-eax-and  0/imm32
 72     b8/copy-to-eax  4/imm32         # ZF is set, so we can overwrite eax now
 73     75/jump-if-not-equal  $compute-width-of-slice:end/disp8
 74     # if (has-metadata?(word, "imm16")) return 2
 75     # . eax = has-metadata?(word, "imm16")
 76     # . . push args
 77     68/push  "imm16"/imm32
 78     51/push-ecx
 79     # . . call
 80     e8/call  has-metadata?/disp32
 81     # . . discard args
 82     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 83     # . if (eax != 0) return 2
 84     3d/compare-eax-and  0/imm32
 85     b8/copy-to-eax  2/imm32         # ZF is set, so we can overwrite eax now
 86     75/jump-if-not-equal  $compute-width-of-slice:end/disp8
 87     # if (has-metadata?(word, "disp16")) return 2
 88     # . eax = has-metadata?(word, "disp16")
 89     # . . push args
 90     68/push  "disp16"/imm32
 91     51/push-ecx
 92     # . . call
 93     e8/call  has-metadata?/disp32
 94     # . . discard args
 95     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 96     # . if (eax != 0) return 2
 97     3d/compare-eax-and  0/imm32
 98     b8/copy-to-eax  2/imm32         # ZF is set, so we can overwrite eax now
 99     75/jump-if-not-equal  $compute-width-of-slice:end/disp8
100     # otherwise return 1
101     b8/copy-to-eax  1/imm32
102 $compute-width-of-slice:end:
103     # . restore registers
104     59/pop-to-ecx
105     # . epilogue
106     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
107     5d/pop-to-ebp
108     c3/return
109 
110 test-compute-width:
111     # . prologue
112     55/push-ebp
113     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
114 $test-compute-width:imm8:
115     # eax = compute-width("0x2/imm8")
116     # . . push args
117     68/push  "0x2/imm8"/imm32
118     # . . call
119     e8/call  compute-width/disp32
120     # . . discard args
121     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
122     # check-ints-equal(eax, 1, msg)
123     # . . push args
124     68/push  "F - test-compute-width: 0x2/imm8"/imm32
125     50/push-eax
126     68/push  1/imm32
127     # . . call
128     e8/call  check-ints-equal/disp32
129     # . . discard args
130     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
131 $test-compute-width:imm16:
132     # eax = compute-width("4/imm16")
133     # . . push args
134     68/push  "4/imm16"/imm32
135     # . . call
136     e8/call  compute-width/disp32
137     # . . discard args
138     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
139     # check-ints-equal(eax, 2, msg)
140     # . . push args
141     68/push  "F - test-compute-width: 4/imm16"/imm32
142     50/push-eax
143     68/push  2/imm32
144     # . . call
145     e8/call  check-ints-equal/disp32
146     # . . discard args
147     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
148 $test-compute-width:imm32:
149     # eax = compute-width("4/imm32")
150     # . . push args
151     68/push  "4/imm32"/imm32
152     # . . call
153     e8/call  compute-width/disp32
154     # . . discard args
155     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
156     # check-ints-equal(eax, 4, msg)
157     # . . push args
158     68/push  "F - test-compute-width: 4/imm32"/imm32
159     50/push-eax
160     68/push  4/imm32
161     # . . call
162     e8/call  check-ints-equal/disp32
163     # . . discard args
164     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
165 $test-compute-width:disp8:
166     # eax = compute-width("foo/disp8")
167     # . . push args
168     68/push  "foo/disp8"/imm32
169     # . . call
170     e8/call  compute-width/disp32
171     # . . discard args
172     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
173     # check-ints-equal(eax, 1, msg)
174     # . . push args
175     68/push  "F - test-compute-width: foo/disp8"/imm32
176     50/push-eax
177     68/push  1/imm32
178     # . . call
179     e8/call  check-ints-equal/disp32
180     # . . discard args
181     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
182 $test-compute-width:disp16:
183     # eax = compute-width("foo/disp16")
184     # . . push args
185     68/push  "foo/disp16"/imm32
186     # . . call
187     e8/call  compute-width/disp32
188     # . . discard args
189     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
190     # check-ints-equal(eax, 2, msg)
191     # . . push args
192     68/push  "F - test-compute-width: foo/disp16"/imm32
193     50/push-eax
194     68/push  2/imm32
195     # . . call
196     e8/call  check-ints-equal/disp32
197     # . . discard args
198     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
199 $test-compute-width:disp32:
200     # eax = compute-width("foo/disp32")
201     # . . push args
202     68/push  "foo/disp32"/imm32
203     # . . call
204     e8/call  compute-width/disp32
205     # . . discard args
206     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
207     # check-ints-equal(eax, 4, msg)
208     # . . push args
209     68/push  "F - test-compute-width: foo/disp32"/imm32
210     50/push-eax
211     68/push  4/imm32
212     # . . call
213     e8/call  check-ints-equal/disp32
214     # . . discard args
215     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
216 $test-compute-width:no-metadata:
217     # eax = compute-width("45")
218     # . . push args
219     68/push  "45"/imm32
220     # . . call
221     e8/call  compute-width/disp32
222     # . . discard args
223     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
224     # check-ints-equal(eax, 1, msg)
225     # . . push args
226     68/push  "F - test-compute-width: 45 (no metadata)"/imm32
227     50/push-eax
228     68/push  1/imm32
229     # . . call
230     e8/call  check-ints-equal/disp32
231     # . . discard args
232     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
233     # . epilogue
234     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
235     5d/pop-to-ebp
236     c3/return
237 
238 # . . vim:nowrap:textwidth=0