diff options
| author | serpilliere <serpilliere@users.noreply.github.com> | 2020-06-09 17:23:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-09 17:23:44 +0200 |
| commit | 90da6f0dc4c3ad03a40e922d99260f3fd6c3aa2d (patch) | |
| tree | 8d4eb792e1924427bf7080d6c40efe2d31c19120 | |
| parent | 042a6441ccdbc4312ba43e926b93a3f35263edb6 (diff) | |
| parent | 196397dd71cf10af77629fe1b66ecc7ba94e8ec4 (diff) | |
| download | miasm-90da6f0dc4c3ad03a40e922d99260f3fd6c3aa2d.tar.gz miasm-90da6f0dc4c3ad03a40e922d99260f3fd6c3aa2d.zip | |
Merge pull request #1145 from 6oclock/bcdadd_simplifications_python
Adding simplifications for bcdadd and bcdadd_cf with python
| -rw-r--r-- | miasm/expression/simplifications.py | 2 | ||||
| -rw-r--r-- | miasm/expression/simplifications_common.py | 51 |
2 files changed, 53 insertions, 0 deletions
diff --git a/miasm/expression/simplifications.py b/miasm/expression/simplifications.py index a56aa0f8..3f54b158 100644 --- a/miasm/expression/simplifications.py +++ b/miasm/expression/simplifications.py @@ -63,6 +63,8 @@ class ExpressionSimplifier(ExprVisitorCallbackBottomToTop): simplifications_common.simp_test_zeroext_inf, simplifications_common.simp_cond_inf_eq_unsigned_zero, simplifications_common.simp_compose_and_mask, + simplifications_common.simp_bcdadd_cf, + simplifications_common.simp_bcdadd, ], m2_expr.ExprSlice: [ diff --git a/miasm/expression/simplifications_common.py b/miasm/expression/simplifications_common.py index ae3494c1..932db49a 100644 --- a/miasm/expression/simplifications_common.py +++ b/miasm/expression/simplifications_common.py @@ -1692,3 +1692,54 @@ def simp_compose_and_mask(_, expr): else: out.append(arg) return expr + +def simp_bcdadd_cf(_, expr): + """bcdadd(const, const) => decimal""" + if not(expr.is_op('bcdadd_cf')): + return expr + arg1 = expr.args[0] + arg2 = expr.args[1] + if not(arg1.is_int() and arg2.is_int()): + return expr + + carry = 0 + res = 0 + nib_1, nib_2 = 0, 0 + for i in range(0,16,4): + nib_1 = (arg1.arg >> i) & (0xF) + nib_2 = (arg2.arg >> i) & (0xF) + + j = (carry + nib_1 + nib_2) + if (j >= 10): + carry = 1 + j -= 10 + j &= 0xF + else: + carry = 0 + return ExprInt(carry, 1) + +def simp_bcdadd(_, expr): + """bcdadd(const, const) => decimal""" + if not(expr.is_op('bcdadd')): + return expr + arg1 = expr.args[0] + arg2 = expr.args[1] + if not(arg1.is_int() and arg2.is_int()): + return expr + + carry = 0 + res = 0 + nib_1, nib_2 = 0, 0 + for i in range(0,16,4): + nib_1 = (arg1.arg >> i) & (0xF) + nib_2 = (arg2.arg >> i) & (0xF) + + j = (carry + nib_1 + nib_2) + if (j >= 10): + carry = 1 + j -= 10 + j &= 0xF + else: + carry = 0 + res += j << i + return ExprInt(res, arg1.size) |