New to ASM...

This is a discussion on New to ASM... within the C++ Programming forums, part of the General Programming Boards category; I'm new to ASM (Just started 10 min ago - inline, to be exact), and am having what sounds to ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    47

    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?

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    One more question: I can load 2 into EBX, but not EDX. Is EDX reserved somehow?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    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.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    47
    Found the answer: edx stores the remainder in a DIV operation, so edx cannot be the argument for a DIV.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  2. Asm + C++
    By JaWiB in forum C++ Programming
    Replies: 17
    Last Post: 06-26-2003, 03:13 PM
  3. Understanding ASM
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 07-12-2002, 07:39 PM
  4. asm mov
    By manu in forum C++ Programming
    Replies: 1
    Last Post: 12-15-2001, 11:59 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21