https://github.com/akkartik/mu/blob/master/202write-int.subx
  1 # write-int: add (the binary representation of) 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->size) 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     e8/call  syscall_exit/disp32
 51     # never gets here
 52 
 53 test-write-int-single:
 54     # - check that write-int writes to first int of 'stream'
 55     # setup
 56     # . clear-stream(_test-stream)
 57     # . . push args
 58     68/push  _test-stream/imm32
 59     # . . call
 60     e8/call  clear-stream/disp32
 61     # . . discard args
 62     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 63     # write-int(_test-stream, "abcd")
 64     # . . push args
 65     68/push  0x64636261/imm32
 66     68/push  _test-stream/imm32
 67     # . . call
 68     e8/call  write-int/disp32
 69     # . . discard args
 70     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 71     # check-stream-equal(_test-stream, "abcd", msg)
 72     # . . push args
 73     68/push  "F - test-write-int-single"/imm32
 74     68/push  "abcd"/imm32
 75     68/push  _test-stream/imm32
 76     # . . call
 77     e8/call  check-stream-equal/disp32
 78     # . . discard args
 79     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
 80     # . end
 81     c3/return
 82 
 83 test-write-byte-buffered-multiple:
 84     # - check that write-int correctly appends multiple writes
 85     # setup
 86     # . clear-stream(_test-stream)
 87     # . . push args
 88     68/push  _test-stream/imm32
 89     # . . call
 90     e8/call  clear-stream/disp32
 91     # . . discard args
 92     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 93     # write-int(_test-stream, "abcd")
 94     # . . push args
 95     68/push  0x64636261/imm32
 96     68/push  _test-stream/imm32
 97     # . . call
 98     e8/call  write-int/disp32
 99     # . . discard args
100     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
101     # write-int(_test-stream, "efgh")
102     # . . push args
103     68/push  0x68676665/imm32
104     68/push  _test-stream/imm32
105     # . . call
106     e8/call  write-int/disp32
107     # . . discard args
108     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
109     # check-stream-equal(_test-stream, "abcdefgh", msg)
110     # . . push args
111     68/push  "F - test-write-byte-buffered-multiple"/imm32
112     68/push  "abcdefgh"/imm32
113     68/push  _test-stream/imm32
114     # . . call
115     e8/call  check-stream-equal/disp32
116     # . . discard args
117     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
118     # . end
119     c3/return
120 
121 # . . vim:nowrap:textwidth=0