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
 13     cd/syscall  0x80/imm8
 14 
 15 print-byte:  # f : (address buffered-file), n : num -> <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   4/rm32/sib    5/base/EBP  4/index/none  .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/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   4/rm32/sib    5/base/EBP  4/index/none  .           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   4/rm32/sib    5/base/EBP  4/index/none  .           .           0x8/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     # . restore registers
 58     59/pop-to-ECX
 59     58/pop-to-EAX
 60     # . epilog
 61     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 62     5d/pop-to-EBP
 63     c3/return
 64 
 65 # extract lowest 4 bits and convert to 8-byte ascii
 66 # return 0xffffffff if more than 4 bits are set
 67 hex-char:  # n : num -> char_or_error/EAX
 68     # . prolog
 69     55/push-EBP
 70     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 71     # EAX = n
 72     8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
 73     # if it's <= 9 add '0' to it
 74     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0x9/imm32         # compare EAX
 75     7f/jump-if-greater  $hex-char:check2/disp8
 76     05/add-to-EAX  0x30/imm32/'0'
 77     eb/jump  $hex-char:end/disp8
 78 $hex-char:check2:
 79     # else if it's <= 15 add ('a' - 10) to it
 80     81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xf/imm32         # compare EAX
 81     7f/jump-if-greater  $hex-char:error/disp8
 82     05/add-to-EAX  0x57/imm32  # 'a' - 10
 83     eb/jump  $hex-char:end/disp8
 84 $hex-char:error:
 85     # otherwise return 0xffffffff
 86     b8/copy-to-EAX  0xffffffff/imm32
 87 $hex-char:end:
 88     # . epilog
 89     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 90     5d/pop-to-EBP
 91     c3/return
 92 
 93 test-print-byte:
 94     # - check that print-byte prints the hex textual representation
 95     # setup
 96     # . clear-stream(_test-stream)
 97     # . . push args
 98     68/push  _test-stream/imm32
 99     # . . call
100     e8/call  clear-stream/disp32
101     # . . discard args
102     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
103     # . clear-stream(_test-buffered-file+4)
104     # . . push args
105     b8/copy-to-EAX  _test-buffered-file/imm32
106     05/add-to-EAX  4/imm32
107     50/push-EAX
108     # . . call
109     e8/call  clear-stream/disp32
110     # . . discard args
111     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
112     # print-byte(_test-buffered-file, 0xa)  # exercises digit, non-digit as well as leading zero
113     # . . push args
114     68/push  0xa/imm32
115     68/push  _test-buffered-file/imm32
116     # . . call
117     e8/call  print-byte/disp32
118     # . . discard args
119     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
120     # flush(_test-buffered-file)
121     # . . push args
122     68/push  _test-buffered-file/imm32
123     # . . call
124     e8/call  flush/disp32
125     # . . discard args
126     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
127     # check-ints-equal(*_test-stream->data, '0a', msg)
128     # . . push args
129     68/push  "F - test-print-byte"/imm32
130     68/push  0x6130/imm32/0a
131     # . . push *_test-stream->data
132     b8/copy-to-EAX  _test-stream/imm32
133     ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
134     # . . call
135     e8/call  check-ints-equal/disp32
136     # . . discard args
137     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
138     # . end
139     c3/return
140 
141 # . . vim:nowrap:textwidth=0