how would i make my C++ functions callable in Assembly? i tried


#include <iostream>

using namespace std;

int sum(int ,int )__attribute__((CDECL));


int a;
int b;

int main()
{
      a=10;
      b=40;

    asm(".intel_syntax noprefix");
    asm("push _a");
    asm("push _b");
    asm("call _sum");
    asm("add esp,8");
    asm("mov _a,eax");
    asm(".att_syntax noprefix");
    cout<<"\n The sum of a + b is now "<<a;
    return 0;
}
int sum(int x,int y)
{

    return x+y;
}



but i get the error undefined reference to sum.

also in ASM, C global variable names are prefixed with an underscore what about local variables how would i reference them ?
Posted on 2010-12-07 03:21:43 by ashken
Try using the following:

extern "C" {
 int sum(int, int);
}

Depending on your compiler and OS the underscore may (or may not) get prepended.  Trying to interface with C++ name-mangling is notoriously difficult, which is why most routines are defined using cdecl ( and the fact that when writing shared libraries you must use cdecl if you expect your library to be sharable among users of different compilers!).

You can generate an assembly listing of your code when compiling to see the exact name being created.

As far as your local variables, they are referenced from the stack, unlike the global variables you define in your data section which you can reference by name in assembly code.  In other words, you can't make variables of local scope visible to external functions or methods by name.  The only way to get access to them is to pass on the stack either the value or the address in order for the called function to work on it.

Hope that helps.
Posted on 2010-12-07 08:41:18 by p1ranha
thanx @p1ranha the extern key word does the trick!
Posted on 2010-12-07 15:34:07 by ashken
ok if i have a fucntion like


int stuff()
{
    int x;
 
    return x;
}


what will be the address of x on the stack?? ,  ??
i have tried accessing x using inline assembly but it ends up an epic fail

Posted on 2010-12-10 16:33:08 by ashken

ok if i have a fucntion like


int stuff()
{
    int x;
 
    return x;
}



what will be the address of x on the stack?? ,  ??
i have tried accessing x using inline assembly but it ends up an epic fail



in 32-bit world would become something like this in assembly:



; offsets of function locals are negative values from ebp
%define x ebp - 4

extern _stuff  ; don't forget this when calling from another language
                ; I've assumed C language based upon your example
_stuff:        ; Thus the prepended underscore
  push ebp                  ; set up the function prologue
  mov ebp, esp              ;
  sub  esp, 4                ;  make room for your local var

; my little insertion to demonstrate initializing your local var
  xor eax, eax              ; set eax to 0
  mov dword, eax 

; code that does other stuff
  .
  .
  .
 
  mov eax, dword    ; store the return value in eax

  add esp, 4        ; function epilogue ( properly clean up the stack )
  pop ebp          ; restore original value of ebp
  ret              ; exit function with eax containing the return value


Note: The function prologue/epilogue can be optimized further but I don't want to confuse
the issue for you yet.  Stick with the above and you'll be safe  ;)

Also, make sure you add

#ifdef __cplusplus
    extern "C" {
        int stuff();    // <- your functions here
    }
#endif

to your header if compiling C++ source code to properly link to your assembler routine.
hope that helps!
Posted on 2010-12-10 18:16:58 by p1ranha



; offsets of function locals are negative values from ebp
%define x ebp - 4

extern _stuff   ; don't forget this when calling from another language
                ; I've assumed C language based upon your example
_stuff:         ; Thus the prepended underscore
   push ebp                   ; set up the function prologue
   mov ebp, esp               ;
   sub  esp, 4                ;  make room for your local var

; my little insertion to demonstrate initializing your local var
   xor eax, eax               ; set eax to 0
   mov dword, eax 

; code that does other stuff
   .
   .
   .
   
   mov eax, dword     ; store the return value in eax

   add esp, 4        ; function epilogue ( properly clean up the stack )
   pop ebp           ; restore original value of ebp
   ret               ; exit function with eax containing the return value



One correction, use global for exporting and extern for importing.  'extern _stuff' is supposed to be 'global _stuff'
Posted on 2010-12-10 19:37:08 by Synfire
Good catch, Bryant.  I've been bitten by "idea" code again.  :D
Posted on 2010-12-12 09:03:00 by p1ranha

ok if i have a fucntion like

int stuff()
{
    int x;
    return x;
}


what will be the address of x on the stack?? ,  ??
i have tried accessing x using inline assembly but it ends up an epic fail


That Depends. ;)

int can even be 8-bit value, for a selected compilers. For a big one (I mean 64-bit and forth), several issues are in force. Keywords: "Agner Fog". He tooks a hassle to combine those calling conventions in one convenient document.
Posted on 2010-12-14 14:37:35 by baldr