Example #1
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)
Example #2
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]
Example #4
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)
Example #5
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)