4

I would like to write functions in assembly along with the c source code, but somehow I can't find the syntax. I tried e.g. in the fname.asm file (simplified):

fill_bmap:
        rts

In the fname.c source:

extern void fill_bmap();

But the compiler gives this error message:

undefined reference to `fill_bmap'

5
  • 2
    So this is a “how do I use some modern software” question?
    – Tommy
    Commented Mar 30 at 15:11
  • 2
    Why is this somehow over the line when cc65, which is just as current, is not? (Though admittedly I would prefer the actual compiler to be named in the question, not some IDE plugin.) Commented Apr 5 at 22:48
  • @user3840170 Haven't voted, but would imagine that it being a topic the basic documentation for the compiler used would cover, so rather a case of missing any research.
    – Raffzahn
    Commented Apr 6 at 9:42
  • Sorry for noob question . The compiler: gcc 13.2. Unfortunately, I really asked the wrong question..I already tried what tofro answered before (I should have written it in), but it didn't work because that wasn't the problem. Meanwhile, I realized. I'm not a professional in modern IDEs, I programmed small routines in assembly about 25 years ago on the good old DEVPac. Thanks for the answers, I'll be more attentive next time! Commented Apr 7 at 16:45
  • A target triple would be nice too: gcc outputs that given the -v option. And the build system: you mention makefiles, but did not say anything if you write them directly or had them generated from something else. Elementary questions are fine in my book, as long as they are well-written. (That beginners do not always write them well is a different matter…) Commented Apr 10 at 11:34

1 Answer 1

9

There are two caveats involved when mixing C and assembly:

  1. You must tell the assembler that a symbol is meant to be externally visible (so that the linker can see it). With vasm, there are several ways to do that depending on the language mode you are using. To make <symbol> known externally, you might have to use one of the following directives (I have ordered them in falling order of probability):

    XDEF    symbol
    .global symbol
    extern  symbol
    public  symbol
    
  2. Some (many) C compilers for historical and other reasons tend to prepend an underscore to external symbols, so, when you're calling a function named

        extfunc ()
    

    from your C code, the linker will actually look for an assembly symbol named

        _extfunc 
    

    So you might have to rename your assembly function depending on the C compiler you are using.

2
  • Thanks for the info, it was helpful. In the meantime, I also realized that the Makefile also needs to be entered source file, e.g.: s_sources := bitmap_fill.s Commented Mar 25 at 7:47
  • I was actually assuming you had already done that. Maybe assumed too much. What's not in the Makefile is not going to be translated...
    – tofro
    Commented Mar 25 at 8:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .