https://github.com/akkartik/mu/blob/main/linux/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 #   $ bootstrap/bootstrap translate ex12.subx -o ex12
 6 #   $ bootstrap/bootstrap run ex12
 7 # You shouldn't get a segmentation fault.
 8 
 9 == code
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     # mmap(Mmap-new-segment->len)
16     bb/copy-to-ebx  Mmap-new-segment/imm32
17     e8/call  syscall_mmap/disp32
18 
19     # write to *eax to check that we have access to the newly-allocated segment
20     c7          0/subop/copy        0/mod/direct    0/rm32/eax    .           .             .           .           .               0x34/imm32        # copy to *eax
21 
22     # exit(eax)
23     89/copy                         3/mod/direct    3/rm32/ebx    .           .             .           0/r32/eax   .               .                 # copy eax to ebx
24     e8/call  syscall_exit/disp32
25 
26 == data
27 
28 # various constants used here were found in the Linux sources (search for file mman-common.h)
29 Mmap-new-segment:  # type mmap_arg_struct
30     # addr
31     0/imm32
32     # len
33     0x100/imm32
34     # protection flags
35     3/imm32  # PROT_READ | PROT_WRITE
36     # sharing flags
37     0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
38     # fd
39     -1/imm32  # since MAP_ANONYMOUS is specified
40     # offset
41     0/imm32  # since MAP_ANONYMOUS is specified
42 
43 # . . vim:nowrap:textwidth=0