https://github.com/akkartik/mu/blob/master/066print-int.subx
  1 # Print the (hex) textual representation of numbers.
  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 # convert the lowest nibble of eax to ascii and return it in the lowest byte of eax
  9 to-hex-char:  # in/eax : int -> out/eax : int
 10     # no error checking; accepts argument in eax
 11     # if (eax <= 9) return eax + '0'
 12     3d/compare-eax-with  0x9/imm32/9
 13     7f/jump-if-greater  $to-hex-char:else/disp8
 14     05/add-to-eax  0x30/imm32/0
 15     c3/return
 16 $to-hex-char:else:
 17     # otherwise return eax + 'a' - 10
 18     05/add-to-eax  0x57/imm32/a-10
 19     c3/return
 20 
 21 append-byte-hex:  # f : (addr stream byte), n : int
 22     # . prologue
 23     55/push-ebp
 24     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 25     # . save registers
 26     50/push-eax
 27     # AL = convert upper nibble to hex
 28     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
 29     c1/shift    5/subop/logic-right 3/mod/direct    0/rm32/eax    .           .             .           .           .               4/imm8            # shift eax right by 4 bits, while padding zeroes
 30     25/and-eax  0xf/imm32
 31     # . AL = to-hex-char(AL)
 32     e8/call  to-hex-char/disp32
 33     # append-byte(f, AL)
 34     # . . push args
 35     50/push-eax
 36     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 37     # . . call
 38     e8/call  append-byte/disp32
 39     # . . discard args
 40     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 41     # AL = convert lower nibble to hex
 42     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
 43     25/and-eax  0xf/imm32
 44     # . AL = to-hex-char(AL)
 45     e8/call  to-hex-char/disp32
 46     # append-byte(f, AL)
 47     # . . push args
 48     50/push-eax
 49     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 50     # . . call
 51     e8/call  append-byte/disp32
 52     # . . discard args
 53     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 54 $append-byte-hex:end:
 55     # . restore registers
 56     58/pop-to-eax
 57     # . epilogue
 58     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 59     5d/pop-to-ebp
 60     c3/return
 61 
 62 test-append-byte-hex:
 63     # - check that append-byte-hex adds the hex textual representation
 64     # setup
 65     # . clear-stream(_test-stream)
 66     # . . push args
 67     68/push  _test-stream/imm32
 68     # . . call
 69     e8/call  clear-stream/disp32
 70     # . . discard args
 71     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 72     # append-byte-hex(_test-stream, 0xa)  # exercises digit, non-digit as well as leading zero
 73     # . . push args
 74     68/push  0xa/imm32
 75     68/push  _test-stream/imm32
 76     # . . call
 77     e8/call  append-byte-hex/disp32
 78     # . . discard args
 79     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 80     # check-stream-equal(_test-stream, "0a", msg)
 81     # . . push args
 82     68/push  "F - test-append-byte-hex"/imm32
 83     68/push  "0a"/imm32
 84     68/push  _test-stream/imm32
 85     # . . call
 86     e8/call  check-stream-equal/disp32
 87     # . . discard args
 88     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 89     # . end
 90     c3/return
 91 
 92 # print the hex representation for the lowest byte of a number
 93 print-byte-buffered:  # f : (addr buffered-file), n : int
 94     # . prologue
 95     55/push-ebp
 96     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 97     # . save registers
 98     50/push-eax
 99     # AL = convert upper nibble to hex
100     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
101     c1/shift    5/subop/logic-right 3/mod/direct    0/rm32/eax    .           .             .           .           .               4/imm8            # shift eax right by 4 bits, while padding zeroes
102     25/and-eax  0xf/imm32
103     # . AL = to-hex-char(AL)
104     e8/call  to-hex-char/disp32
105     # write-byte-buffered(f, AL)
106     # . . push args
107     50/push-eax
108     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
109     # . . call
110     e8/call  write-byte-buffered/disp32
111     # . . discard args
112     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
113     # AL = convert lower nibble to hex
114     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
115     25/and-eax  0xf/imm32
116     # . AL = to-hex-char(AL)
117     e8/call  to-hex-char/disp32
118     # write-byte-buffered(f, AL)
119     # . . push args
120     50/push-eax
121     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
122     # . . call
123     e8/call  write-byte-buffered/disp32
124     # . . discard args
125     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
126 $print-byte-buffered:end:
127     # . restore registers
128     58/pop-to-eax
129     # . epilogue
130     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
131     5d/pop-to-ebp
132     c3/return
133 
134 test-print-byte-buffered:
135     # - check that print-byte-buffered prints the hex textual representation
136     # setup
137     # . clear-stream(_test-stream)
138     # . . push args
139     68/push  _test-stream/imm32
140     # . . call
141     e8/call  clear-stream/disp32
142     # . . discard args
143     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
144     # . clear-stream($_test-buffered-file->buffer)
145     # . . push args
146     68/push  $_test-buffered-file->buffer/imm32
147     # . . call
148     e8/call  clear-stream/disp32
149     # . . discard args
150     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
151     # print-byte-buffered(_test-buffered-file, 0xa)  # exercises digit, non-digit as well as leading zero
152     # . . push args
153     68/push  0xa/imm32
154     68/push  _test-buffered-file/imm32
155     # . . call
156     e8/call  print-byte-buffered/disp32
157     # . . discard args
158     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
159     # flush(_test-buffered-file)
160     # . . push args
161     68/push  _test-buffered-file/imm32
162     # . . call
163     e8/call  flush/disp32
164     # . . discard args
165     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
166     # check-stream-equal(_test-stream, "0a", msg)
167     # . . push args
168     68/push  "F - test-print-byte-buffered"/imm32
169     68/push  "0a"/imm32
170     68/push  _test-stream/imm32
171     # . . call
172     e8/call  check-stream-equal/disp32
173     # . . discard args
174     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
175     # . end
176     c3/return
177 
178 print-int32:  # f : (addr stream byte), n : int
179     # pseudocode:
180     #  write(f, "0x")
181     #  ecx = 28
182     #  while true
183     #    if (ecx < 0) break
184     #    eax = n >> ecx
185     #    eax = eax & 0xf
186     #    append-byte(f, AL)
187     #    ecx -= 4
188     #
189     # . prologue
190     55/push-ebp
191     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
192     # . save registers
193     50/push-eax
194     51/push-ecx
195     # ecx = 28
196     b9/copy-to-ecx  0x1c/imm32
197 $print-int32:print-hex-prefix:
198     # write(f, "0x")
199     # . . push args
200     68/push  "0x"/imm32
201     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
202     # . . call
203     e8/call  write/disp32
204     # . . discard args
205     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
206 $print-int32:loop:
207     # if (ecx < 0) break
208     81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0/imm32           # compare ecx
209     7c/jump-if-lesser  $print-int32:end/disp8
210     # eax = n >> ecx
211     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
212     d3/>>ecx    5/subop/pad-zeroes  3/mod/direct    0/rm32/eax    .           .             .           .           .               .                 # shift eax right by ecx bits, padding zeroes
213     # eax = to-hex-char(AL)
214     25/and-eax  0xf/imm32
215     e8/call  to-hex-char/disp32
216     # append-byte(f, AL)
217     # . . push args
218     50/push-eax
219     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
220     # . . call
221     e8/call  append-byte/disp32
222     # . . discard args
223     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
224     # ecx -= 4
225     81          5/subop/subtract    3/mod/direct    1/rm32/ecx    .           .             .           .           .               4/imm32           # subtract from ecx
226     eb/jump  $print-int32:loop/disp8
227 $print-int32:end:
228     # . restore registers
229     59/pop-to-ecx
230     58/pop-to-eax
231     # . epilogue
232     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
233     5d/pop-to-ebp
234     c3/return
235 
236 test-print-int32:
237     # - check that print-int32 prints the hex textual representation
238     # setup
239     # . clear-stream(_test-stream)
240     # . . push args
241     68/push  _test-stream/imm32
242     # . . call
243     e8/call  clear-stream/disp32
244     # . . discard args
245     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
246     # print-int32(_test-stream, 0x8899aa)
247     # . . push args
248     68/push  0x8899aa/imm32
249     68/push  _test-stream/imm32
250     # . . call
251     e8/call  print-int32/disp32
252     # . . discard args
253     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
254     # check-stream-equal(_test-stream, "0x008899aa", msg)
255     # . . push args
256     68/push  "F - test-print-int32"/imm32
257     68/push  "0x008899aa"/imm32
258     68/push  _test-stream/imm32
259     # . . call
260     e8/call  check-stream-equal/disp32
261     # . . discard args
262     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
263     # . end
264     c3/return
265 
266 print-int32-buffered:  # f : (addr buffered-file), n : int
267     # pseudocode:
268     #  write-buffered(f, "0x")
269     #  ecx = 28
270     #  while true
271     #    if (ecx < 0) break
272     #    eax = n >> ecx
273     #    eax = eax & 0xf
274     #    write-byte-buffered(f, AL)
275     #    ecx -= 4
276     #
277     # . prologue
278     55/push-ebp
279     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
280     # . save registers
281     50/push-eax
282     51/push-ecx
283     # ecx = 28
284     b9/copy-to-ecx  0x1c/imm32
285 $print-int32-buffered:print-hex-prefix:
286     # write-buffered(f, "0x")
287     # . . push args
288     68/push  "0x"/imm32
289     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
290     # . . call
291     e8/call  write-buffered/disp32
292     # . . discard args
293     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
294 $print-int32-buffered:loop:
295     # if (ecx < 0) break
296     81          7/subop/compare     3/mod/direct    1/rm32/ecx    .           .             .           .           .               0/imm32           # compare ecx
297     7c/jump-if-lesser  $print-int32-buffered:end/disp8
298     # eax = n >> ecx
299     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
300     d3/>>ecx    5/subop/pad-zeroes  3/mod/direct    0/rm32/eax    .           .             .           .           .               .                 # shift eax right by ecx bits, padding zeroes
301     # eax = to-hex-char(AL)
302     25/and-eax  0xf/imm32
303     e8/call  to-hex-char/disp32
304     # write-byte-buffered(f, AL)
305     # . . push args
306     50/push-eax
307     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
308     # . . call
309     e8/call  write-byte-buffered/disp32
310     # . . discard args
311     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
312     # ecx -= 4
313     81          5/subop/subtract    3/mod/direct    1/rm32/ecx    .           .             .           .           .               4/imm32           # subtract from ecx
314     eb/jump  $print-int32-buffered:loop/disp8
315 $print-int32-buffered:end:
316     # . restore registers
317     59/pop-to-ecx
318     58/pop-to-eax
319     # . epilogue
320     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
321     5d/pop-to-ebp
322     c3/return
323 
324 test-print-int32-buffered:
325     # - check that print-int32-buffered prints the hex textual representation
326     # setup
327     # . clear-stream(_test-stream)
328     # . . push args
329     68/push  _test-stream/imm32
330     # . . call
331     e8/call  clear-stream/disp32
332     # . . discard args
333     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
334     # . clear-stream($_test-buffered-file->buffer)
335     # . . push args
336     68/push  $_test-buffered-file->buffer/imm32
337     # . . call
338     e8/call  clear-stream/disp32
339     # . . discard args
340     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
341     # print-int32-buffered(_test-buffered-file, 0x8899aa)
342     # . . push args
343     68/push  0x8899aa/imm32
344     68/push  _test-buffered-file/imm32
345     # . . call
346     e8/call  print-int32-buffered/disp32
347     # . . discard args
348     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
349     # flush(_test-buffered-file)
350     # . . push args
351     68/push  _test-buffered-file/imm32
352     # . . call
353     e8/call  flush/disp32
354     # . . discard args
355     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
356 +-- 18 lines: #?     # dump line -----------------------------------------------------------------------------------------------------------------------------
374     # check-stream-equal(_test-stream, "0x008899aa", msg)
375     # . . push args
376     68/push  "F - test-print-int32-buffered"/imm32
377     68/push  "0x008899aa"/imm32
378     68/push  _test-stream/imm32
379     # . . call
380     e8/call  check-stream-equal/disp32
381     # . . discard args
382     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
383     # . end
384     c3/return
385 
386 # . . vim:nowrap:textwidth=0