diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 939fe3eda62321cecf8778e8631c3b878806e8bf..e92589b06d3fa10f046e10c8563dc5e4451e09d4 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -1214,17 +1214,20 @@ DynamicModel::writeDynamicBlockBytecode(const string &basename) const
               fstpr.write(code_file, instruction_number);
             }
         }
+
+      /* Write temporary terms for derivatives. This is done before FENDEQU,
+         because residuals of a subsequent block may depend on temporary terms for
+         the derivatives of the present block.
+
+         Also note that in the case of “evaluate” blocks, derivatives are not
+         computed in the “evaluate” mode; still their temporary terms must be
+         computed even in that mode, because for the same reason as above they may
+         be needed in subsequent blocks. */
+      write_eq_tt(blocks[block].size);
+
       FENDEQU_ fendequ;
       fendequ.write(code_file, instruction_number);
 
-      /* If the block is not of type “evaluate backward/forward”, then we write
-         the temporary terms for derivatives at this point, i.e. before the
-         JMPIFEVAL, because they will be needed in both “simulate” and
-         “evaluate” modes. */
-      if (simulation_type != BlockSimulationType::evaluateBackward
-          && simulation_type != BlockSimulationType::evaluateForward)
-        write_eq_tt(blocks[block].size);
-
       // Get the current code_file position and jump if eval = true
       streampos pos1 = code_file.tellp();
       FJMPIFEVAL_ fjmp_if_eval(0);
@@ -1345,13 +1348,6 @@ DynamicModel::writeDynamicBlockBytecode(const string &basename) const
       code_file.seekp(pos3);
       prev_instruction_number = instruction_number;
 
-      /* If the block is of type “evaluate backward/forward”, then write the
-         temporary terms for derivatives at this point, because they have not
-         been written before the JMPIFEVAL. */
-      if (simulation_type == BlockSimulationType::evaluateBackward
-          || simulation_type == BlockSimulationType::evaluateForward)
-        write_eq_tt(blocks[block].size);
-
       // Write the derivatives for the “evaluate” mode
       for (const auto &[indices, d] : blocks_derivatives[block])
         {
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index ffea4fb8f3984423eee9ddf6e9e7499068f02443..7adf0a450f9f907ca6bde1798e08fb06c2bc6337 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -700,6 +700,17 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
               fstpr.write(code_file, instruction_number);
             }
         }
+
+      /* Write temporary terms for derivatives. This is done before FENDEQU,
+         because residuals of a subsequent block may depend on temporary terms for
+         the derivatives of the present block.
+
+         Also note that in the case of “evaluate” blocks, derivatives are not
+         computed in the “evaluate” mode; still their temporary terms must be
+         computed even in that mode, because for the same reason as above they may
+         be needed in subsequent blocks. */
+      write_eq_tt(blocks[block].size);
+
       FENDEQU_ fendequ;
       fendequ.write(code_file, instruction_number);
 
@@ -707,9 +718,6 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
       if (simulation_type != BlockSimulationType::evaluateBackward
           && simulation_type != BlockSimulationType::evaluateForward)
         {
-          // Write temporary terms for derivatives
-          write_eq_tt(blocks[block].size);
-
           switch (simulation_type)
             {
             case BlockSimulationType::solveBackwardSimple:
@@ -876,14 +884,16 @@ StaticModel::writeStaticBlockBytecode(const string &basename) const
               fstpr.write(code_file, instruction_number);
             }
         }
+
+      // Write temporary terms for derivatives
+      // Same remark as above
+      write_eq_tt(blocks[block].size);
+
       FENDEQU_ fendequ_l;
       fendequ_l.write(code_file, instruction_number);
 
       // The Jacobian if we have to solve the block determinsitic bloc
 
-      // Write temporary terms for derivatives
-      write_eq_tt(blocks[block].size);
-
       switch (simulation_type)
         {
         case BlockSimulationType::solveBackwardSimple: