diff --git a/src/cc65/assignment.c b/src/cc65/assignment.c
index eed097272..0cdf2772d 100644
--- a/src/cc65/assignment.c
+++ b/src/cc65/assignment.c
@@ -163,6 +163,8 @@ void Assignment (ExprDesc* Expr)
 
         CodeMark AndPos;
         CodeMark PushPos;
+        CodeMark RhsPos;
+        CodeMark RhsEndPos;
 
         unsigned Mask;
         unsigned Flags;
@@ -194,15 +196,20 @@ void Assignment (ExprDesc* Expr)
         g_push (Flags, 0);
 
      	/* Read the expression on the right side of the '=' */
+        GetCodePos (&RhsPos);
      	hie1 (&Expr2);
+        GetCodePos (&RhsEndPos);
 
      	/* Do type conversion if necessary. Beware: Do not use char type
          * here!
          */
      	TypeConversion (&Expr2, ltype);
 
-        /* Special treatment if the value is constant */
-        if (ED_IsConstAbsInt (&Expr2)) {
+        /* Special treatment if the value is constant. */
+        /* Beware: Expr2 may contain side effects, so there must not be
+         * code generated for Expr2.
+         */
+        if (ED_IsConstAbsInt (&Expr2) && CodeRangeIsEmpty (&RhsPos, &RhsEndPos)) {
 
             /* Get the value and apply the mask */
             unsigned Val = (unsigned) (Expr2.IVal & Mask);