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