Calling C/C++ function from ASM code

Topics on common programming languages

Calling C/C++ function from ASM code

Postby Neo » Fri Oct 02, 2009 12:19 pm

Following steps need to be followed to call C/C++ functions from an ASM code.

  1. Compile C code and get the OBJ file
  2. Call the C/C++ function as   .call _funcname(parameters)
    Example:
    Code: Select all
             SWAP2      lastVal, reg1
             SWAP4      reg1, reg1

             .call      _write_to_disk(reg1)

             ZERO      lastN
             ZERO      lastVal
             B         savelastVN                  ; Branch


    There are few things to notice here.
    • The C/C++ function is write_to_disk. But we have added '_' prefix. This usual as the compiler adds '_' to functions in the symbol table
    • Whenever we pass a parameter from ASM, we need to make sure the C/C++ function is ready accept the size of a register. For example: In a 32-bit system where registers are 32-bit, the C/C++ function prototype must be something like write_to_disk(int value)

NOTE 1 : If the function is called from inline assembly make sure you don't put the '_'
NOTE 2 : If it is x86 assembly, the calling function is 'call' without the dot (.) prefix.
User avatar
Neo
Site Admin
Site Admin
 
Posts: 2591
Joined: Tue Jul 14, 2009 8:37 pm
Cash on hand: 219,868.20
Bank: 15,536,454.00
Location: Colombo/London
Medals: 2
EC_Bronze_Star (1) EC_Achievment (1)

Invitations sent: 94
Registered friends: 12
Highscores: 13
Reputation point: 72
Lieutenant

Re: Calling C/C++ function from ASM code

Postby Herath » Tue Aug 10, 2010 4:47 pm

I have seen programs Pushing parameters in to the stack before calling a function. (While disassembling little executable files.) . I am taking about x86 assembly.
User avatar
Herath
Support Team
Support Team
 
Posts: 432
Joined: Thu Aug 05, 2010 1:39 pm
Cash on hand: 1,861.30
Bank: 96,267.80
Medals: 1
EC_Achievment (1)

Invitations sent: 1
Registered friends: 0
Highscores: 6
Reputation point: 11
Corporal

Re: Calling C/C++ function from ASM code

Postby Neo » Tue Aug 10, 2010 6:15 pm

In Intel x86 asm, call and return of functions handled using push/pop (stack) as you said. An example is given below.

Code: Select all
__declspec(naked) unsigned long
SET_AA(unsigned long inputDWord )
{
    __asm
    {
        mov eax, [esp+4]
        mov al, 0xAA
        ret               // final value is in eax
    }
}

__declspec(naked) void
function(unsigned long inputDWord, unsigned long *outputDWord)
{
    _asm
    {
    // you need a prolog/epilog to make C happy
    // here's the prolog:
    push ebp
    mov ebp, esp

    mov ebx, inputDWord    // the value you're going to change
    mov ecx, outputDWord   // address of where to put the result

    push ebx
    call SET_AA // puts the result in eax
    pop ebx

    // copy the result to the thing ecx points to (*outputDWord)
    mov [ecx], eax

    // epilog to keep C happy
    pop ebp
    ret
    }
}
User avatar
Neo
Site Admin
Site Admin
 
Posts: 2591
Joined: Tue Jul 14, 2009 8:37 pm
Cash on hand: 219,868.20
Bank: 15,536,454.00
Location: Colombo/London
Medals: 2
EC_Bronze_Star (1) EC_Achievment (1)

Invitations sent: 94
Registered friends: 12
Highscores: 13
Reputation point: 72
Lieutenant

Re: Calling C/C++ function from ASM code

Postby Herath » Tue Aug 10, 2010 6:25 pm

Wow!. You seems to be great at asm. I like to learn that too. I know a bit. But I like to go further. There is no end to it!

Thank you for the explanation. I used to practice reverse engineering of software when I was home after A/Levels. I just had a memory of it. :)
User avatar
Herath
Support Team
Support Team
 
Posts: 432
Joined: Thu Aug 05, 2010 1:39 pm
Cash on hand: 1,861.30
Bank: 96,267.80
Medals: 1
EC_Achievment (1)

Invitations sent: 1
Registered friends: 0
Highscores: 6
Reputation point: 11
Corporal


Return to .Net & Other Programming

Who is online

Users browsing this forum: No registered users and 1 guest