www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/06/01/22:30:16

From: Nate Eldredge <neldredge AT hmc DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Asm jmp and C label - link error: undefined reference...
Date: 01 Jun 2001 19:27:03 -0700
Organization: MindSpring Enterprises
Lines: 88
Sender: nate AT mercury DOT bitbucket
Message-ID: <834rtzd3bc.fsf@mercury.bitbucket>
References: <MPG DOT 158222a0d5002a7f989680 AT news DOT libero DOT it>
NNTP-Posting-Host: a5.f7.d5.43
Mime-Version: 1.0
X-Server-Date: 2 Jun 2001 02:27:03 GMT
User-Agent: Gnus/5.0802 (Gnus v5.8.2) Emacs/20.5
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Fabrizio Da Ros <fabrium AT libero DOT it> writes:

> Hi, I'm going crazy to solve a little problem, can 
> someone help me?
> This is the source code:
> 
> -------------
> #include <stdio.h>
> 
> short int a;
>
> void func1 ()
> {
>      __asm__ __volatile__ (
>                "btl $0, _a\n\t"

Bug: You're using the 32-bit form of bt, but a is only 16 bits.

>                "jc boardtest" );
>      goto notest;
> boardtest:
>      printf ("testing...\n");
> notest:
>      printf ("exiting...\n");
> }
> 
> int main ()
> {
>      a = 1;
>      func1 ();
> }
> ----------
> 
> OK, it's stupid, but this is only an example to explain 
> the problem. 
> I need to jump with "jc" after testing the bit. The 
> problem arises in the link stage:
> "undefined reference to boardtest" (in the line where is 
> the jc). No way to solve the problem. I tried to prefix 
> with on or two _, I tried to put a dummy goto in func1 to 
> boardtest, to tell gcc that i NEED that label. The 
> problem arises every time. I tried to define the label in 
> an asm statament, but is the same. The only way is to 
> write the code all in asm, BUT I CAN'T do this in my real 
> program.

The compiler doesn't use the same names for labels as the source did,
so you can't use a C label as your jump target.  Your label will have
to be inside the asm block.  Also, you can't jump arbitrarily into
spots in the C code and expect it to work, so your asm block will have
to return the result of its decision some other way.  (The setxx
instructions are useful here.)  If you do use a jump, it should be to
a local label (see the as manual for details); otherwise inlining by
the compiler may cause the label to appear repeatedly, which won't
work.

Also, for better portability, don't refer to "_a" in your asm block,
since not all systems use the leading underscore.  You can use gcc's
"Extended Asm" features for this; there are documents about it in the
gcc manual, and on the DJGPP web site.

I would probably write your code like this:

int a;

void func1 (void)
{
        char bit_was_set;
        asm("bt $0, %1; "
            "setc %0"
            : "=g" (bit_was_set) : "g" (a) : "cc");
        if (bit_was_set) ...
}

> Another way to solve the problem is to compile with -S 
> option to obtain the assembly source, and write in the 
> correct location the label "boardtest:", than compile and 
> link. But...It's not human!
> 
> I have downloaded and installed the entirely DJGPP 
> package about 2 mounth ago...
> 
> Thank you, Fabrizio.

-- 

Nate Eldredge
neldredge AT hmc DOT edu

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019