flat assembler
Message board for the users of flat assembler.

Index > Main > Interfacing a routine in C

Author
Thread Post new topic Reply to topic
milind



Joined: 15 Feb 2004
Posts: 33
milind
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??
Post 16 Feb 2004, 13:47
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
pelaillo
Missing in inaction


Joined: 19 Jun 2003
Posts: 878
Location: Colombia
pelaillo
Do you need to link statically or dinamically? Win32 or Linux?
Statically, you need to use a linker (I suggest GoLink) and compile your assembly (coff format) exporting the function (see fasm examples for coff)
From C you need to produce a header including the reference of your function and parameters passed to it.

Dinamically, you create a dll with fasm and import your function from C code as if were a system library.
I am going to clean some examples to post here.
Post 16 Feb 2004, 15:08
View user's profile Send private message Yahoo Messenger Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind
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++.
Post 16 Feb 2004, 16:43
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
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...
Post 16 Feb 2004, 18:48
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
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
Post 16 Feb 2004, 19:46
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind
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.
Post 16 Feb 2004, 23:34
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
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
Post 16 Feb 2004, 23:57
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
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
Post 17 Feb 2004, 19:07
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind
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.
Post 18 Feb 2004, 21:21
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind
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?
Post 18 Feb 2004, 21:36
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
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
Post 18 Feb 2004, 22:07
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex
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...
Post 19 Feb 2004, 11:05
View user's profile Send private message Visit poster's website Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind
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.
Post 19 Feb 2004, 11:10
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.