ShadowCrossZero
ShadowCrossZero

Reputation: 429

How do you know if an assembly instruction stores in a new variable or not?

Let's say the value 4 is stored in register "%eax" (and the C variable for it is "varX") and the value 3 is stored in register %edx (and the C variable for it is "varY"). If the assembly code is "subl %edx, %eax", then in C how do I know if the instruction translates to

int varZ = varX - varY;

or

int varX = varX - varY;

If the second case is correct, then what assembly instruction would cause the first case to be true?

Upvotes: 0

Views: 120

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490418

Looking at that instruction in isolation, you probably can't know with any certainty whether the target is varX, varZ, or something else entirely.

Something like int varZ = varX - varY; might compile to a sequence like this (using Intel rather than AT&T syntax):

mov eax, varX
sub eax, varY
mov varZ, eax

Likewise, for varX = varX - varY;, you might get something like this:

mov eax, varX
sub eax, varY
mov varX, eax

The sub just gives the difference of varX and varY. If you store the result to varX, then it's giving varX = ..., but if you store the result to varZ, then it's giving varZ = ....

It's also possible that it's enregistered varX in eax, in which case the sub by itself might be equivalent to varX = varX - varyY;, but without seeing how the value is used, it's impossible to be sure.

Upvotes: 0

Related Questions