EnglishFrenchGermanSpainItalianDutchPortugueseRussianKoreanJapaneseArabic Chinese Simplified

Saturday, April 16, 2011

Calling functions with Inline Assembly

Compiles under Visual C++ 2008 as a console application and should compile with Visual C++ 2005


#include <iostream>

// Function that adds to integers and returns the result.
int add(int num1, int num2)
{
    return (num1+num2);
}

int main()
{
    // This variable will hold our return value
    int returnValue;

    // Call the inline assembler
    // Note that the standard code in C/C++ to achieve the same
    // result is simply "int returnValue = add(16, 10);".
    __asm
    {
        // Push parameters on to the stack (arguments pushed in 
        // right to left order using the standard cdecl calling convention)
        push 10       
        push 16

        // Call the add function
        call add   

        // We should now restore the stack pointer. We have pushed two
        // values on to the stack, each element on the stack
        // takes up 4 bytes (32 bits), so we need to increment
        // the stack pointer 8 places.
        add esp, 8       

        // The cdecl calling convention puts the return value in the EAX register
        // so we move the value stored in EAX into our returnValue variable
        mov returnValue, eax
    }

    // Output the result
    std::cout << "Return value = " << returnValue;

    std::cin.get();
    return 0;
}

Artikel Terkait:

Comments :

0 comments to “Calling functions with Inline Assembly”

Post a Comment

 

Copyright © 2009 by Learn Technology

Template by Blogger Templates | Powered by Blogger