https://github.com/akkartik/mu/blob/master/076zero-out.subx
 1 # Fill a region of memory with zeroes.
 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 zero-out:  # start : address, len : int
 9     # pseudocode:
10     #   curr/esi = start
11     #   i/ecx = 0
12     #   while true
13     #     if (i >= len) break
14     #     *curr = 0
15     #     ++curr
16     #     ++i
17     #
18     # . prolog
19     55/push-ebp
20     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
21     # . save registers
22     50/push-eax
23     51/push-ecx
24     52/push-edx
25     56/push-esi
26     # curr/esi = start
27     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
28     # i/ecx = 0
29     31/xor                          3/mod/direct    1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # clear ecx
30     # edx = len
31     8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           2/r32/edx   0xc/disp8       .                 # copy *(ebp+12) to edx
32 $zero-out:loop:
33     # if (i >= len) break
34     39/compare                      3/mod/direct    1/rm32/ecx    .           .             .           2/r32/edx   .               .                 # compare ecx with edx
35     7d/jump-if-greater-or-equal  $zero-out:end/disp8
36     # *curr = 0
37     c6          0/subop/copy        0/mod/direct    6/rm32/esi    .           .             .           .           .               0/imm8            # copy byte to *esi
38     # ++curr
39     46/increment-esi
40     # ++i
41     41/increment-ecx
42     eb/jump  $zero-out:loop/disp8
43 $zero-out:end:
44     # . restore registers
45     5e/pop-to-esi
46     5a/pop-to-edx
47     59/pop-to-ecx
48     58/pop-to-eax
49     # . epilog
50     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
51     5d/pop-to-ebp
52     c3/return
53 
54 test-zero-out:
55     # . prolog
56     55/push-ebp
57     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
58     # region/ecx = 34, 35, 36, 37
59     68/push  0x37363534/imm32
60     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
61     # zero-out(ecx, 3)
62     # . . push args
63     68/push  3/imm32/len
64     51/push-ecx
65     # . . call
66     e8/call  zero-out/disp32
67     # . . discard args
68     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
69     # first 3 bytes cleared, fourth left alone
70     # . check-ints-equal(*ecx, 0x37000000, msg)
71     # . . push args
72     68/push  "F - test-zero-out"/imm32
73     68/push  0x37000000/imm32
74     ff          6/subop/push        0/mod/indirect  1/rm32/ecx    .           .             .           .           .               .                 # push *ecx
75     # . . call
76     e8/call  check-ints-equal/disp32
77     # . . discard args
78     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
79     # . epilog
80     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
81     5d/pop-to-ebp
82     c3/return
83 
84 # . . vim:nowrap:textwidth=0