Project

General

Profile

Actions

Getting Started with x86 » History » Revision 84

« Previous | Revision 84/87 (diff) | Next »
Alexander Kamkin, 04/06/2017 08:46 PM


Getting Started with x86

Prerequisite

MicroTESK should be installed.

Demo Specifications

Specifications of the x86 (8086) instruction set architecture (ISA) can be found in $MICROTESK_HOME/arch/demo/x86/model/x86.nml.

Instruction are described in nML by means of the following constructs (move r16/r16 is taken as an example):

  1. the signature
    op mov_r16r16 (dst: R16, src: R16)
  2. the assembly format
    syntax = format("mov %s, %s", dst.syntax, src.syntax)
  3. the binary encoding
    image = format("1000101111%s%s", dst.image, src.image)
  4. the semantics
      action = {
        dst = src;
        ...
      }
    

To compile the ISA model, run the following command:

sh $MICROTESK_HOME/bin/compile.sh x86.nml

Demo Templates

Test templates for the x86 (8086) ISA can be found in $MICROTESK_HOME/arch/demo/x86/templates.

The directory contains a number of demo templates including the following ones:

block.rb demonstrates how to use block constructs
block_random.rb demonstrates how to create randomized instruction sequences using block constructs
euclid.rb demonstrates test program simulation to predict the resulting microprocessor state
random.rb demonstrates how to randomize tests by using biases and distributions
random_immediate.rb demonstrates how to randomize immediate values
random_registers.rb demonstrates how to randomize registers (dependencies)

Test templates are written in Ruby extended with specific constructs (let us look at block.rb):

  1. the code in the beginning includes the x86_base.rb file where a base template X86BaseTemplate is defined and declares a BlockTemplate template:
    require_relative ''x86_base''
    
    class BlockTemplate < X86BaseTemplate  # BlockTemplate is a heir of @X86BaseTemplate@
    
  2. here is a template entry point:
    def run
  3. this block produces a sequence consisting of three instructions:
      sequence { # {{mov, sub, add}}
        mov_r16r16 ax, bx
        sub_r16r16 cx, dx
        add_r16r16 r16(_), r16(_)
      }.run
    
  4. this block produces an atomic sequence consisting of three instructions (atomic sequences are not interrupted while being merged with other ones):
      atomic { # {{mov, add, sub}}
        mov_r16r16 ax, bx
        add_r16r16 cx, dx
        sub_r16r16 r16(_), r16(_)
      }.run
    
  5. this block produces three sequences each consisting of one instruction:
      iterate { # {{mov}, {sub}, {add}}
        mov_r16r16 ax, bx
        sub_r16r16 cx, dx
        add_r16r16 r16(_), r16(_)
      }.run
    
  6. this block produces four sequences each consisting of two instructions
    the combinator constructs the Cartesian product of the nested sets of sequences; the compositor randomly merges each tuple of the product into one sequence:
      block(:combinator => ''product'',  # {({sub}, {mov}), ({sub}, {sub}), ({add}, {mov}), ({add}, {sub})}
            :compositor => ''random'') { # {  {mov, sub},     {sub, sub},     {add, mov},     {add, sub}  }
    
        iterate { # {{sub}, {add}}
          sub_r16r16 cx, dx
          add_r16r16 ax, bx
        }
    
        iterate { # {{mov}, {sub}}
          mov_r16r16 ax, bx
          sub_r16r16 r16(_), r16(_)
        }
      }.run
    
  7. this block merges two sequences in a random fashion
    the combinator constructs the diagonal of the Cartesian product; the compositor randomly merges the tuples into sequences; the obfuscator reorders the sequences:
      block(:combinator => ''diagonal'', # {({sub, or}, {start, and, end})}
            :compositor => ''random'',   # {  {sub, start, and, end, or}  }
            :obfuscator => ''random'') { # {  {or, start, and, end, sub}  }
    
        sequence { # {sub, or}
          sub_r16r16 bx, ax
          or_r16r16 cx, dx
        }
    
        atomic {   # {start, add, end}
          prologue { comment ''Atomic starts'' }
          epilogue { comment ''Atomic ends'' }
    
          and_r16r16 r16(_), r16(_)
        }
      }.run
    

To generate a test program from a test template (in our case, from block.rb), run the following command:

sh $MICROTESK_HOME/bin/generate.sh x86 block.rb --code-file-prefix block --code-file-extension s -v

When generation is finished, the resulting assembly code can be found in $MICROTESK_HOME:

        ...

    ;==================================================================================================
    ; Prologue

    section .text
    global _start

_start:

    ;==================================================================================================
    ; Test Case 0 (block.rb:28)

    ; Preparation
    mov BX, 43715

    ; Stimulus
    mov AX, BX
    sub CX, DX
    add SI, DX

        ...

To compile the test program, run the following commands:

nasm -f elf block_0000.s
ld -m i386pe -s -o block_0000 block_0000.o

The test program can be executed in the simulator

Updated by Alexander Kamkin almost 7 years ago · 84 revisions