about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--miasm2/expression/expression.py3
-rw-r--r--miasm2/expression/modint.py16
-rw-r--r--test/expression/expression.py14
-rw-r--r--test/expression/modint.py15
4 files changed, 40 insertions, 8 deletions
diff --git a/miasm2/expression/expression.py b/miasm2/expression/expression.py
index 0001dc24..ef7fa1f6 100644
--- a/miasm2/expression/expression.py
+++ b/miasm2/expression/expression.py
@@ -448,7 +448,8 @@ class ExprInt(Expr):
         return hash((EXPRINT, self.__arg, self.__size))
 
     def _exprrepr(self):
-        return "%s(0x%X)" % (self.__class__.__name__, self.__get_int())
+        return "%s(0x%X, %d)" % (self.__class__.__name__, self.__get_int(),
+                                 self.__size)
 
     def __contains__(self, e):
         return self == e
diff --git a/miasm2/expression/modint.py b/miasm2/expression/modint.py
index 90dabfac..b6a0e4ee 100644
--- a/miasm2/expression/modint.py
+++ b/miasm2/expression/modint.py
@@ -41,11 +41,15 @@ class moduint(object):
             return self.__class__(self.arg & y)
 
     def __div__(self, y):
+        # Python: 8 / -7 == -2 (C-like: -1)
+        # int(float) trick cannot be used, due to information loss
+        den = int(y)
+        num = int(self)
+        result_sign = 1 if (den * num) >= 0 else -1
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(int(float(self.arg) / y.arg))
-        else:
-            return self.__class__(int(float(self.arg) / y))
+        return ((abs(num) / abs(den)) * result_sign)
 
     def __int__(self):
         return int(self.arg)
@@ -64,11 +68,11 @@ class moduint(object):
             return self.__class__(self.arg << y)
 
     def __mod__(self, y):
+        # See __div__ for implementation choice
+        cls = self.__class__
         if isinstance(y, moduint):
             cls = self.maxcast(y)
-            return cls(self.arg - (y.arg * int(float(self.arg)/y.arg)))
-        else:
-            return self.__class__(self.arg - (y * int(float(self.arg)/y)))
+        return cls(self.arg - (y * (self / y)))
 
     def __mul__(self, y):
         if isinstance(y, moduint):
diff --git a/test/expression/expression.py b/test/expression/expression.py
index 58c0ca37..8e8b5e07 100644
--- a/test/expression/expression.py
+++ b/test/expression/expression.py
@@ -51,3 +51,17 @@ for expr in [
         print "For value %s" % consval.value
         for constraint in consval.constraints:
             print "\t%s" % constraint.to_constraint()
+
+# Repr
+for expr in [
+        cst1,
+        A,
+        ExprMem(cst1, 32),
+        ExprCond(cond1, cst1, cst2),
+        A + cst1,
+        ExprCompose(A, cst1),
+        A.msb(),
+        ExprAff(A, cst1),
+]:
+    print repr(expr)
+    assert expr == eval(repr(expr))
diff --git a/test/expression/modint.py b/test/expression/modint.py
index e7c19d0c..17c12907 100644
--- a/test/expression/modint.py
+++ b/test/expression/modint.py
@@ -8,7 +8,7 @@ d = uint1(0)
 e = uint1(1)
 
 f = uint8(0x1)
-
+g = int8(-3)
 
 print a, b, c
 print a + b, a + c, b + c
@@ -53,7 +53,20 @@ assert(f ^ f == 0)
 assert(f ^ 0 == f)
 assert(0 ^ f == f)
 assert(1 ^ f == 0)
+assert(c / g == -1)
+assert(c / -3 == -1)
+assert(c % g == 1)
+assert(c % -3 == 1)
 
 print e + c, c + e, c - e, e - c
 print 1000 * a
 print hex(a)
+
+define_int(128)
+define_uint(128)
+h = uint128(0x11223344556677889900AABBCCDDEEFF)
+i = int128(-0x9900AABBCCDDEEFF1122334455667788)
+
+assert(i / h == 6)
+assert(i % h == 0x3221aa32bb43cd58d9cc54dd65ee7e)
+