flat assembler
Message board for the users of flat assembler.
Index
> Linux > [AMD64] HOWTO: Mix c++ (GCC) and FASM |
Author |
|
Nikso 08 Oct 2006, 10:52
Hi there!
So I needed to mix asm and c++ under Linux for 64 bit OS development. As you may know, in an AMD64 environment GCC use ABI 0.98 specification (see here). This means that parameters are no longer passed thru the stack but registers are used instead. NOTE: this example is not complete yet. I was not able to refer exported variables (look at commented lines in the code) AsmPart.asm Code: ; Writed for board.flatassembler.net by Nikso ; CppAndAsm ; ; x86-64 GCC calling convention (ABI 0.9 ; GCC will use the following register (in order) for parameters passing: ; RDI, RSI, RDX, RCX, R8, R9, (stack) ; this is true only if the parameter fit in a register. ; Return registers are: RAX, RDX ; For more informations: http://www.x86-64.org/documentation format ELF64 section '.text' executable ; void write(char*) extrn "_Z5writePc" as write ; int sum(int a, int b) public sum sum: MOV EAX,EDI ADD EAX,ESI ;??? ADD EAX,[num] PUSH RAX MOV EDI,strg CALL write POP RAX RET section '.data' writeable extrn "num" as num ;public strg strg DB "everything!",0 Compile with: Code: fasm AsmPart.asm AsmPart.o CppPart.cpp Code: // Writed for board.flatassembler.net by Nikso #include <iostream> using namespace std; extern "C" int sum(int a, int b); //extern char* strg; int num = 10; void write(char* cosa) { cout << "Calculate within ASM " << cosa << endl; } int main() { // cout << strg << endl; cout << "2 + 3 = " << sum(2,3) << endl; return 0; } compile with: Code: gcc -o CppPart.o -c CppPart.cpp Now link with: Code: gcc -o cppandfasm AsmPart.o CppPart.o In the attached archive there are sources, makefile, binary and the GCC asm compilation of the cpp file. Hope this help! Bye
|
|||||||||||
08 Oct 2006, 10:52 |
|
vid 08 Oct 2006, 11:22
great, thanks.
how did you come to "_Z5writePc" decoration? i found going back from stack to registers stupid... :/ |
|||
08 Oct 2006, 11:22 |
|
mattst88 10 Oct 2006, 22:47
I know how to pass integers back and forth, but returning floating point numbers is getting the best of me.
The ABI says that xmm0 and xmm1 are used for returninf floating point numbers, but can someone give me an example? |
|||
10 Oct 2006, 22:47 |
|
mattst88 12 Oct 2006, 01:55
Is the high quadword or low quadword used for passing a double? Or is the double duplicated throughout both quadwords?
I've been doing some tests and the results I've been getting are completely unexplainable. Sometimes, it'll return the value, other times not. Sometimes in the high quadword, others in the low. I've even gotten it to return 0.0 if both quadwords are 0.0, and -0.0 if the low quadword is 0.0 and the high undefined. wtf is going on? Could someone be kind enough to give me an example? |
|||
12 Oct 2006, 01:55 |
|
Mark Larson 08 Nov 2006, 20:16
I haven't gotten floating point working either ( was trying with printf()). However I did notice one other thing.
you MAY need to set rax up according to the ABI. It is supposed to contain the number of floating point parameters you are passing to the function if the function takes a variable number of parameters. From the 0.98 version of the ABI: "with variable arguments passes information about the number of SSE registers used" _________________ BIOS programmers do it fastest! |
|||
08 Nov 2006, 20:16 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.