1

Is there any difference between the following two groups of instructions in assembly? When should I use the first case and when should I use the second?

case 1:

INC SI
INC SI

case 2:

ADD SI, 02
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
Klea Sanka
  • 25
  • 3
  • 4
  • On some older processors, perhaps 286 or older, LEA SI, 2[SI] was faster than add immediate or two increments. – rcgldr Mar 04 '15 at 05:47

3 Answers3

4

inc leaves the carry flag unchanged, that is occasionally useful (long addition for example). But it also has funny effects sometimes. On some processors (Core2 and Nehalem), reading any part of the flags after they have been "partially updated" causes a stall of up to 7 cycles. Using both the carry and some other flag after an inc (but this generally does not make sense to do) has a penalty on almost all processors, even the ones that cleverly "split" the flags in several parts. On P4, inc has the penalty (instead of an instruction that reads its flags), in the form of a false dependency on the previous flags, so it's pretty much unavoidable except by just not using inc.

Since these were 16 bit instructions, they may cause more problems (partial register write, length changing prefix depending on the mode)

harold
  • 61,398
  • 6
  • 86
  • 164
1

The first (inc) does not need an explicit amount to increase (always 1), and thus be encoded more efficiently.

On some (really old) processors that could mean it can be scheduled more efficiently. But since both are relatively simple operations, the difference won't be noticable on modern x86 CPUs without specially crafted code.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
0

The second may require extra storage and potentially extra time to fetch it out of storage. Some machine instructions have space for small constant values to be included with the instruction. If so, then the first pair of instructions might take more time than the second case's single instruction.

So it depends on the machine the assembly language is for.

Alan
  • 716
  • 5
  • 15