about summary refs log tree commit diff stats
path: root/miasm2
diff options
context:
space:
mode:
authorCamille Mougey <commial@gmail.com>2015-07-30 08:31:34 +0200
committerCamille Mougey <commial@gmail.com>2015-07-30 08:31:34 +0200
commitea4ca3425e49c59fa4cb9b5b5b91cc9431dcb843 (patch)
treedda7deef86d31ed01361eca60719f81ef2db79a5 /miasm2
parent817fc666eac74c802d4d592f50a3872a3197f4a5 (diff)
parent5802dfd9ff93f755b648ff4a3ba236a80c121fb3 (diff)
downloadmiasm-ea4ca3425e49c59fa4cb9b5b5b91cc9431dcb843.tar.gz
miasm-ea4ca3425e49c59fa4cb9b5b5b91cc9431dcb843.zip
Merge pull request #199 from serpilliere/fix_x86_sem
Fix x86 sem
Diffstat (limited to 'miasm2')
-rw-r--r--miasm2/arch/x86/sem.py148
-rw-r--r--miasm2/expression/expression_helper.py19
2 files changed, 143 insertions, 24 deletions
diff --git a/miasm2/arch/x86/sem.py b/miasm2/arch/x86/sem.py
index 6b5ae583..f99d2d9d 100644
--- a/miasm2/arch/x86/sem.py
+++ b/miasm2/arch/x86/sem.py
@@ -20,6 +20,7 @@ import miasm2.expression.expression as m2_expr
 from miasm2.expression.simplifications import expr_simp
 from miasm2.arch.x86.regs import *
 from miasm2.arch.x86.arch import mn_x86, repeat_mn, replace_regs
+from miasm2.expression.expression_helper import expr_cmps, expr_cmpu
 from miasm2.ir.ir import ir, irbloc
 import math
 import struct
@@ -1212,7 +1213,9 @@ def jz(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(zf, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(zf,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e = [m2_expr.ExprAff(meip, dst_o),
          m2_expr.ExprAff(ir.IRDst, dst_o),
      ]
@@ -1224,7 +1227,8 @@ def jcxz(ir, instr, dst):
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
     dst_o = m2_expr.ExprCond(mRCX[instr.mode][:16],
-                             n, dst).zeroExtend(instr.mode)
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1235,7 +1239,8 @@ def jecxz(ir, instr, dst):
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
     dst_o = m2_expr.ExprCond(mRCX[instr.mode][:32],
-                             n, dst).zeroExtend(instr.mode)
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1245,7 +1250,9 @@ def jrcxz(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(mRCX[instr.mode], n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(mRCX[instr.mode],
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1255,7 +1262,9 @@ def jnz(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(zf, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(zf,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1265,7 +1274,9 @@ def jp(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(pf, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(pf,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1275,7 +1286,9 @@ def jnp(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(pf, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(pf,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1285,7 +1298,9 @@ def ja(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(cf | zf, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(cf | zf,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1295,7 +1310,9 @@ def jae(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(cf, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(cf,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1305,7 +1322,9 @@ def jb(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(cf, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(cf,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1315,7 +1334,9 @@ def jbe(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(cf | zf, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(cf | zf,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1325,7 +1346,9 @@ def jge(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(nf - of, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(nf - of,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1335,7 +1358,9 @@ def jg(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(zf | (nf - of), n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(zf | (nf - of),
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1345,7 +1370,9 @@ def jl(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(nf - of, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(nf - of,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1355,7 +1382,9 @@ def jle(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(zf | (nf - of), dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(zf | (nf - of),
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1365,7 +1394,9 @@ def js(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(nf, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(nf,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1375,7 +1406,9 @@ def jns(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(nf, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(nf,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1385,7 +1418,9 @@ def jo(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(of, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(of,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1395,7 +1430,9 @@ def jno(ir, instr, dst):
     e = []
     meip = mRIP[instr.mode]
     n = m2_expr.ExprId(ir.get_next_label(instr), dst.size)
-    dst_o = m2_expr.ExprCond(of, n, dst).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(of,
+                             n.zeroExtend(instr.mode),
+                             dst.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1410,7 +1447,9 @@ def loop(ir, instr, dst):
 
     n = m2_expr.ExprId(ir.get_next_label(instr), instr.mode)
     c = myecx - m2_expr.ExprInt_from(myecx, 1)
-    dst_o = m2_expr.ExprCond(c, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(c,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(myecx, c))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
@@ -1432,7 +1471,9 @@ def loopne(ir, instr, dst):
     c &= zf ^ m2_expr.ExprInt1(1)
 
     e.append(m2_expr.ExprAff(myecx, myecx - m2_expr.ExprInt_from(myecx, 1)))
-    dst_o = m2_expr.ExprCond(c, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(c,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -1451,7 +1492,9 @@ def loope(ir, instr, dst):
                  m2_expr.ExprInt1(0))
     c &= zf
     e.append(m2_expr.ExprAff(myecx, myecx - m2_expr.ExprInt_from(myecx, 1)))
-    dst_o = m2_expr.ExprCond(c, dst, n).zeroExtend(instr.mode)
+    dst_o = m2_expr.ExprCond(c,
+                             dst.zeroExtend(instr.mode),
+                             n.zeroExtend(instr.mode))
     e.append(m2_expr.ExprAff(meip, dst_o))
     e.append(m2_expr.ExprAff(ir.IRDst, dst_o))
     return e, []
@@ -2416,9 +2459,65 @@ def rdtsc(ir, instr):
     return e, []
 
 
-# XXX TODO
 def daa(ir, instr):
-    return [], None
+    e = []
+    r_al = mRAX[instr.mode][:8]
+
+    cond1 = expr_cmpu(r_al[:4], m2_expr.ExprInt_fromsize(4, 0x9)) | af
+    e.append(m2_expr.ExprAff(af, cond1))
+
+
+    cond2 = expr_cmpu(m2_expr.ExprInt8(6), r_al)
+    cond3 = expr_cmpu(r_al, m2_expr.ExprInt8(0x99)) | cf
+
+
+    cf_c1 = m2_expr.ExprCond(cond1,
+                             cf | (cond2),
+                             m2_expr.ExprInt1(0))
+    new_cf = m2_expr.ExprCond(cond3,
+                              m2_expr.ExprInt1(1),
+                              m2_expr.ExprInt1(0))
+    e.append(m2_expr.ExprAff(cf, new_cf))
+
+    al_c1 = m2_expr.ExprCond(cond1,
+                             r_al + m2_expr.ExprInt8(6),
+                             r_al)
+
+    new_al = m2_expr.ExprCond(cond3,
+                              al_c1 + m2_expr.ExprInt8(0x60),
+                              al_c1)
+    e.append(m2_expr.ExprAff(r_al, new_al))
+    return e, []
+
+def das(ir, instr):
+    e = []
+    r_al = mRAX[instr.mode][:8]
+
+    cond1 = expr_cmpu(r_al[:4], m2_expr.ExprInt_fromsize(4, 0x9)) | af
+    e.append(m2_expr.ExprAff(af, cond1))
+
+
+    cond2 = expr_cmpu(m2_expr.ExprInt8(6), r_al)
+    cond3 = expr_cmpu(r_al, m2_expr.ExprInt8(0x99)) | cf
+
+
+    cf_c1 = m2_expr.ExprCond(cond1,
+                             cf | (cond2),
+                             m2_expr.ExprInt1(0))
+    new_cf = m2_expr.ExprCond(cond3,
+                              m2_expr.ExprInt1(1),
+                              cf_c1)
+    e.append(m2_expr.ExprAff(cf, new_cf))
+
+    al_c1 = m2_expr.ExprCond(cond1,
+                             r_al - m2_expr.ExprInt8(6),
+                             r_al)
+
+    new_al = m2_expr.ExprCond(cond3,
+                              al_c1 - m2_expr.ExprInt8(0x60),
+                              al_c1)
+    e.append(m2_expr.ExprAff(r_al, new_al))
+    return e, []
 
 
 def aam(ir, instr, a):
@@ -3272,6 +3371,7 @@ mnemo_func = {'mov': mov,
               'cqo': cqo,
 
               'daa': daa,
+              'das': das,
               'aam': aam,
               'aad': aad,
               'aaa': aaa,
diff --git a/miasm2/expression/expression_helper.py b/miasm2/expression/expression_helper.py
index 3555530a..196ad5cd 100644
--- a/miasm2/expression/expression_helper.py
+++ b/miasm2/expression/expression_helper.py
@@ -533,3 +533,22 @@ class ExprRandom(object):
             cls.generated_elements = {}
 
         return got
+
+def _expr_cmp_gen(arg1, arg2):
+    return (arg2 - arg1) ^ ((arg2 ^ arg1) & ((arg2 - arg1) ^ arg2))
+
+def expr_cmpu(arg1, arg2):
+    """
+    Returns a one bit long Expression:
+    * 1 if @arg1 is strictly greater than @arg2 (unsigned)
+    * 0 otherwise.
+    """
+    return (_expr_cmp_gen(arg1, arg2) ^ arg2 ^ arg1).msb()
+
+def expr_cmps(arg1, arg2):
+    """
+    Returns a one bit long Expression:
+    * 1 if @arg1 is strictly greater than @arg2 (signed)
+    * 0 otherwise.
+    """
+    return _expr_cmp_gen(arg1, arg2).msb()