https://github.com/akkartik/mu/blob/master/examples/ex12.subx
 1 # Example showing mmap syscall.
 2 # Create a new segment using mmap, save the address, write to it.
 3 #
 4 # To run:
 5 #   $ ./subx translate examples/ex12.subx -o examples/ex12
 6 #   $ ./subx run examples/ex12
 7 # You shouldn't get a segmentation fault.
 8 
 9 == code 0x09000000
10 #   instruction                     effective address                                                   register    displacement    immediate
11 # . op          subop               mod             rm32          base        index         scale       r32
12 # . 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
13 
14 Entry:
15     # syscall(mmap, 0x1000)
16     bb/copy-to-ebx  Mmap-new-segment/imm32
17     b8/copy-to-eax  0x5a/imm32/mmap
18     cd/syscall  0x80/imm8
19 
20     # write to *eax to check that we have access to the newly-allocated segment
21     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0x34/imm32        # copy to *eax
22 
23     # syscall(exit, eax)
24     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
25     b8/copy-to-eax  1/imm32/exit
26     cd/syscall  0x80/imm8
27 
28 == data 0x0a000000
29 
30 # various constants used here were found in the Linux sources (search for file mman-common.h)
31 Mmap-new-segment:  # type mmap_arg_struct
32     # addr
33     0/imm32
34     # len
35     0x100/imm32
36     # protection flags
37     3/imm32  # PROT_READ | PROT_WRITE
38     # sharing flags
39     0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
40     # fd
41     -1/imm32  # since MAP_ANONYMOUS is specified
42     # offset
43     0/imm32  # since MAP_ANONYMOUS is specified
44 
45 # . . vim:nowrap:textwidth=0