[Vollbild]  [Home]

Call C++-functions and use C++-variables from inside NASM

- Function names in gcc :

- Variable-names in gcc :

And like this you can call a function written in gcc :

Any c++ program :
test.cpp :-compil it with: g++ -c test.cpp -o test_cpp.o
------------------------
int myvar=2; //global variable, however it isn't set to 2 in my test program.
char myextravar;//global variable
int sqr(){//global function - sqr(void) -> _Z3sqrv
     myvar = myvar*myvar;//myvarª
     return myvar;//If you have such a line eax holds the value.
     }
void normal(){//-> _Z6normalv
     myvar = myvar+3;//Here myvar is hanged at the global position
     }
------------------------

Your NASM program :
test.nasm :-compil it with: nasm -f elf test.nasm
------------------------
section .data

EXTERN myvar;this makes myvar from the c++ programm useable for you
EXTERN _Z3sqrv;this makes the function int sqr(void) from the c++ programm useable for you
EXTERN _Z6normalv;this makes the function void normal(void) from the c++ programm useable for you

section .text
global _start

%include "collection.inc"

_start:
movecx,20
a:
call_Z3sqrv;call the global c++ function sqr()
calloutput;a standard output procedure (syscall) eax = output
call_Z6normalv;call the global c++ function normal()
moveax,[myvar];or you can get the c++ variabel into eax like this too
calloutput;a standard output procedure (syscall)
loopa;jmp to a if ecx > 0
ende:
moveax,1;system call number (sys_exit)
int$80;call kernel (EXIT)
------------------------

linking both object-files together

Now the bigest and easiest part :
That is done with : ld -s -o test test.o test_cpp.o
test.o is your NASM-object-file and should stand befor the c++-file test_cpp.o.
Now you have an executeable called 'test'.

Some Tips

I know something !  
 
Free Web Hosting