Esempio n. 1
1
def test_reg_int():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        add(eax, 10),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int])
    assert fun(1234) == 1244
Esempio n. 2
0
def test():
    start = Label()
    end = Label()

    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(ebx, ebp.addr + 8),
        mov(eax, 1),
        start,
        cmp(ebx, 1),
        jl(end),
        mul(ebx),
        dec(ebx),
        jmp(start),
        end,
        pop(ebp),
        ret(),
    )
    fun = prog.compile(restype=c_int, argtypes=[c_int])

    for i in range(
            13
    ):  #factorial of 12 is the maximum integer fitting a 32 bit register
        assert fun(i) == factorial(i)
Esempio n. 3
0
def log(name, operand, type=c_int):
    if isinstance(operand, Register):
        if isinstance(operand, st):
            type = c_float
            push_fun = [
                sub(esp, 4),
                mov(ebp, esp),
                fld(operand),
                fstp(ebp.addr),
            ]
        else:
            push_fun = push(operand)

        @function(None, type)
        def _debug(value):
            print(name % value)

        return [
            push(eax),
            push(ebx),
            push(ecx),
            push(edx),
            push(ebp),
            push_fun,
            mov(eax, _debug),
            call(eax),
            add(esp, 4),
            pop(ebp),
            pop(edx),
            pop(ecx),
            pop(ebx),
            pop(eax),
        ]
    else:
        raise Exception('operand is not suited: %s' % operand)
Esempio n. 4
0
def log(name, operand, type=c_int):
    if isinstance(operand, Register):
        if isinstance(operand, st):
            type = c_float
            push_fun = [
                sub(esp, 4),
                mov(ebp, esp),
                fld(operand),
                fstp(ebp.addr),
            ]
        else:
            push_fun = push(operand)
        @function(None, type)
        def _debug(value):
            print(name % value)
        return [
            push(eax),
            push(ebx),
            push(ecx),
            push(edx),
            push(ebp),
            push_fun,
            mov(eax, _debug),
            call(eax),
            add(esp, 4),
            pop(ebp),
            pop(edx),
            pop(ecx),
            pop(ebx),
            pop(eax),
        ]
    else:
        raise Exception('operand is not suited: %s' % operand)
Esempio n. 5
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(restype=c_int)
    assert fun(1234) == 1234
Esempio n. 6
0
def example():
    msg = 'Hello World!\n'
    prog = Program(
        mov(ebx, 1),
        mov(ecx, String(msg)),
        mov(edx, len(msg)),
        syscall('write'),
        ret(),
        )
    fun = prog.compile()
    fun()
Esempio n. 7
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(ebx, 1234),
        log('ebx is', ebx),
        pop(ebp),
        ret(),
    )
    fun = prog.compile()
    fun()
Esempio n. 8
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr + 8),
        inc(eax),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int])
    assert fun(1234) == 1235
Esempio n. 9
0
def example():
    msg = 'Hello World!'
    prog = Program(
        mov(ebx, 1),
        mov(ecx, String(msg)),
        mov(edx, len(msg)),
        syscall('write'),
        ret(),
    )
    fun = prog.compile()
    fun()
Esempio n. 10
0
def test_reg_reg():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        mov(ebx, ebp.addr+12),
        add(eax, ebx),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int, c_int])
    assert fun(1234, 20) == 1254
Esempio n. 11
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),

        mov(ebx, 1234),
        log('ebx is', ebx),
        
        pop(ebp),
        ret(),
    )
    fun = prog.compile()
    fun()
Esempio n. 12
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, foo),
        push(1234),
        call(eax),
        add(esp, 4),
        pop(ebp),
        ret(),
    )
    fun = prog.compile()
    fun()
    assert called == 1234
Esempio n. 13
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),

        mov(eax, foo),
        push(1234),
        call(eax),
        add(esp, 4),
        
        pop(ebp),
        ret(),
    )
    fun = prog.compile()
    fun()
    assert called == 1234
Esempio n. 14
0
def example():
    prog = Program(
        mov(ebx, 42),
        syscall('exit'),
    )
    fun = prog.compile()
    fun()
Esempio n. 15
0
def example():
    prog = Program(
        mov(ebx, 42),
        syscall('exit'),
    )
    fun = prog.compile()
    fun()
Esempio n. 16
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr + 8),
        cmp(eax, 100),
        pushfd(),
        pop(eax),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int])

    assert fun(99) & CF
    assert fun(100) & ZF
    assert not (fun(101) & CF or fun(101) & ZF)
Esempio n. 17
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        cmp(eax, 100),
        pushfd(),
        pop(eax),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_int])

    assert fun(99) & CF
    assert fun(100) & ZF
    assert not (fun(101) & CF  or fun(101) & ZF)
Esempio n. 18
0
def test_float():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        fld(ebp.addr + 8),
        sub(esp, 4),
        mov(ebp, esp),
        fstp(ebp.addr),
        mov(eax, bar),
        call(eax),
        add(esp, 4),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(None, [c_float])
    fun(1234.5)
    assert called == 1234.5
Esempio n. 19
0
def test():
    prog = Program(
        mov(ebx, 10),
        lea(eax, ebx, 20),
        ret(),
    )
    fun = prog.compile(c_int)
    assert fun() == 30
Esempio n. 20
0
def test_float():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        
        fld(ebp.addr+8),
        sub(esp, 4),
        mov(ebp, esp),
        fstp(ebp.addr),
        mov(eax, bar),
        call(eax),
        add(esp, 4),
        
        pop(ebp),
        ret(),
    )
    fun = prog.compile(None, [c_float])
    fun(1234.5)
    assert called == 1234.5
Esempio n. 21
0
def test():
    loop = Label()
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr + 8),
        mov(ebx, ebp.addr + 12),
        mov(ecx, ebp.addr + 16),
        mov(edx, ebp.addr + 20),
        loop,
        fld(ebx.addr),
        fmul(ecx.addr),
        fstp(edx.addr),
        add(ebx, 4),
        add(ecx, 4),
        add(edx, 4),
        dec(eax),
        cmp(eax, 0),
        jg(loop),
        pop(ebp),
        ret(),
    )

    fun = prog.compile(argtypes=[
        c_int,
        POINTER(c_float),
        POINTER(c_float),
        POINTER(c_float),
    ])

    size = 1000
    a = (c_float * size)()
    b = (c_float * size)()
    c = (c_float * size)()
    a[:] = range(0, size)
    b[:] = range(size, size * 2)
    c[:] = [0] * size

    fun(size, a, b, c)
    #weirdly off by 1 for higher result numbers
    for i in range(size):
        assert c[i] == a[i] * b[i]
def test():
    loop = Label()
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        mov(ebx, ebp.addr+12),
        mov(ecx, ebp.addr+16),
        mov(edx, ebp.addr+20),
        loop,
            fld(ebx.addr),
            fmul(ecx.addr),
            fstp(edx.addr),
            add(ebx, 4),
            add(ecx, 4),
            add(edx, 4),
            dec(eax),
        cmp(eax, 0),
        jg(loop),
        pop(ebp),
        ret(),
    )
   
    fun = prog.compile(argtypes=[
        c_int,
        POINTER(c_float),
        POINTER(c_float),
        POINTER(c_float),
    ])
   
    size = 1000
    a = (c_float*size)()
    b = (c_float*size)()
    c = (c_float*size)()
    a[:] = range(0, size)
    b[:] = range(size, size*2)
    c[:] = [0] * size
  
    fun(size, a, b, c)
    #weirdly off by 1 for higher result numbers
    for i in range(size):
        assert c[i] == a[i] * b[i]
Esempio n. 23
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        fld(ebp.addr + 8),
        fmul(ebp.addr + 12),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_float, [c_float, c_float])
    assert fun(4, 2) == 8.0
Esempio n. 24
0
def syscall(name):
    names = dict(
        exit=1,
        read=3,
        write=4,
    )

    return [
        mov(eax, names[name]),
        interrupt(0x80),
    ]
Esempio n. 25
0
def test():
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        fld(ebp.addr+8),
        fmul(ebp.addr+12),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_float, [c_float, c_float])
    assert fun(4, 2) == 8.0
Esempio n. 26
0
def syscall(name):
    names = dict(
        exit    = 1,
        read    = 3,
        write   = 4,
    )

    return [
        mov(eax, names[name]),
        interrupt(0x80),
    ]
Esempio n. 27
0
def test():
    a = (c_float*10)()
    a[2] = 2501.1
    b = (c_float*10)()
    b[2] = 1234.0

    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr+8),
        add(eax, 8),
        mov(edx, ebp.addr+12),
        add(edx, 8),
        mov(edx, edx.addr),
        mov(eax.addr, edx),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(argtypes=[POINTER(c_float), POINTER(c_float)])
    fun(a, b)
    assert a[2] == b[2]
Esempio n. 28
0
def test():
    label = Label('foo')
    prog = Program(
        mov(eax, 0),
        label,
        inc(eax),
        cmp(eax, 100),
        jl(label),
        ret(),
    )
    fun = prog.compile(c_int)
    assert fun() == 100
Esempio n. 29
0
def test():
    label = Label('foo')
    prog = Program(
        mov(eax, 0),
        label,
        inc(eax),
        cmp(eax, 100),
        jl(label),
        ret(),
    )
    fun = prog.compile(c_int)
    assert fun() == 100
Esempio n. 30
0
def test_float():
    label = Label()
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        fld(ebp.addr+8),
        fld(ebp.addr+12),
        fcomi(st(1)),
        fstp(st(0)),
        fstp(st(0)),
        ja(label),
        mov(eax, 1),
        pop(ebp),
        ret(),
        label,
        mov(eax, 2),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_float, c_float])
    assert fun(10, 20) == 2
    assert fun(20, 10) == 1
Esempio n. 31
0
def test_float():
    label = Label()
    prog = Program(
        push(ebp),
        mov(ebp, esp),
        fld(ebp.addr + 8),
        fld(ebp.addr + 12),
        fcomi(st(1)),
        fstp(st(0)),
        fstp(st(0)),
        ja(label),
        mov(eax, 1),
        pop(ebp),
        ret(),
        label,
        mov(eax, 2),
        pop(ebp),
        ret(),
    )
    fun = prog.compile(c_int, [c_float, c_float])
    assert fun(10, 20) == 2
    assert fun(20, 10) == 1
Esempio n. 32
0
def test():
    start = Label()
    end = Label()

    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(ebx, ebp.addr+8),
        mov(eax, 1),
        start,
        cmp(ebx, 1),
        jl(end),
        mul(ebx),
        dec(ebx),
        jmp(start),
        end,
        pop(ebp),
        ret(),
    )
    fun = prog.compile(restype=c_int, argtypes=[c_int])

    for i in range(13): #factorial of 12 is the maximum integer fitting a 32 bit register
        assert fun(i) == factorial(i)
Esempio n. 33
0
# -*- coding: utf-8 -*-
"""
    add
    ~~~

    :copyright: 2008 by Henri Tuhola <*****@*****.**>
    :license: GNU AGPL v3 or later, see LICENSE for more details.
"""

from ctypes import c_long

from pyasm import Program
from pyasm.instructions import mov, add, ret
from pyasm.registers import rax, rdi

if __name__ == '__main__':
    prog = Program(
        mov(rax, rdi),
        add(rax, 10),
        ret()
    )

    fun = prog.compile(restype=c_long, argtypes=[c_long])
    assert fun(1234) == 1244
Esempio n. 34
0
# -*- coding: utf-8 -*-
"""
    copy
    ~~~~

    :copyright: 2008 by Henri Tuhola <*****@*****.**>
    :license: GNU AGPL v3 or later, see LICENSE for more details.
"""

from ctypes import POINTER, c_float, c_long

from pyasm import Program
from pyasm.instructions import mov, ret
from pyasm.registers import eax, rdi, rsi

if __name__ == '__main__':
    a = (c_float * 10)()
    a[2] = 2501.1
    b = (c_float * 10)()
    b[2] = 1234.0

    prog = Program(mov(eax, rsi.addr + 8), mov(rdi.addr + 8, eax), ret())
    fun = prog.compile(argtypes=[POINTER(c_float), POINTER(c_float)])
    fun(a, b)
    assert a[2] == b[2]
Esempio n. 35
0
from ctypes import POINTER, c_long

from pyasm import Program
from pyasm.base import Label
from pyasm.instructions import mov, mul, dec, cmp, jg, ret
from pyasm.registers import rax, rdi

def factorial(value):
    accum = 1
    while value > 1:
        accum *= value
        value -= 1
    return accum

if __name__ == '__main__':
    loop = Label()
    prog = Program(
        mov(rax, 1),
        loop,
        mul(rax, rdi),
        dec(rdi),
        cmp(rax, 1),
        ja(loop),
        ret()
    )
    fun = prog.compile(restype=c_long, argtypes=[c_long])

    for i in xrange(20):
        assert fun(i) == factorial(i)
Esempio n. 36
0
def test():
    prog = Program(push(ebp), mov(ebp, esp), mov(eax, ebp.addr + 8), inc(eax), pop(ebp), ret())
    fun = prog.compile(c_int, [c_int])
    assert fun(1234) == 1235
Esempio n. 37
0
def compile():
    outer_loop = Label()
    inner_loop = Label()
    distance_condition = Label()
    distance_else = Label()
    min_condition = Label()
    size_condition = Label()

    prog = Program(
        push(ebp),
        mov(ebp, esp),
        mov(eax, ebp.addr + 8),  #size
        cmp(eax, 2),
        jl(size_condition),
        mov(ecx, ebp.addr + 12),  #array pointer
        fld(ebp.addr + 16),  #force factor
        fld(ebp.addr + 20),  #minimum distance
        fld(ebp.addr + 24),  #maximum distance
        sub(eax, 1),
        mov(ebx, 4 * 4),
        mul(ebx),
        add(ecx, eax),
        mov(eax, ebp.addr + 12),  #array pointer
        outer_loop,
        mov(ebx, eax),
        add(ebx, 4 * 4),
        inner_loop,
        # x vector
        fld(eax.addr + 0 * 4),
        fsub(ebx.addr + 0 * 4),
        # y vector
        fld(eax.addr + 1 * 4),
        fsub(ebx.addr + 1 * 4),
        # distance
        fld(st(1)),
        fmul(st(0), st(0)),
        fld(st(1)),
        fmul(st(0), st(0)),
        faddp(),
        fsqrt(),
        #make sure the distance is bigger then 10
        fcomi(st(4)),
        fcmovb(st(4)),
        #only calculate if the distance is less then 200
        fcomi(st(3)),
        ja(distance_condition),
        #cube distance
        fld(st(0)),
        fmul(st(1), st(0)),
        fmulp(),
        #compute force vector
        fdiv(st(2), st(0)),
        fdivp(),
        fld(st(4)),
        fmul(st(2), st(0)),
        fmulp(),
        #accumulate y component
        fld(st(0)),
        fadd(eax.addr + 3 * 4),
        fstp(eax.addr + 3 * 4),
        fsubr(ebx.addr + 3 * 4),
        fstp(ebx.addr + 3 * 4),
        #accumulate x component
        fld(st(0)),
        fadd(eax.addr + 2 * 4),
        fstp(eax.addr + 2 * 4),
        fsubr(ebx.addr + 2 * 4),
        fstp(ebx.addr + 2 * 4),
        jmp(distance_else),
        distance_condition,
        fstp(st(0)),
        fstp(st(0)),
        fstp(st(0)),
        distance_else,
        add(ebx, 4 * 4),
        cmp(ebx, ecx),
        jbe(inner_loop),
        add(eax, 4 * 4),
        cmp(eax, ecx),
        jb(outer_loop),

        #restore the fpu
        fstp(st(0)),
        fstp(st(0)),
        fstp(st(0)),
        size_condition,
        pop(ebp),
        ret(),
    )

    fun = prog.compile(argtypes=[
        c_int,
        POINTER(c_float),
        c_float,
        c_float,
        c_float,
    ])

    return fun
Esempio n. 38
0
# -*- coding: utf-8 -*-
"""
    echo
    ~~~~

    :copyright: 2008 by Florian Boesch <*****@*****.**>.
    :license: GNU AGPL v3 or later, see LICENSE for more details.
"""

from ctypes import c_long

from pyasm import Program
from pyasm.instructions import mov, ret
from pyasm.registers import rax, rdi

if __name__ == '__main__':
    #linux only call convention, not valid for windows
    prog = Program(
        mov(rax, rdi),
        ret(),
    )
    fun = prog.compile(restype=c_long)
    assert fun(1234) == 1234
Esempio n. 39
0
# -*- coding: utf-8 -*-

"""
    echo
    ~~~~

    :copyright: 2008 by Florian Boesch <*****@*****.**>.
    :license: GNU AGPL v3 or later, see LICENSE for more details.
"""

from ctypes import c_long

from pyasm import Program
from pyasm.instructions import mov, ret
from pyasm.registers import rax, rdi

if __name__ == "__main__":
    # linux only call convention, not valid for windows
    prog = Program(mov(rax, rdi), ret())
    fun = prog.compile(restype=c_long)
    assert fun(1234) == 1234
Esempio n. 40
0
# -*- coding: utf-8 -*-
"""
    copy
    ~~~~

    :copyright: 2008 by Henri Tuhola <*****@*****.**>
    :license: GNU AGPL v3 or later, see LICENSE for more details.
"""

from ctypes import POINTER, c_float, c_long

from pyasm import Program
from pyasm.instructions import mov, ret
from pyasm.registers import eax, rdi, rsi

if __name__ == '__main__':
    a = (c_float*10)()
    a[2] = 2501.1
    b = (c_float*10)()
    b[2] = 1234.0

    prog = Program(
        mov(eax, rsi.addr+8),
        mov(rdi.addr+8, eax),
        ret()
    )
    fun = prog.compile(argtypes=[POINTER(c_float),POINTER(c_float)])
    fun(a, b)
    assert a[2] == b[2]
Esempio n. 41
0
def compile():
    outer_loop = Label()
    inner_loop = Label()
    distance_condition = Label()
    distance_else = Label()
    min_condition = Label()
    size_condition = Label()

    prog = Program(
        push(ebp),
        mov(ebp, esp),

        mov(eax, ebp.addr+8), #size
        cmp(eax, 2),
        jl(size_condition),

        mov(ecx, ebp.addr+12), #array pointer

        fld(ebp.addr+16), #force factor
        fld(ebp.addr+20), #minimum distance
        fld(ebp.addr+24), #maximum distance

        sub(eax, 1),
        mov(ebx, 4*4),
        mul(ebx),
        add(ecx, eax),

        mov(eax, ebp.addr+12), #array pointer

        outer_loop,
            mov(ebx, eax),
            add(ebx, 4*4),
            inner_loop,
                # x vector
                fld(eax.addr+0*4),
                fsub(ebx.addr+0*4),
                # y vector 
                fld(eax.addr+1*4),
                fsub(ebx.addr+1*4),
                # distance
                fld(st(1)), 
                fmul(st(0), st(0)),
                fld(st(1)), 
                fmul(st(0), st(0)),
                faddp(),
                fsqrt(),
                #make sure the distance is bigger then 10
                fcomi(st(4)),
                fcmovb(st(4)),
                #only calculate if the distance is less then 200
                fcomi(st(3)),
                ja(distance_condition),
                    #cube distance
                    fld(st(0)),
                    fmul(st(1), st(0)),
                    fmulp(),
                    #compute force vector
                    fdiv(st(2), st(0)),
                    fdivp(),
                    fld(st(4)),
                    fmul(st(2), st(0)),
                    fmulp(),
                    #accumulate y component
                    fld(st(0)),
                    fadd(eax.addr+3*4),
                    fstp(eax.addr+3*4),
                    fsubr(ebx.addr+3*4),
                    fstp(ebx.addr+3*4),
                    #accumulate x component
                    fld(st(0)),
                    fadd(eax.addr+2*4),
                    fstp(eax.addr+2*4),
                    fsubr(ebx.addr+2*4),
                    fstp(ebx.addr+2*4),
                jmp(distance_else),
                distance_condition,
                    fstp(st(0)),
                    fstp(st(0)),
                    fstp(st(0)),
                distance_else,
            add(ebx, 4*4),
            cmp(ebx, ecx),
            jbe(inner_loop),
        add(eax, 4*4),
        cmp(eax, ecx),
        jb(outer_loop),
        
        #restore the fpu
        fstp(st(0)),
        fstp(st(0)),
        fstp(st(0)),

        size_condition,
        
        pop(ebp),
        ret(),
    )

    fun = prog.compile(argtypes=[
        c_int,
        POINTER(c_float),
        c_float,
        c_float,
        c_float,
    ])

    return fun