flat assembler
Message board for the users of flat assembler.

Index > Windows > linking c w/ asm

Author
Thread Post new topic Reply to topic
Paul6253



Joined: 19 Oct 2003
Posts: 31
Location: NY
Paul6253 07 Dec 2003, 18:59
perhaps I would like to call my asm routines from c, or exchange variables
between them
here's an example of what I mean:

Code:

asm module--
;need to make this function global so
;can be called from c

inc_var:
           push ebp
           mov esp,ebp
           mov eax,dword ptr [ebp+8]
           pop ebp
           inc eax
           ret
            



c module --

extern int inc_var(int);
main(){
int x = 41;
//return the meaning of life
x=inc_var(x);


}

how do I make inc_var visible in c? 
I read the fasm doc but didnt see anything about exporting local symbols

Thanks for info 
    

_________________
Plez xcuce mi spelng
Post 07 Dec 2003, 18:59
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 07 Dec 2003, 20:16
try this:
Code:
format COFF                ;  or MS COFF
public inc_var as "_inc_var"

inc_var: 
           push ebp 
           mov esp,ebp 
           mov eax,dword ptr [ebp+8] 
           pop ebp 
           inc eax 
           ret
    


it is mentioned in fasm manual:
Quote:
"public" directive declares the existing symbol as public, it should be followed by the name of symbol, optionally it can be followed by the "as" operator and the quoted string containing name under which symbol should be available as public.


regards
Post 07 Dec 2003, 20:16
View user's profile Send private message Visit poster's website Reply with quote
Paul6253



Joined: 19 Oct 2003
Posts: 31
Location: NY
Paul6253 07 Dec 2003, 22:34
Thanks for info...
still having probs with linker(ld) complaining of undefined symbol,
using dev C++ compiler...
i dunno, but too tired
cya later

_________________
Plez xcuce mi spelng
Post 07 Dec 2003, 22:34
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 08 Dec 2003, 06:14
in C++ code you must declare variable (inc_var, the one defined in asm part) in extern "c" {} block. It has what to do with way C++ compiler interprets names. C names were just "decorated" with "_" prefix (you see it in asm code, _inc_var), while C++ names do not have any standard, they differ on some compiler. extern "C" { } tells compiler to declare C names (_ prefix) in block
Post 08 Dec 2003, 06:14
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 23 Feb 2004, 12:49
This thread was helpful, but I would like to know how to reference a variable declared in C++ in the assembly code??
I think in assembler we can use:
extrn "_myvar" as myvar

But how should the variable be defined in C++ ??
Post 23 Feb 2004, 12:49
View user's profile Send private message Visit poster's website Yahoo Messenger Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind 23 Feb 2004, 12:50
Another thing is how can we determine whether the routine in Assembly has been called via a far call or a near call?
Post 23 Feb 2004, 12:50
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 23 Feb 2004, 12:54
proably it would be something like:
Code:
extern "C" {
  int my_var;
};
    


But I can't check it rightnow because I have recently reinstalled my system and I have to download and install mingw...

regards
Post 23 Feb 2004, 12:54
View user's profile Send private message Visit poster's website Reply with quote
milind



Joined: 15 Feb 2004
Posts: 33
milind 23 Feb 2004, 13:13
So whats the difference between a variable declared in Assembly and a variable declared in C++ with respect to its referencing in C++??
Post 23 Feb 2004, 13:13
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 23 Feb 2004, 13:22
- data defined in C and C++ goes automatically to the data section, while in assembly you can put it where you want Wink
- names in C are "decorated" in object file - that's why you have to use "_" preffix in the assembly source. C++ makes even more complicated decorations, that's why the easiest way is tu use "extern "C" {" when using them in asm.

regards
Post 23 Feb 2004, 13:22
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 23 Feb 2004, 16:39
If you use headers for use both with C and C++, a construct like the following saves the day Smile
Code:
#ifdef __cplusplus
extern "C" {
#endif

// prototypes and stuff goes here

#ifdef __cplusplus
}
#endif
    
Post 23 Feb 2004, 16:39
View user's profile Send private message Visit poster's website Reply with quote
Vortex



Joined: 17 Jun 2003
Posts: 318
Vortex 23 Feb 2004, 18:45
Paul6253,

About Dev C++ compiler, does it support MS COFF output?

For an example of using Fasm with VC++ , have a look at this thread

_________________
Code it... That's all...
Post 23 Feb 2004, 18:45
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 23 Feb 2004, 19:08
OK, the main problem with symbols decoration in C++ part is that there is no standard for this decoration. C decoration with "_" prefix is standard, but there is lack of such standard for C++ so every compiler uses another standard. If you want to know name for some compiler, just look at link errors saying some symbol is undefined. It usually shows already decorated name.
Post 23 Feb 2004, 19:08
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard 23 Feb 2004, 20:36
Vortex wrote:
About Dev C++ compiler, does it support MS COFF output?


It doesn't. Actually DevC++ is not a compiler, it's just an IDE for gcc compiler that produces "ordinary" COFF objects. When I was making simple tests with interfacing assembly and C, I realised that both formats (COFF and MS COFF) are at least partially compatible. When I compiled a small object (just a few bytes of code), the only two or three bytes were different. And I was able to link this simple MS COFF output using gcc linker, that normally works with "ordinary" COFF. I just wonder 'when' those formats would be incompatible...

regards
Post 23 Feb 2004, 20:36
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 23 Feb 2004, 20:41
Afaik the only incompatibilities between GNU-coff and ms-coff are with regards to relocations. One format stores 0 for imm32 and relies on the linker to fix up the symbol completely, the other stores module-relative in imm32 and depends on the linker to just add the relocation delta... something like this, anyway. Thus, it should be possible to make a tool to convert between the two.
Post 23 Feb 2004, 20:41
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.