Project

General

Profile

Getting Started with x86 » History » Version 48

Mikhail Chupilko, 04/06/2017 05:40 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 41 Alexander Kamkin
Instruction are described in [[nML Language Reference|nML]] by means of the following constructs (_mov r16/r16_ is taken as an example):
14 8 Alexander Kamkin
15 10 Alexander Kamkin
## the signature
16 8 Alexander Kamkin
<pre>op mov_r1616 (dst: GPR16, src: GPR16)</pre>
17 10 Alexander Kamkin
## the assembly format
18 8 Alexander Kamkin
<pre>syntax = format("mov %s, %s", dst.syntax, src.syntax)</pre>
19 10 Alexander Kamkin
## the binary encoding
20 8 Alexander Kamkin
<pre>image = format("1000101111%s%s", dst.image, src.image)</pre>
21 14 Alexander Kamkin
## the semantics
22 1 Mikhail Chupilko
<pre>
23
  action = {
24
    dst = src;
25 9 Alexander Kamkin
    ...
26 1 Mikhail Chupilko
  }
27 24 Alexander Kamkin
</pre>
28
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 32 Alexander Kamkin
Test templates for the x86 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 41 Alexander Kamkin
Test templates are written in "Ruby":http://www.ruby-lang.org extended with specific [[Template_Description_Language|constructs]].
46
47 48 Mikhail Chupilko
Let''s review one of the templates (@block.rb@) in detail.
48
# includes the file where the base class is defined, containing definition of the x86 ISA registers, their preparators, etc.
49
<pre>require_relative ''x86_base''</pre>
50
# declares current template class declaration as a heir of X86BaseTemplate
51
<pre>class BlockTemplate < X86BaseTemplate</pre>
52
# defines a "run" method (also, "initialize", "pre", and "post" methods are declared in the base class, and always inherited)
53
<pre> def run</pre>
54
# produces a single test case that consists of three instructions
55
<pre>
56
  sequence {
57
    mov_r16r16 ax, bx
58
    sub_r16r16 cx, dx
59
    add_r16r16 gpr16(_), gpr16(_)
60
  }.run
61
</pre>
62
# atomic sequence; works as sequence in this context
63
<pre>
64
  atomic {
65
    mov_r16r16 ax, bx
66
    add_r16r16 cx, dx
67
    sub_r16r16 gpr16(_), gpr16(_)
68
  }.run
69
</pre>
70
# produces three test cases each consisting of one instruction
71
<pre>
72
  iterate {
73
    mov_r16r16 ax, bx
74
    sub_r16r16 cx, dx
75
    add_r16r16 gpr16(_), gpr16(_)
76
  }.run
77
</pre>
78
# produces four test cases consisting of two instructions (Cartesian product composed in a random order)
79
<pre>
80
  block(:combinator => ''product'', :compositor => ''random'') {
81
    iterate {
82
      sub_r16r16 cx, dx
83
      add_r16r16 ax, bx
84
    }
85
    iterate {
86
      mov_r16r16 ax, bx
87
      sub_r16r16 gpr16(_), gpr16(_)
88
    }
89
  }.run
90
</pre>
91
# merges two sequences in random fashion; atomic sequences are unmodifiable
92
<pre>
93
  block(:combinator => ''diagonal'', :compositor => ''random'', :obfuscator => ''random'') {
94
    sequence {
95
      sub_r16r16 bx, ax
96
      or_r16r16 cx, dx
97
    }
98 1 Mikhail Chupilko
99 48 Mikhail Chupilko
    atomic {
100
      prologue { comment ''Atomic starts'' }
101
      epilogue { comment ''Atomic ends'' }
102
103
      and_r16r16 gpr16(_), gpr16(_)
104
    }
105
  }.run
106
</pre>
107
# finishes definition of the "run" method and class "BlockTemplate"
108
<pre>
109
 end
110
end
111
</pre>
112
113
114
To generate test program(s) from a test template (in our case, from @block.rb@), run the following command:
115
<pre>$MICROTESK_HOME/bin/generate.sh x86 block.rb --code-file-prefix block --code-file-extension -v</pre>
116
117 1 Mikhail Chupilko
When generation is finished, the resulting assembly code can be found in @$MICROTESK_HOME@.
118 48 Mikhail Chupilko
119
To execute resulted test cases is possible by means of the "online simulator":https://www.tutorialspoint.com/compile_assembly_online.php
120
!Example_block.png!