Giter VIP home page Giter VIP logo

pyasm2's Introduction

pyasm2 - x86 assembler library (C) 2012 Jurriaan Bremer

Although its called pyasm2, this is not per se a successor of Pyasm or pyASM. pyasm2 aims to be as flexible as possible, it will support x86, SSE and SSE2.

A key feature of pyasm2 is the ability to have blocks of instructions and being able to give the base address at a later time, that is, you don't need to know the address of instructions before-hand. For example, you can construct a series of instructions, request the size that will be needed in order to store all instructions as sequence, allocate this memory and write the instructions from there, this approach is very useful when making JIT compilers etc.

The syntax of pyasm2 is supposed to be as simple as possible.

For example, an instruction such as "mov eax, dword [ebx+edx*2+32]" can be encoded using pyasm2 as the following.

mov(eax, dword [ebx+edx*2+32])

These memory addresses also support segment registers, e.g.

mov(eax, dword[fs:0xc0])

although this is currently only supported in 64bit python versions.

Furthermore, pyasm2 makes it possible to chain multiple instructions. Take for example the following statement.

block(mov(eax, ebx), push(32))

However, for more implementation-specific details, please refer to the IMPLEMENTATION file.

pyasm2's People

Contributors

jbremer avatar rqndom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyasm2's Issues

memory addresses with segment

When referencing an address with a segment, e.g. in the following snippet.

mov(eax, dword[fs:eax])

Then, because the python index limit for 32bit versions is MAX_INT which is 1<<31, we have to store the memory address temporarily in a global dictionary, with a random key. The key, an integer, is passed along in __getitem__ so that the MemoryAddress object (e.g. byte, dword, ..) can read the memory address from the global dictionary (and delete it from the dictionary afterwards.)
After the object has been passed, all is well.

xchg

Seems there are several problems when I'm trying to assemble xchg instructions, here is an example :

>>> '%s' % xchg(esi, esp)
Traceback (most recent call last):
  File "stdin", line 1, in module
  File "D:\TODO\pyasm2.py", line 755, in __str__
    ret += self.modrm(modrm_reg, modrm_rm)
  File "D:\TODO\pyasm2.py", line 512, in modrm
    reg = op1.index
AttributeError: 'NoneType' object has no attribute 'index'

The problem seems to be there only if I assemble xchg instruction with the opcode \x87 or \x86.

WindowsError: exception: priviledged instruction

Whenever I implement a loop, I get the above error. I fail to figure out why it happens.
It didn't matter which type of label, they all produced the same result. It doesn't matter
what code i have before or after. As soon as I put in a Label I get the above error.

The tests provided all worked flawlessly.

I'm running windows 7 64bit, using 32bit Python 2.7.12.

Oh, I should add that I used parts of pyacc to create executable memory,
which has no issues running the code created by pyasm2. That works completely,
yet as soon as I include a label it gives me the error.

Any ideas? I know, asking is a stretch, but I don't know yet what to do.
Or maybe do you know a way to actually execute the code?

Because there doesn't seem to be one, so I had to come up with this ...

bug when using scale on MemoryAddress

This code works:

mov(eax, [ebx+0x4])

>>> mov eax, [ebx+0x4]

But this one outputs an error:

mov(eax, [ebx*4+0x4])

>>> <repr(<pyasm2.x86.mov at 0x7f59dd3af320>) failed: AttributeError: 'int' object has no attribute 'value'>

It appears to be related to the disp attribute.

Proposed patch:

--- x86.py.bak  2016-10-14 01:06:35.613063043 +0200
+++ x86.py  2016-10-14 01:04:42.665010353 +0200
@@ -283,7 +283,7 @@
             q = str(self.reg2) if self.mult == 1 else \
                 str(self.reg2) + '*' + str(self.mult)
             s += q if not len(s) else '+' + q
-        if self.disp.value:
+        if self.disp:
             if self.disp >= 0:
                 q = '0x%x' % int(self.disp)
             else:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.