https://github.com/akkartik/mu/blob/master/subx/066print-byte.subx
 1 # Print the (hex) textual representation of the lowest byte of a number.
 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 print-byte:  # f : (address buffered-file), n : int -> <void>
 9     # . prolog
10     55/push-EBP
11     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
12     # . save registers
13     50/push-EAX
14     # AL = convert upper nibble to hex
15     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
16     c1/shift    5/subop/logic-right 3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm8            # shift EAX right by 4 bits, while padding zeroes
17     25/and-EAX  0xf/imm32
18     # . AL = to-hex-char(AL)
19     e8/call  to-hex-char/disp32
20     # write-byte(f, AL)
21     # . . push args
22     50/push-EAX
23     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
24     # . . call
25     e8/call  write-byte/disp32
26     # . . discard args
27     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
28     # AL = convert lower nibble to hex
29     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
30     25/and-EAX  0xf/imm32
31     # . AL = to-hex-char(AL)
32     e8/call  to-hex-char/disp32
33     # write-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  write-byte/disp32
39     # . . discard args
40     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
41 $print-byte:end:
42     # . restore registers
43     58/pop-to-EAX
44     # . epilog
45     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
46     5d/pop-to-EBP
47     c3/return
48 
49 test-print-byte:
50     # - check that print-byte prints the hex textual representation
51     # setup
52     # . clear-stream(_test-stream)
53     # . . push args
54     68/push  _test-stream/imm32
55     # . . call
56     e8/call  clear-stream/disp32
57     # . . discard args
58     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
59     # . clear-stream(_test-buffered-file+4)
60     # . . push args
61     b8/copy-to-EAX  _test-buffered-file/imm32
62     05/add-to-EAX  4/imm32
63     50/push-EAX
64     # . . call
65     e8/call  clear-stream/disp32
66     # . . discard args
67     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
68     # print-byte(_test-buffered-file, 0xa)  # exercises digit, non-digit as well as leading zero
69     # . . push args
70     68/push  0xa/imm32
71     68/push  _test-buffered-file/imm32
72     # . . call
73     e8/call  print-byte/disp32
74     # . . discard args
75     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
76     # flush(_test-buffered-file)
77     # . . push args
78     68/push  _test-buffered-file/imm32
79     # . . call
80     e8/call  flush/disp32
81     # . . discard args
82     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
83     # check-stream-equal(_test-stream, "0a", msg)
84     # . . push args
85     68/push  "F - test-print-byte"/imm32
86     68/push  "0a"/imm32
87     68/push  _test-stream/imm32
88     # . . call
89     e8/call  check-stream-equal/disp32
90     # . . discard args
91     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
92     # . end
93     c3/return
94 
95 # . . vim:nowrap:textwidth=0