# Mu - subx/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 (from the subx directory):
     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
    10 # instruction                     effective address                                                   operand     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   # syscall(mmap, 0x1000)
    15   bb/copy-to-EBX  mmap-new-segment/imm32
    16   b8/copy-to-EAX  0x5a/imm32/mmap
    17   cd/syscall  0x80/imm8
    18
    19   # store to *EAX
    20   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0x34/imm32              # copy to *EAX
    21
    22   # syscall(exit, EAX)
    23   89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                       # copy EAX to EBX
    24   b8/copy-to-EAX  1/imm32/exit
    25   cd/syscall  0x80/imm8
    26
    27 == data
    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   00 00 00 00  # null
    32   # len
    33   00 01 00 00  # 0x1000
    34   # protection flags
    35   03 00 00 00  # PROT_READ | PROT_WRITE
    36   # sharing flags
    37   22 00 00 00  # MAP_PRIVATE | MAP_ANONYMOUS
    38   # fd
    39   ff ff ff ff  # -1 since MAP_ANONYMOUS is specified
    40   # offset
    41   00 00 00 00  # 0 since MAP_ANONYMOUS is specified
    42
    43 # vim:nowrap:textwidth=0