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 # 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     25/and-EAX  0xf/imm32
 25     # . AL = to-hex-char(AL)
 26     e8/call  to-hex-char/disp32
 27     # write-byte(f, AL)
 28     # . . push args
 29     50/push-EAX
 30     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 31     # . . call
 32     e8/call  write-byte/disp32
 33     # . . discard args
 34     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 35     # AL = convert lower nibble to hex
 36     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   0xc/disp8       .                 # copy *(EBP+12) to EAX
 37     25/and-EAX  0xf/imm32
 38     # . AL = to-hex-char(AL)
 39     e8/call  to-hex-char/disp32
 40     # write-byte(f, AL)
 41     # . . push args
 42     50/push-EAX
 43     ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
 44     # . . call
 45     e8/call  write-byte/disp32
 46     # . . discard args
 47     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 48 $print-byte:end:
 49     # . restore registers
 50     58/pop-to-EAX
 51     # . epilog
 52     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
 53     5d/pop-to-EBP
 54     c3/return
 55 
 56 test-print-byte:
 57     # - check that print-byte prints the hex textual representation
 58     # setup
 59     # . clear-stream(_test-stream)
 60     # . . push args
 61     68/push  _test-stream/imm32
 62     # . . call
 63     e8/call  clear-stream/disp32
 64     # . . discard args
 65     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 66     # . clear-stream(_test-buffered-file+4)
 67     # . . push args
 68     b8/copy-to-EAX  _test-buffered-file/imm32
 69     05/add-to-EAX  4/imm32
 70     50/push-EAX
 71     # . . call
 72     e8/call  clear-stream/disp32
 73     # . . discard args
 74     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 75     # print-byte(_test-buffered-file, 0xa)  # exercises digit, non-digit as well as leading zero
 76     # . . push args
 77     68/push  0xa/imm32
 78     68/push  _test-buffered-file/imm32
 79     # . . call
 80     e8/call  print-byte/disp32
 81     # . . discard args
 82     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
 83     # flush(_test-buffered-file)
 84     # . . push args
 85     68/push  _test-buffered-file/imm32
 86     # . . call
 87     e8/call  flush/disp32
 88     # . . discard args
 89     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 90     # check-stream-equal(_test-stream, "0a", msg)
 91     # . . push args
 92     68/push  "F - test-print-byte"/imm32
 93     68/push  "0a"/imm32
 94     68/push  _test-stream/imm32
 95     # . . call
 96     e8/call  check-stream-equal/disp32
 97     # . . discard args
 98     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
 99     # . end
100     c3/return
101 
102 # . . vim:nowrap:textwidth=0