I am trying to learn assembly for an upcoming exam, and this is the piece of inline assembly code I have written using msvc.
<pre>#include <stdio.h>
int main() {
int x = 144;
__asm {
mov eax, x
red: inc eax
cmp eax, 144
jnz red
mov x, eax
};
printf("%d\n", x);
return 0;
}
I was expecting the value of
eax
register to increment to 145 and then to be moved to 'x', but I am getting the output as 144 instead of 145. Someone please explain what's going on.
Also, does the
cmp
instruction 'set the flag as if it had performed subtraction on the operand'? If that would be the case, in case of
x = 144
, the value of zero should never be set to
0
? Is my understanding correct here?
What I have tried:
Assigning a different value to
x
like,
x = 5
, works as expected and the contents of
eax
register increments till
144
, but I don't why it doesn't increment to
145
when
x
is originally assigned as
144
?