Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to learn assembly for an upcoming exam, and this is the piece of inline assembly code I have written using msvc.

C++
<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?
Posted
Updated 25-Oct-23 10:43am
v2

Look at your code: You start off with x as 144, copy that into eax increment it by one and compare the value to 144. If it is not equal to 144 you go round the loop and increment it again.
Only when eax contains 144 - the original value of x - do you exit the loop and set the value of x to ... 144 again.

Hence, it looks like eax never gets incremented because the processor does a quick flash through every possible value of eax other than 144 before printing the value. If you had a slow enough processor you would notice a delay ...
 
Share this answer
 
Your code works but you have the huge loop in there so your code increment EAX to 145 in first step then increment it into 146 and so on, as the register in unsigned it wrap to maximum value and comes back to 0 and then it continue incrementing so once it become back to 144 it exit the loop so you have the result in x back the 144, I think you should modify logic of loop exit: try to change jnz to jz - this will work based on your description.
 
Share this answer
 
Comments
Shobhit_23 16-Oct-23 9:45am    
Okay, a slightly different doubt now, to verify this I have now kept a count variable(in register ebx), I am incrementing it on each increment-ation of eax. I am now comparing it against 142, so that the value of ebx doesn't get to it's maximum. I was expecting to see a large value(around 2^16) in ebx, but what I am seeing is -2. Can you explain it?

#include <stdio.h>

int main() {
	int x = 144;
	int y;
	__asm {
		mov eax, x
		mov ebx, 0
red:	inc eax
		inc ebx
		cmp eax, 142
		jnz red
		mov x, eax
		mov y, ebx
	};
	printf("%d\n", x);
	printf("%d\n", y);
	return 0;
}
0x01AA 16-Oct-23 10:37am    
You can use also rax or rbx, which are 64 bit
Maxim Kartavenkov 16-Oct-23 10:48am    
He is on msvc
0x01AA 16-Oct-23 11:28am    
And that means?
Maxim Kartavenkov 16-Oct-23 11:32am    
MSVC does not supports inline __asm directive on x64 platform build - so he build code on x86 platform this means there is no 64 bit registers available

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900