-
New to ASM...
I'm new to ASM (Just started 10 min ago - inline, to be exact), and am having what sounds to me like a syntax error. What confuses me is that the same line of code in a different ASM block works fine!
When attempting to compile the following code...
Code:
unsigned int x = 100;
unsigned int result;
//Intention: x = (x * (x + 1)) / 2
_asm
{
mov eax, x //eax = x
mov ecx, eax //ecx = eax
inc ecx //ecx += 1
mul ecx //eax *= ecx
div 2 //eax /= ecx
mov result, eax //result = eax
}
cout << result << "\n";
...I get the following error: "improper operand type" at the last line of assembly code (mov result, eax). What I don't understand at all is how I can have the same line in some previous inline ASM (which, btw, should return the same value), and have it work fine! That (working) code is as follows:
Code:
unsigned int x = 100;
unsigned int result;
_asm
{
xor eax, eax //eax will store the cumulative sum
mov ecx, x //enter x into the loop counter
label:
add eax, ecx //add the counter value to the cumulative sum
loop label //repeat
mov result, eax //set result value
}
What is the difference between the two lines of ASM in the two different blocks? And why will the first one not work?
-
The problem isn't the final line, it's 'div 2'. Something like this should work -
Code:
_asm
{
mov ebx, x
mov eax, ebx
inc ebx
mul ebx
mov ebx,2
div ebx
mov result,eax //result = eax
}
-
I've tracked down the error to the div. That's where the real problem lies. In fact, if I bit shift right by 1, it works. But I'd still like to know how to perform integer division!
-
Thanks, I didn't see your reply before. I tracked it down, with a friend, to be the same thing you just said. You can add, sub, and mov with constants, but not mul or div.
But, all in all, I'm finding this ASM stuff to be easier than people make it out to be.
Thanks a lot anyway.
-
One more question: I can load 2 into EBX, but not EDX. Is EDX reserved somehow?
-
Your last question has to do with the types of the registers (i'll assume you have no idea what I'm talking about). Check out http://www.xs4all.nl/~smit/asm01001.htm for a thorough explanation of what each register does.
-
I know that the registers ax, bx, etc., hold 16 bit values - aka shorts. I also know that the extended registers - eax, ebx, etc. - hold 32-bit values - aka ints. And I know there are some registers that point to areas in memory, but I haven't gotten into them yet. Thats about it. So that link should be helpful. Thanks.
-
Found the answer: edx stores the remainder in a DIV operation, so edx cannot be the argument for a DIV.