ForceBru PyVM Versions Save

A virtual machine written in Python that executes x86 binaries according to the Intel Software Developer Manual

v0.1-alpha

6 years ago

PyVM is a virtual machine written in pure Python that runs Intel 8086/8088 machine code. No dependencies, no installation required.

Running tests

You may want to run the tests first. To do this, execute from the command line:

cd path/to/PyVM
python unittest_basic.py
python unittest_VM.py

Assembling your code

At the moment, PyVM can only execute flat binaries. You can assemble those with, for example, NASM:

nasm -f bin -o program.bin program.s

Then, execute the binary with PyVM:

import VM

vm = VM.VM(1024)  # a virtual machine with 1024 bytes of memory; you may need to increase this value

vm.execute_file("path/to/program.bin")  # this is what you've assembled at the previous step

Errors that may occur

If you decide to open an issue, please include a minimal, complete and verifiable example that reproduces the error and the traceback.

  • Not enough memory supplied -> increase the amount of memory given to the VM

    Traceback (most recent call last):
      File "...", line 53, in <module>
        vm.execute_bytes(binary)
      File ".../VM/fetchLoop.py", line ..., in execute_bytes
        self.mem.set(offset, data)
      File ".../VM/Memory.py", line ..., in set
        assert offset + size in self.bounds
    AssertionError
    
  • Unknown opcode -> please open an issue entitled Unknown opcode: [opcode in hex]

    ...
    ValueError: Unknown opcode: 0xf8
    
  • The executable attempted to access a nonexistent memory location -> make sure your code absolutely cannot do this (that is, works fine after having been assembled, linked and run as a native executable), that there's enough memory supplied to the VM and open an issue entitled Invalid memory access.

    ...
       assert offset + size in self.bounds
    AssertionError
    
  • Other errors -> please open an issue entitled accordingly

Features

  • all 8-, 16- and 32-bit registers
  • stack
  • user memory
  • flat memory model
  • ~50% of 8086/8088 Intel processor's instruction set
  • some basic prefixes
  • execution of flat binaries and raw bytes

TODO

  • add segments
  • implement multiplication and division
  • add more instructions
  • add support for more binary formats