I'm trying to increase a number value by using an inline assembly in C++. The reason that I do it that way is to practice my "inline assembly" skills.
Well that's what I've done so far:
void main()
{
int x;
cout << "Please enter a number ";
cin >> x;
cout << "The number you entered is: " << x << "\n";
foo(&x);
cout << "The new number is: " << x;
cin >> x;
}
void foo(int *x)
{
__asm
{
inc [x]
};
}
And the value never changed.
You are incrementing the value of x
, actually. X
in terms of assembly language is a constant containing the address of x
variable (of function foo
). Which, in turn, contains the address of main
's x
. So, inc [x]
causes an increment of the pointer. You need to increment the value stored at the address [x]
, like inc [[x]]
. Of course you can not do it in one instruction in assembly language since you need two memory accesses: to know where the value is stored and to actually increment the value. So I'd advise a code like this:
push eax
mov eax, [x]
inc dword ptr [eax]
pop eax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With