https://github.com/akkartik/mu/blob/main/linux/134emit-hex-array.subx
  1 == code
  2 #   instruction                     effective address                                                   register    displacement    immediate
  3 # . op          subop               mod             rm32          base        index         scale       r32
  4 # . 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
  5 
  6 # print 'arr' in hex with a space after every byte
  7 emit-hex-array:  # out: (addr buffered-file), arr: (addr array byte)
  8     # . prologue
  9     55/push-ebp
 10     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 11     # . save registers
 12     50/push-eax
 13     51/push-ecx
 14     52/push-edx
 15     57/push-edi
 16     # edi = out
 17     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           7/r32/edi   8/disp8         .                 # copy *(ebp+8) to edi
 18     # edx = arr
 19     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
 20     # var curr/ecx: (addr byte) = arr->data
 21     8d/copy-address                 1/mod/*+disp8   2/rm32/edx    .           .             .           1/r32/ecx   4/disp8         .                 # copy edx+4 to ecx
 22     # var max/edx: (addr byte) = &arr->data[arr->size]
 23     8b/copy                         0/mod/indirect  2/rm32/edx    .           .             .           2/r32/edx   .               .                 # copy *edx to edx
 24     01/add                          3/mod/direct    2/rm32/edx    .           .             .           1/r32/ecx   .               .                 # add ecx to edx
 25     # var c/eax: byte = 0
 26     31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
 27 $emit-hex-array:loop:
 28     # if (curr >= width) break
 29     39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           2/r32/edx   .               .                 # compare ecx with edx
 30     73/jump-if-addr>=  $emit-hex-array:end/disp8
 31     # emit-hex(out, c = *curr, width=1)
 32     # . . push args
 33     68/push  1/imm32/width
 34     8a/copy-byte                    0/mod/indirect  1/rm32/ecx    .           .             .           0/r32/AL    .               .                 # copy byte at *ecx to AL
 35     50/push-eax
 36     57/push-edi
 37     # . . call
 38     e8/call  emit-hex/disp32
 39     # . . discard args
 40     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 41     # ++curr
 42     41/increment-ecx
 43     eb/jump  $emit-hex-array:loop/disp8
 44 $emit-hex-array:end:
 45     # . restore registers
 46     5f/pop-to-edi
 47     5a/pop-to-edx
 48     59/pop-to-ecx
 49     58/pop-to-eax
 50     # . epilogue
 51     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 52     5d/pop-to-ebp
 53     c3/return
 54 
 55 test-emit-hex-array:
 56     # . prologue
 57     55/push-ebp
 58     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 59     # setup
 60     # . clear-stream(_test-output-stream)
 61     # . . push args
 62     68/push  _test-output-stream/imm32
 63     # . . call
 64     e8/call  clear-stream/disp32
 65     # . . discard args
 66     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 67     # . clear-stream($_test-output-buffered-file->buffer)
 68     # . . push args
 69     68/push  $_test-output-buffered-file->buffer/imm32
 70     # . . call
 71     e8/call  clear-stream/disp32
 72     # . . discard args
 73     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 74     # var arr/ecx (array byte) = [01, 02, 03]
 75     68/push  0x00030201/imm32  # bytes 01 02 03
 76     68/push  3/imm32/size
 77     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
 78     # emit-hex-array(_test-output-buffered-file, arr)
 79     # . . push args
 80     51/push-ecx
 81     68/push  _test-output-buffered-file/imm32
 82     # . . call
 83     e8/call  emit-hex-array/disp32
 84     # . . discard args
 85     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 86     # . flush(_test-output-buffered-file)
 87     # . . push args
 88     68/push  _test-output-buffered-file/imm32
 89     # . . call
 90     e8/call  flush/disp32
 91     # . . discard args
 92     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 93 +-- 33 lines: #?     # dump output ------------------------------------------------------------------------------------------------------------------------------------------------------
126     # check-next-stream-line-equal(_test-output-stream, "01 02 03 ", msg)
127     # . . push args
128     68/push  "F - test-emit-hex-array"/imm32
129     68/push  "01 02 03 "/imm32
130     68/push  _test-output-stream/imm32
131     # . . call
132     e8/call  check-next-stream-line-equal/disp32
133     # . . discard args
134     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
135     # . epilogue
136     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
137     5d/pop-to-ebp
138     c3/return
139 
140 # . . vim:nowrap:textwidth=0