flat assembler
Message board for the users of flat assembler.
Index
> Main > Interfacing a routine in C |
Author |
|
milind 16 Feb 2004, 13:47
Hello,
I needed to know how can I write a routine in assembly and call it from my C program and vice versa? Is there any tutorial on the web for this?? |
|||
16 Feb 2004, 13:47 |
|
milind 16 Feb 2004, 16:43
Thanks for ur reply, but I don't get a lot of things. Are there any examples I can get to see the interface. I am talking about Win32 examples and it will be good if I can see for both Static and Dynamic linking. As well as a link both ways, assembly to C and C to Assembly. I mainly use Visual C++.
|
|||
16 Feb 2004, 16:43 |
|
Vortex 16 Feb 2004, 18:48
milind,
Here is an example of static linking, you can check the latest version of my inc2inc tool. You can use Hutch's masm32.lib with Fasm: http://board.flatassembler.net/topic.php?t=588&postdays=0&postorder=asc&start=0 Also, using C run-time library with Fasm: http://board.win32asmcommunity.net/showthread.php?s=&threadid=9510 _________________ Code it... That's all... |
|||
16 Feb 2004, 18:48 |
|
vid 16 Feb 2004, 19:46
just use format COFF, and define extern for all symbol you want to export. Then link created COFF file as .OBJ to your C/C++ project. Just dont forget about extern "C" and _ before names
|
|||
16 Feb 2004, 19:46 |
|
milind 16 Feb 2004, 23:34
How do I define extern and global symbols. I checked with FASM Programmers referece document and it did not describe any directive to be extern or global.
|
|||
16 Feb 2004, 23:34 |
|
decard 16 Feb 2004, 23:57
Those directives are described in section 2.4. (Formatter directives) of FASM Manual. You can declare public and external symbols for example this way:
Code: extrn "_my_symbol" as my_symbol:dword public other_symbol as "_other_symbol" other_symbol: call my_symbol ret remember that C expects all function names in object file to be preceeded with "_" character. regards |
|||
16 Feb 2004, 23:57 |
|
vid 17 Feb 2004, 19:07
and remember we are talking about C.
If you are using C++, you also have to declare symbols which are defined in fasm code in extern "C" {} block |
|||
17 Feb 2004, 19:07 |
|
milind 18 Feb 2004, 21:21
Hello Vid,
Well I was manage to do something thanks to you. But I don't understand what is the meaning of putting "C" after extern in the C program. |
|||
18 Feb 2004, 21:21 |
|
milind 18 Feb 2004, 21:36
Can't seem to make this work:
VC++ code: #include<iostream.h> extern "C" void disp(int, int); extern "C" int sum; void main() { disp(3, 4); cout<<sum; } FASM code: format COFF public disp as "_disp" extrn "_sum" as sum:word disp: push bp push ax push bx mov bp, sp mov ax, [bp+4] mov bx, [bp+6] add ax, bx mov [sum],ax pop bx pop ax pop bp ret Error in VC ++ Build is: Compiling... c2assemdispa.cpp Linking... c2assemdispa.obj : error LNK2001: unresolved external symbol _sum dispa.obj : error LNK2001: unresolved external symbol _sum Debug/Cmixassembly.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. Cmixassembly.exe - 3 error(s), 0 warning(s) What's wrong? |
|||
18 Feb 2004, 21:36 |
|
decard 18 Feb 2004, 22:07
That's because you declared "sum" as an external symbol in both C and assembly modules. You have to define this variable in one of them. For example in FASM source:
Code: format COFF public disp as "_disp" public sum as "_sum" sum dw ? disp: (...) And when suing MSVC, you should rather use "format MS COFF" than "format COFF"; I'm not sure about that, but those formats are a bit different so this could cause errors in more complec projects. regards |
|||
18 Feb 2004, 22:07 |
|
Vortex 19 Feb 2004, 11:05
milind,
Something very important, you can't mix 32-bit VC++ code with 16-bit code: Code: push bp push ax push bx mov bp, sp mov ax, [bp+4] mov bx, [bp+6] add ax, bx mov [sum],ax pop bx pop ax pop bp ret In 32-bit mode, the stack is dword aligned; statements like mox ax,[bp+4] are meaningless. _________________ Code it... That's all... |
|||
19 Feb 2004, 11:05 |
|
milind 19 Feb 2004, 11:10
Yes, I found that our, it was my mistake to use the 16 bit code. Now the code is working fine but I want to know how can I use variables declared in C. Right now sum was declared in assembly. How should I declare sum in C and then use it in Assembly.
|
|||
19 Feb 2004, 11:10 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.