Getting Started with x86 » History » Version 74
Alexander Kamkin, 04/06/2017 08:28 PM
1 | 18 | Alexander Kamkin | h1. Getting Started with x86 |
---|---|---|---|
2 | 1 | Mikhail Chupilko | |
3 | {{toc}} |
||
4 | |||
5 | h2. Prerequisite |
||
6 | |||
7 | 31 | Alexander Kamkin | MicroTESK should be [[Installation Guide|installed]]. |
8 | 1 | Mikhail Chupilko | |
9 | 13 | Alexander Kamkin | h2. Demo Specifications |
10 | 1 | Mikhail Chupilko | |
11 | 29 | Alexander Kamkin | Specifications of the x86 (8086) instruction set architecture (ISA) can be found in "$MICROTESK_HOME/arch/demo/x86/model/x86.nml":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/model/x86.nml. |
12 | 1 | Mikhail Chupilko | |
13 | 54 | Alexander Kamkin | Instruction are described in [[nML Language Reference|nML]] by means of the following constructs (_move r16/r16_ is taken as an example): |
14 | 8 | Alexander Kamkin | |
15 | 10 | Alexander Kamkin | ## the signature |
16 | 59 | Alexander Kamkin | <pre><code class="c">op mov_r16r16 (dst: R16, src: R16)</code></pre> |
17 | 10 | Alexander Kamkin | ## the assembly format |
18 | 50 | Alexander Protsenko | <pre><code class="c">syntax = format("mov %s, %s", dst.syntax, src.syntax)</code></pre> |
19 | 10 | Alexander Kamkin | ## the binary encoding |
20 | 50 | Alexander Protsenko | <pre><code class="c">image = format("1000101111%s%s", dst.image, src.image)</code></pre> |
21 | 14 | Alexander Kamkin | ## the semantics |
22 | 50 | Alexander Protsenko | <pre><code class="c"> |
23 | 1 | Mikhail Chupilko | action = { |
24 | dst = src; |
||
25 | 9 | Alexander Kamkin | ... |
26 | 1 | Mikhail Chupilko | } |
27 | 50 | Alexander Protsenko | </code></pre> |
28 | 24 | Alexander Kamkin | |
29 | 11 | Alexander Kamkin | To compile the ISA model, run the following command: |
30 | 47 | Alexander Protsenko | <pre>sh $MICROTESK_HOME/bin/compile.sh x86.nml</pre> |
31 | 1 | Mikhail Chupilko | |
32 | 17 | Alexander Kamkin | h2. Demo Templates |
33 | 1 | Mikhail Chupilko | |
34 | 57 | Alexander Kamkin | Test templates for the x86 (8086) ISA can be found in "$MICROTESK_HOME/arch/demo/x86/templates":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates. |
35 | 1 | Mikhail Chupilko | |
36 | 33 | Alexander Kamkin | The directory contains a number of demo templates including the following ones: |
37 | 17 | Alexander Kamkin | |
38 | 36 | Alexander Kamkin | {background:#f6fcff}. | "block.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/block.rb | demonstrates how to use block constructs | |
39 | 35 | Alexander Kamkin | | "block_random.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/block_random.rb | demonstrates how to create randomized instruction sequences using block constructs | |
40 | 38 | Alexander Kamkin | {background:#f6fcff}. | "euclid.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/euclid.rb | demonstrates test program simulation to predict the resulting microprocessor state | |
41 | 44 | Alexander Kamkin | | "random.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/random.rb | demonstrates how to randomize tests by using biases and distributions | |
42 | 40 | Alexander Kamkin | {background:#f6fcff}. | "random_immediate.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/random_immediate.rb | demonstrates how to randomize immediate values | |
43 | | "random_registers.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/random_registers.rb | demonstrates how to randomize registers (dependencies) | |
||
44 | 1 | Mikhail Chupilko | |
45 | 60 | Alexander Kamkin | Test templates are written in "Ruby":http://www.ruby-lang.org extended with specific [[Template_Description_Language|constructs]] (let us look at "block.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/block.rb): |
46 | 50 | Alexander Protsenko | |
47 | 67 | Alexander Kamkin | # the code in the beginning includes the "x86_base.rb":http://forge.ispras.ru/projects/microtesk/repository/entry/trunk/microtesk/src/main/arch/demo/x86/templates/x86_base.rb file where a base template @X86BaseTemplate@ is defined and declares a @BlockTemplate@ template: |
48 | 62 | Alexander Kamkin | <pre><code class="ruby"> |
49 | 1 | Mikhail Chupilko | require_relative ''x86_base'' |
50 | 63 | Alexander Kamkin | |
51 | 64 | Alexander Kamkin | class BlockTemplate < X86BaseTemplate # BlockTemplate is a heir of @X86BaseTemplate@ |
52 | 1 | Mikhail Chupilko | </code></pre> |
53 | 72 | Alexander Kamkin | # here is a template entry point: |
54 | 1 | Mikhail Chupilko | <pre><code class="ruby"> def run</code></pre> |
55 | 67 | Alexander Kamkin | # this block produces a sequence consisting of three instructions: |
56 | 48 | Mikhail Chupilko | <pre><code class="ruby"> |
57 | 66 | Alexander Kamkin | sequence { # sequence: {mov, sub, add} |
58 | 1 | Mikhail Chupilko | mov_r16r16 ax, bx |
59 | 66 | Alexander Kamkin | sub_r16r16 cx, dx # registers: fixed |
60 | add_r16r16 r16(_), r16(_) # registers: randomized |
||
61 | }.run |
||
62 | </code></pre> |
||
63 | 67 | Alexander Kamkin | # this block produces an atomic sequence consisting of three instructions (atomic sequences are not interrupted while being merged with other ones): |
64 | 66 | Alexander Kamkin | <pre><code class="ruby"> |
65 | 67 | Alexander Kamkin | atomic { # atomic sequence: {mov, add, sub} |
66 | 66 | Alexander Kamkin | mov_r16r16 ax, bx |
67 | add_r16r16 cx, dx |
||
68 | sub_r16r16 r16(_), r16(_) |
||
69 | 1 | Mikhail Chupilko | }.run |
70 | 48 | Mikhail Chupilko | </code></pre> |
71 | 67 | Alexander Kamkin | # this block produces three sequences each consisting of one instruction: |
72 | 1 | Mikhail Chupilko | <pre><code class="ruby"> |
73 | 67 | Alexander Kamkin | iterate { # sequences: {{mov}, {sub}, {add}} |
74 | 1 | Mikhail Chupilko | mov_r16r16 ax, bx |
75 | sub_r16r16 cx, dx |
||
76 | add_r16r16 r16(_), r16(_) |
||
77 | 48 | Mikhail Chupilko | }.run |
78 | </code></pre> |
||
79 | 69 | Alexander Kamkin | # this block produces four sequences each consisting of two instructions |
80 | 72 | Alexander Kamkin | ~the combinator constructs the Cartesian product of the nested sets of sequences; the compositor randomly merges each tuple of the product into one sequence:~ |
81 | 1 | Mikhail Chupilko | <pre><code class="ruby"> |
82 | 48 | Mikhail Chupilko | block(:combinator => ''product'', :compositor => ''random'') { |
83 | 68 | Alexander Kamkin | # combinator: {({sub}, {mov}), ({sub}, {sub}), ({add}, {mov}), ({add}, {sub})} |
84 | 73 | Alexander Kamkin | # compositor: { {mov, sub}, {sub, sub}, {add, mov}, {add, sub} } |
85 | 67 | Alexander Kamkin | |
86 | iterate { # sequences: {{sub}, {add}} |
||
87 | 48 | Mikhail Chupilko | sub_r16r16 cx, dx |
88 | add_r16r16 ax, bx |
||
89 | } |
||
90 | 67 | Alexander Kamkin | iterate { # sequences: {{mov}, {sub}} |
91 | 48 | Mikhail Chupilko | mov_r16r16 ax, bx |
92 | 59 | Alexander Kamkin | sub_r16r16 r16(_), r16(_) |
93 | 48 | Mikhail Chupilko | } |
94 | 1 | Mikhail Chupilko | }.run |
95 | 48 | Mikhail Chupilko | </code></pre> |
96 | 72 | Alexander Kamkin | # this block merges two sequences in a random fashion |
97 | 74 | Alexander Kamkin | ~the combinator constructs the diagonal of the Cartesian product; the compositor randomly merges the tuples into sequences; the obfuscator reorders the sequences:~ |
98 | 1 | Mikhail Chupilko | <pre><code class="ruby"> |
99 | block(:combinator => ''diagonal'', :compositor => ''random'', :obfuscator => ''random'') { |
||
100 | 74 | Alexander Kamkin | # combinator: {({sub, or}, {start, and, end})} |
101 | # compositor: { {sub, start, and, end, or} } |
||
102 | # obfuscator: { {or, start, and, end, sub} } |
||
103 | 72 | Alexander Kamkin | |
104 | sequence { # sequence {sub, or} |
||
105 | 1 | Mikhail Chupilko | sub_r16r16 bx, ax |
106 | 48 | Mikhail Chupilko | or_r16r16 cx, dx |
107 | 1 | Mikhail Chupilko | } |
108 | |||
109 | 74 | Alexander Kamkin | atomic { # sequence {start, add, end} |
110 | 1 | Mikhail Chupilko | prologue { comment ''Atomic starts'' } |
111 | 48 | Mikhail Chupilko | epilogue { comment ''Atomic ends'' } |
112 | |||
113 | 59 | Alexander Kamkin | and_r16r16 r16(_), r16(_) |
114 | 48 | Mikhail Chupilko | } |
115 | }.run |
||
116 | 50 | Alexander Protsenko | </code></pre> |
117 | 48 | Mikhail Chupilko | |
118 | To generate test program(s) from a test template (in our case, from @block.rb@), run the following command: |
||
119 | 53 | Mikhail Chupilko | <pre>sh $MICROTESK_HOME/bin/generate.sh x86 block.rb --code-file-prefix block --code-file-extension s -v</pre> |
120 | 48 | Mikhail Chupilko | |
121 | 49 | Alexander Protsenko | When generation is finished, the resulting assembly code can be found in @$MICROTESK_HOME@. |
122 | 50 | Alexander Protsenko | |
123 | 52 | Mikhail Chupilko | To compile "the output file":http://forge.ispras.ru/attachments/download/5127/block_0000.s, run the following commands: |
124 | 51 | Alexander Protsenko | <pre> |
125 | nasm -f elf block_0000.s |
||
126 | ld -m i386pe -s -o block_0000 block_0000.o |
||
127 | </pre> |
||
128 | 48 | Mikhail Chupilko | |
129 | To execute resulted test cases is possible by means of the "online simulator":https://www.tutorialspoint.com/compile_assembly_online.php |
||
130 | !Example_block.png! |