https://github.com/akkartik/mu/blob/master/091write-int.subx
  1 # write-int: add a single int to a stream
  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 write-int:  # out : (addr stream byte), n : int
  9     # . prologue
 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     51/push-ecx
 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     # ecx = out->write
 19     8b/copy                         0/mod/indirect  7/rm32/edi    .           .             .           1/r32/ecx   .               .                 # copy *edi to ecx
 20     # if (out->write >= out->length) abort
 21     3b/compare                      1/mod/*+disp8   7/rm32/edi    .           .             .           1/r32/ecx   8/disp8         .                 # compare ecx with *(edi+8)
 22     7d/jump-if->=  $write-int:abort/disp8
 23 $write-int:to-stream:
 24     # out->data[out->write] = n
 25     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   0xc/disp8       .                 # copy *(ebp+12) to eax
 26     89/copy                         1/mod/*+disp8   4/rm32/sib    7/base/edi  1/index/ecx   .           0/r32/eax   0xc/disp8       .                 # copy eax to *(edi+ecx+12)
 27     # out->write += 4
 28     81          0/subop/add         0/mod/indirect  7/rm32/edi    .           .             .           .           .               4/imm32           # add to *edi
 29 $write-int:end:
 30     # . restore registers
 31     5f/pop-to-edi
 32     59/pop-to-ecx
 33     58/pop-to-eax
 34     # . epilogue
 35     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
 36     5d/pop-to-ebp
 37     c3/return
 38 
 39 $write-int:abort:
 40     # . _write(2/stderr, error)
 41     # . . push args
 42     68/push  "write-int: out of space\n"/imm32
 43     68/push  2/imm32/stderr
 44     # . . call
 45     e8/call  _write/disp32
 46     # . . discard args
 47     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 48     # . syscall(exit, 1)
 49     bb/copy-to-ebx  1/imm32
 50     b8/copy-to-eax  1/imm32/exit
 51     cd/syscall  0x80/imm8
 52     # never gets here
 53 
 54 test-write-int-single:
 55     # - check that write-int writes to first int of 'stream'
 56     # setup
 57     # . clear-stream(_test-stream)
 58     # . . push args
 59     68/push  _test-stream/imm32
 60     # . . call
 61     e8/call  clear-stream/disp32
 62     # . . discard args
 63     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 64     # write-int(_test-stream, "abcd")
 65     # . . push args
 66     68/push  0x64636261/imm32
 67     68/push  _test-stream/imm32
 68     # . . call
 69     e8/call  write-int/disp32
 70     # . . discard args
 71     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 72     # check-stream-equal(_test-stream, "abcd", msg)
 73     # . . push args
 74     68/push  "F - test-write-int-single"/imm32
 75     68/push  "abcd"/imm32
 76     68/push  _test-stream/imm32
 77     # . . call
 78     e8/call  check-stream-equal/disp32
 79     # . . discard args
 80     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 81     # . end
 82     c3/return
 83 
 84 test-write-byte-buffered-multiple:
 85     # - check that write-int correctly appends multiple writes
 86     # setup
 87     # . clear-stream(_test-stream)
 88     # . . push args
 89     68/push  _test-stream/imm32
 90     # . . call
 91     e8/call  clear-stream/disp32
 92     # . . discard args
 93     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 94     # write-int(_test-stream, "abcd")
 95     # . . push args
 96     68/push  0x64636261/imm32
 97     68/push  _test-stream/imm32
 98     # . . call
 99     e8/call  write-int/disp32
100     # . . discard args
101     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
102     # write-int(_test-stream, "efgh")
103     # . . push args
104     68/push  0x68676665/imm32
105     68/push  _test-stream/imm32
106     # . . call
107     e8/call  write-int/disp32
108     # . . discard args
109     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
110     # check-stream-equal(_test-stream, "abcdefgh", msg)
111     # . . push args
112     68/push  "F - test-write-byte-buffered-multiple"/imm32
113     68/push  "abcdefgh"/imm32
114     68/push  _test-stream/imm32
115     # . . call
116     e8/call  check-stream-equal/disp32
117     # . . discard args
118     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
119     # . end
120     c3/return
121 
122 # . . vim:nowrap:textwidth=0