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