https://github.com/akkartik/mu/blob/master/subx/053new-segment.subx
 1 # Create a new segment (for data) using mmap().
 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 Entry:   # manual test
 9     # EAX = new-segment(0x1000)
10     # . . push args
11     68/push  0x1000/imm32
12     # . . call
13     e8/call  new-segment/disp32
14     # . . discard args
15     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
16 
17     # write to *EAX to check that we have access to the newly-allocated segment
18     c7          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0x34/imm32        # copy to *EAX
19 
20     # syscall(exit, EAX)
21     89/copy                         3/mod/direct    3/rm32/EBX    .           .             .           0/r32/EAX   .               .                 # copy EAX to EBX
22     b8/copy-to-EAX  1/imm32/exit
23     cd/syscall  0x80/imm8
24 
25 new-segment:  # len : int -> address
26     # . prolog
27     55/push-EBP
28     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
29     53/push-EBX
30     # copy len to _mmap-new-segment->len
31     # TODO: compute _mmap-new-segment+4 before runtime
32     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
33     bb/copy-to-EBX  _mmap-new-segment/imm32
34     89/copy                         1/mod/*+disp8   3/rm32/EBX    .           .             .           0/r32/EAX   4/disp8         .                 # copy EAX to *(EBX+4)
35     # mmap(_mmap-new-segment)
36     bb/copy-to-EBX  _mmap-new-segment/imm32
37     b8/copy-to-EAX  0x5a/imm32/mmap
38     cd/syscall  0x80/imm8
39 $new-segment:end:
40     # . epilog
41     5b/pop-to-EBX
42     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
43     5d/pop-to-EBP
44     c3/return
45 
46 == data
47 
48 # various constants used here were found in the Linux sources (search for file mman-common.h)
49 _mmap-new-segment:  # type mmap_arg_struct
50     # addr
51     0/imm32
52     # len
53     0/imm32
54     # protection flags
55     3/imm32  # PROT_READ | PROT_WRITE
56     # sharing flags
57     0x22/imm32  # MAP_PRIVATE | MAP_ANONYMOUS
58     # fd
59     -1/imm32  # since MAP_ANONYMOUS is specified
60     # offset
61     0/imm32  # since MAP_ANONYMOUS is specified
62 
63 # . . vim:nowrap:textwidth=0