about summary refs log tree commit diff stats
path: root/miasm2/expression
diff options
context:
space:
mode:
Diffstat (limited to 'miasm2/expression')
-rw-r--r--miasm2/expression/expression.py3
-rw-r--r--miasm2/expression/modint.py16
2 files changed, 12 insertions, 7 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):