Beispiel #1
0
def TestDECMemMem_MemStep():
    A = extarray.extarray('l', 1000)
    B = extarray.extarray('l', 1000)

    for i in xrange(1000):
        A[i] = i

    prgm = env.Program()
    code = prgm.get_stream()
    a = registers.esi
    b = registers.edi
    code.add(x86.mov(a, memory.MemRef(registers.ebp, 2 * _ws)))
    code.add(x86.mov(b, memory.MemRef(registers.ebp, 3 * _ws)))
    n = memory.MemRef(registers.esp, 0)
    code.add(x86.sub(registers.esp, 12))
    code.add(x86.mov(n, 1000))
    #code.add(x86.push(1000))
    s = memory.MemRef(registers.esp, 2 * _ws)
    code.add(x86.mov(s, -4))
    i_iter = syn_iter(code,
                      n,
                      mode=DEC,
                      step=s,
                      count_reg=memory.MemRef(registers.esp, 1 * _ws),
                      clobber_reg=registers.eax)
    j = registers.ebx
    for i_ in i_iter:
        code.add(x86.mov(j, i_))
        code.add(x86.mov(registers.eax, memory.MemRef(a, index=j, scale=4)))
        code.add(x86.mov(memory.MemRef(b, index=j, scale=4), registers.eax))

    code.add(x86.add(registers.esp, 12))

    params = env.ExecParams()
    params.p1 = A.buffer_info()[0] - _ws
    params.p2 = B.buffer_info()[0] - _ws
    prgm += code
    proc = env.Processor()
    proc.execute(prgm, mode='int', params=params)

    for i in range(len(B) - 1, 0, -4):
        assert (B[i] == i)
Beispiel #2
0
def TestDECMemMem_MemStep():
  A = extarray.extarray('l', 1000)
  B = extarray.extarray('l', 1000)

  for i in xrange(1000):
    A[i] = i

  prgm = env.Program()
  code = prgm.get_stream()
  a = registers.esi
  b = registers.edi
  code.add(x86.mov(a, memory.MemRef(registers.ebp, 2*_ws)))
  code.add(x86.mov(b, memory.MemRef(registers.ebp, 3*_ws)))
  n = memory.MemRef(registers.esp, 0)
  code.add(x86.sub(registers.esp, 12))
  code.add(x86.mov(n, 1000))
  #code.add(x86.push(1000))
  s = memory.MemRef(registers.esp, 2*_ws)
  code.add(x86.mov(s, -4))
  i_iter = syn_iter(code, n, mode=DEC, step=s, count_reg = memory.MemRef(registers.esp, 1*_ws), clobber_reg=registers.eax)
  j = registers.ebx
  for i_ in i_iter:
    code.add(x86.mov(j, i_))
    code.add(x86.mov(registers.eax, memory.MemRef(a, index=j, scale=4)))
    code.add(x86.mov(memory.MemRef(b, index=j, scale=4), registers.eax))

  code.add(x86.add(registers.esp, 12))

  params = env.ExecParams()
  params.p1 = A.buffer_info()[0]-_ws
  params.p2 = B.buffer_info()[0]-_ws
  prgm += code
  proc = env.Processor()
  proc.execute(prgm, mode='int', params=params)

  for i in range(len(B)-1, 0, -4):
    assert(B[i] == i)
def Test():
    prgm = env.Program()
    code = prgm.get_stream()
    proc = env.Processor()
    params = env.ExecParams()
    params.p1 = 3
    mr32 = MemRef(ebp, 8)
    mr8 = MemRef(ebp, 8, data_size = 8)


    lbl1 = prgm.get_label("lbl1")
    lbl2 = prgm.get_label("lbl2")

    code.add(x86_isa.xor(eax, eax))

    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.jne(lbl1))

    code.add(x86_isa.ret())

    code.add(lbl1)
    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.je(lbl2))
    code.add(x86_isa.add(eax, 12))
    code.add(lbl2)

    prgm += code
    ret = proc.execute(prgm)
    print "ret", ret
    assert(ret == 12)

    prgm = env.Program()
    code = prgm.get_stream()

    fwd_lbl = prgm.get_label("FORWARD")
    bck_lbl = prgm.get_label("BACKWARD")

    code.add(x86_isa.xor(eax, eax))
    code.add(bck_lbl)
    code.add(x86_isa.cmp(eax, 1))
    code.add(x86_isa.jne(fwd_lbl))
    for i in xrange(0, 65):
      code.add(x86_isa.pop(edi))
    code.add(fwd_lbl)

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    assert(ret == 0)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")
    out_lbl = prgm.get_label("OUT")
    skip_lbl = prgm.get_label("SKIP")

    code.add(x86_isa.xor(eax, eax))
    code.add(loop_lbl)
    r_tmp = prgm.acquire_register()
    for i in range(0, 1):
      for i in xrange(0, 24):
        code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))

      code.add(x86_isa.add(eax, 4))
      code.add(x86_isa.cmp(eax, 20))
      code.add(x86_isa.je(out_lbl))

      for i in xrange(0, 24):
        code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))

      code.add(x86_isa.cmp(eax, 32))
      code.add(x86_isa.jne(loop_lbl))

    code.add(out_lbl)

    code.add(x86_isa.jmp(skip_lbl))
    for i in xrange(0, 2):
      code.add(x86_isa.add(r_tmp, MemRef(esp, 4)))
    code.add(skip_lbl)

    prgm.release_register(r_tmp)

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 20)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")
    else_lbl = prgm.get_label("ELSE")
    finish_lbl = prgm.get_label("finish")

    code.add(x86_isa.mov(eax, -1))
    code.add(x86_isa.mov(edx, 0))

    code.add(loop_lbl)

    code.add(x86_isa.add(eax, 1))
    code.add(x86_isa.cmp(eax, 16))
    code.add(x86_isa.jge(finish_lbl))

    code.add(x86_isa.add(edx, eax))
    code.add(x86_isa.mov(edx, edi))
    code.add(x86_isa.and_(edi, 0x1))
    code.add(x86_isa.jnz(else_lbl))

    code.add(x86_isa.add(edx, 1))
    code.add(x86_isa.jmp(loop_lbl))

    code.add(else_lbl)
    code.add(x86_isa.add(edx, edi))
    code.add(x86_isa.jmp(loop_lbl))

    code.add(finish_lbl)
    code.add(x86_isa.mov(eax, edx))

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 1)


    prgm = env.Program()
    code = prgm.get_stream()

    loop_lbl = prgm.get_label("LOOP")

    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.xor(ecx, ecx))
    code.add(x86_isa.mov(edx, 1))

    code.add(loop_lbl)
    code.add(x86_isa.inc(eax))
    code.add(x86_isa.cmp(eax, 7))
    code.add(x86_isa.cmove(ecx, edx))
    code.add(x86_isa.jecxz(loop_lbl))

    prgm += code
    ret = proc.execute(prgm, mode = 'int')
    print "ret", ret
    assert(ret == 7)


    prgm = env.Program()
    code = prgm.get_stream()

    code.add(x86_isa.add(eax, 200))
    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.add(al, 32))
    code.add(x86_isa.add(bl, 32))
    code.add(x86_isa.xor(bl, bl))
    code.add(x86_isa.mov(mr8, al))
    code.add(x86_isa.add(mr32, 0))
    code.add(x86_isa.mov(eax, mr32))
    code.add(x86_isa.mov(al, mr8))

    code.add(x86_isa.imul(ax, ax, 4))
    code.add(x86_isa.imul(ebx, eax, 10))
    code.add(x86_isa.mov(cx, 1232))
    code.add(x86_isa.sub(ax, cx))
    code.add(x86_isa.xor(eax,eax))
    code.add(x86_isa.mov(eax,ebx))
    code.add(x86_isa.clc())
    code.add(x86_isa.rcl(eax, 1))
    code.add(x86_isa.rcr(eax, 1))


    prgm += code
    #ret = proc.execute(code, debug = True, params = params)
    id1 = proc.execute(prgm, params = params, mode = 'int', async = True)
    id2 = proc.execute(prgm, params = params, mode = 'int', async = True)
    ret = proc.execute(prgm, params = params, mode = 'int')
    print "Return main thread: %d" % (ret)
    ret = proc.join(id1)
    print "Return thread 1: %d" % (ret)
    ret = proc.join(id2)
    print "Return thread 2: %d" % (ret)

    prgm = env.Program()
    code = prgm.get_stream()
    code.add(x86_isa.fldpi())
    code.add(x86_isa.fld1())
    code.add(x86_isa.fadd(st0, st0))
    code.add(x86_isa.fmulp())
    code.add(x86_isa.fsin())
    code.add(x86_isa.fcos())
    code.add(x86_isa.fld1())
    code.add(x86_isa.fyl2xp1())

    prgm += code
    ret = proc.execute(prgm, params = params, mode = 'fp')
    print "Return main thread: %f" % (ret)

    prgm = env.Program()
    code = prgm.get_stream()
    code.add(x86_isa.movd(xmm0, mr32))
    code.add(x86_isa.mov(ebx, mr32))
    code.add(x86_isa.movd(xmm1, ebx))
    code.add(x86_isa.paddq(xmm0, xmm1))
    code.add(x86_isa.pextrw(ecx, xmm0, 0))
    code.add(x86_isa.pxor(mm1, mm1))
    code.add(x86_isa.pinsrw(mm1, ecx, 0))
    code.add(x86_isa.movq2dq(xmm0, mm1))
    code.add(x86_isa.movdq2q(mm2, xmm0))
    code.add(x86_isa.movd(edx,mm2))
    code.add(x86_isa.movd(xmm5,edx))
    code.add(x86_isa.movd(ecx, xmm5))
    code.add(x86_isa.pxor(xmm6, xmm6))
    code.add(x86_isa.pinsrw(xmm6, ecx, 0))
    code.add(x86_isa.movd(eax, xmm6))
    code.add(x86_isa.emms())

    prgm += code
    prgm.print_code(hex = True)
    ret = proc.execute(prgm, params = params, mode = 'int')
    print "Return main thread: %d" % (ret)


    # Try the LOCK prefix
    prgm = env.Program()
    code = prgm.get_stream()

    code.add(x86_isa.xor(eax, eax))
    code.add(x86_isa.add(mr32, eax))
    code.add(x86_isa.add(mr32, eax, lock = True))

    prgm += code
    prgm.print_code(hex = True)

    proc.execute(prgm, params = params)
    return