https://github.com/akkartik/mu/blob/master/subx/063print-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 # main:
  9     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 10     # syscall(exit, Num-test-failures)
 11     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 12     b8/copy-to-EAX  1/imm32/exit
 13     cd/syscall  0x80/imm8
 14 
 15 print-byte:  # f : (address buffered-file), n : int -> <void>
 16     # . prolog
 17     55/push-EBP
 18     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 19     # . save registers
 20     50/push-EAX
 21     # AL = convert upper nibble to hex
 22     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 23     c1/shift    5/subop/logic-right 3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm8            # shift EAX right by 4 bits, while padding zeroes
 24     # . hex-char(AL)
 25     # . . push args
 26     50/push-EAX
 27     # . . call
 28     e8/call  hex-char/disp32
 29     # . . discard args
 30     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 31     # write-byte(f, AL)
 32     # . . push args
 33     50/push-EAX
 34     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 35     # . . call
 36     e8/call  write-byte/disp32
 37     # . . discard args
 38     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 39     # AL = convert lower nibble to hex
 40     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 41     25/and-EAX  0xf/imm32
 42     # . hex-char(AL)
 43     # . . push args
 44     50/push-EAX
 45     # . . call
 46     e8/call  hex-char/disp32
 47     # . . discard args
 48     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 49     # write-byte(f, AL)
 50     # . . push args
 51     50/push-EAX
 52     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 53     # . . call
 54     e8/call  write-byte/disp32
 55     # . . discard args
 56     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 57 $print-byte:end:
 58     # . restore registers
 59     59/pop-to-ECX
 60     58/pop-to-EAX
 61     # . epilog
 62     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 63     5d/pop-to-EBP
 64     c3/return
 65 
 66 # extract lowest 4 bits and convert to 8-byte ascii
 67 # return 0xffffffff if more than 4 bits are set
 68 hex-char:  # n : int -> char_or_error/EAX
 69     # . prolog
 70     55/push-EBP
 71     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 72     # EAX = n
 73     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
 74     # if it's <= 9 add '0' to it
 75     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0x9/imm32         # compare EAX
 76     7f/jump-if-greater  $hex-char:check2/disp8
 77     05/add-to-EAX  0x30/imm32/'0'
 78     eb/jump  $hex-char:end/disp8
 79 $hex-char:check2:
 80     # else if it's <= 15 add ('a' - 10) to it
 81     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xf/imm32         # compare EAX
 82     7f/jump-if-greater  $hex-char:error/disp8
 83     05/add-to-EAX  0x57/imm32  # 'a' - 10
 84     eb/jump  $hex-char:end/disp8
 85 $hex-char:error:
 86     # otherwise return 0xffffffff
 87     b8/copy-to-EAX  0xffffffff/imm32
 88 $hex-char:end:
 89     # . epilog
 90     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 91     5d/pop-to-EBP
 92     c3/return
 93 
 94 test-print-byte:
 95     # - check that print-byte prints the hex textual representation
 96     # setup
 97     # . clear-stream(_test-stream)
 98     # . . push args
 99     68/push  _test-stream/imm32
100     # . . call
101     e8/call  clear-stream/disp32
102     # . . discard args
103     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
104     # . clear-stream(_test-buffered-file+4)
105     # . . push args
106     b8/copy-to-EAX  _test-buffered-file/imm32
107     05/add-to-EAX  4/imm32
108     50/push-EAX
109     # . . call
110     e8/call  clear-stream/disp32
111     # . . discard args
112     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
113     # print-byte(_test-buffered-file, 0xa)  # exercises digit, non-digit as well as leading zero
114     # . . push args
115     68/push  0xa/imm32
116     68/push  _test-buffered-file/imm32
117     # . . call
118     e8/call  print-byte/disp32
119     # . . discard args
120     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
121     # flush(_test-buffered-file)
122     # . . push args
123     68/push  _test-buffered-file/imm32
124     # . . call
125     e8/call  flush/disp32
126     # . . discard args
127     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
128     # check-ints-equal(*_test-stream->data, '0a', msg)
129     # . . push args
130     68/push  "F - test-print-byte"/imm32
131     68/push  0x6130/imm32/0a
132     # . . push *_test-stream->data
133     b8/copy-to-EAX  _test-stream/imm32
134     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
135     # . . call
136     e8/call  check-ints-equal/disp32
137     # . . discard args
138     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
139     # . end
140     c3/return
141 
142 # . . vim:nowrap:textwidth=0