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