Thread: Using __asm

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Using __asm

    I have a problem. My ASM code works fine when included as part of main, but when I make a function that takes a reference it doesn't work. What I mean is:
    Code:
    // The following function generates no errors,
    // but when called does not effect the variable passed.
    // If the __asm lines are put in the main program, however
    // and used with integer num, it works.
    // How do I use assembly lines to access variables passed
    // to functions? =|
    void zapit (int &num) {
        __asm mov ecx, [num]
        __asm xor  ecx, ecx
        __asm mov [num], ecx
    }

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    uh...

    ¬¬ ;

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i think your problem is here:
    void zapit(int &num) ....

    you feed the reference into the function when its called to let it know where the variable is. you need to have a pointer to accept the reference. in other words:
    void zapit(int *num) ....
    i know almost nothing about inline asm

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You could just do -

    Code:
    void zapit (int& num) {
    	
    		__asm mov eax,num		
    		__asm mov [eax],0
    
        
    }

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > __asm xor ecx, ecx

    I don't know any inline assembly, but isn't this just xoring the number with itself? (just making it 0)

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >I don't know any inline assembly, but isn't this just xoring the number with itself? (just making it 0)

    Yes, it's a quicker instruction than mov so is often used when setting a register to zero , but in the above function it's not the register that needs setting to zero so I don't think it would be of any use.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    That's interesting - thanks Sorensen...

    And I'm with ygf - why would you have the function prototype the way you do, and not the way he does?

    IOW, why this:
    void foo(int& bar)

    instead of this:
    void foo(int *bar)

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    It's just a reference instead of a pointer, there's not really any difference in this example (except how the variable is passed into the function). Both would do exactly the same thing, using the reference cleans up the syntax a little. Although it may not be immeadiately clear when you are passing a parameter into a function by reference instead of value using C++ references.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think you are changing the address instead of the value. Try something similar to this:
    Code:
    // The following function generates no errors,
    // but when called does not effect the variable passed.
    // If the __asm lines are put in the main program, however
    // and used with integer num, it works.
    // How do I use assembly lines to access variables passed
    // to functions? =|
    void zapit (int &num) {
        __asm mov di, num
        __asm mov ecx, [di]
        __asm xor  ecx, ecx
        __asm mov [di], ecx
    }
    I'm not sure if I have the correct register names. I'm using an old compiler. Here is what I used:
    Code:
    void zapit (int &num) {
        __asm mov di, num
        __asm mov ax, [di]
        __asm xor ax, ax
        __asm mov [di], ax
    }

  10. #10
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Surprising that people don't know what references are o_O;

    Anyway... um, I don't really care what it does, I'm trying to get communication with the variable I passed. This asm code works, but not in functions... and I want to know how to get it to work hehe...

    None of the suggestions have worked so far... I think ASM doesn't understand references or something, but it creates an error when I try to use pointers

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What compiler are you using?

  12. #12
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Originally posted by vVv
    >Surprising that people don't know what references are o_O;

    Score: -1, Troll.

    #include <iostream.h>
    int buf;
    void blah( int& ref )
    {
    buf = ref;
    __asm__( "
    movl buf,%eax;
    xorl %eax,%eax;
    movl %eax,buf;
    ");
    ref = buf;
    }

    main()
    {
    int var =20;
    blah( var );
    cout << var << endl;
    }
    That's not really answering the question. You're using C/C++, to assign the value to the reference.


    >I think ASM doesn't understand references or something

    ASM understands references as they are just the same as pointers underneath the syntax. However, the mnemonics used by C/C++ compilers for inline assembly varies from compiler to compiler. This definitely works on MSVC for all values of num -

    Code:
    void zapit (int& num) {
    	
    		__asm mov eax,[num]		
    		__asm mov DWORD PTR[eax],0
      
    }

  13. #13
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ...or using a similar method to your original -

    Code:
    void zapit (int& num) {
    	
        	__asm mov eax,[num]	
            __asm xor ecx,ecx
    	__asm mov [eax],ecx
      
    }

Popular pages Recent additions subscribe to a feed