This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more infromation.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Invalid Operands with Inline assembly


Stan,

Why would not the following work just as well...

extern long TxStr;		  /* defined as .globl TxStr in external asm file */

int main()
{
   char pHelloStr[6] = "Hello";
   
   while(1)
   {

      asm( 
          " mov  %0, r4;"          /* Move global asm label TxStr to r4
*/
          " mov  %1, r0;"          /* And string to r0 */
          " jsr  @r4;"             /* And jump via r4 */
          :                        /* no outputs */
          : "r" (TxStr),           /* %0 is Input 0, a register, use
TxStr */
            "r" (pHelloStr)        /* %1 is Input 1, a register, use
pHelloStr */
          : "r0", "r4"             /* and uses r0 and r4 as scratch */
          );
   }
   
   return(0);
}

int __main() {}                     /* I was told I need this for gcc,
don't know why */


I do not see why the pointers are needed since TxStr already is defined
in the asm file as .globl TxStr. The linker should apply the right
address here.  And the string pHelloStr is basically the address for the
string "Hello" above so could be directly loaded into the register.  It
does compile and link fine.  I tried it but it did not work since I
think I am branching to the wrong 'main'.  I looked at the assembly
listing and ___main is defined as the global instead of the 'main' at
the top of my program. So I guess I need to branch to ___main.

I would like to know why you added in the pointers in your example. 
Thanks.

Robert F.




Stan Katz wrote:
> 
> Robert Floyd [mailto:robert.floyd@inet.com] Wrote:
> >>Snip
> > I have a particular assembly source file that
> > contains everything I need to use the serial port on the
> > Hitachi part.
> > For example, it contains one piece of code that allows one to send a
> > string out the serial port.  So in another assembly file if I want to
> > send out "Hello World", I do the following:
> >
> > DoHelloWorld:
> >       mov.l   pTxString,r4            ! address loaded for
> > global TxStr function
> >       mova    pHelloStr,r0            ! get 'Hello World'
> > string and put in r0
> >       jsr     @r4                     ! jump to address in r4
> > (global TxStr)
> >
> > ---and at the bottom of my file I have----
> >
> > pTxString:    .long   TxStr               ! TxStr is a global
> > function in another asm
> > file
> > pHelloStr:    .asciz  "\n\rHello World "   ! my ascii string
> >
> >
> > Since I really want to write my code in C but I also don't want to
> > recreate the wheel, since there is alot of assembly language
> > functions I
> > could use, how could I write this DoHelloWorld function in C.
> >> Snip
> 
> This is a case where the parameters for the asm function can help, I would
> try something like the following (based on the code that you sent and I
> snipped)
> 
> extern long TxChar;
> 
> int main()
> {
>    char pHelloStr[6] = "Hello";
> 
>    while(1)
>    {
>    register long *pointer;
>    register long *string;
> 
>       asm(
>           " mov  %0, r4;"         /* Move function pointer to r4 */
>           " mov  %1, r0;"          /* And string pointer to r0 */
>           " jsr  @r4;"             /* And jump via r4 */
>           :                        /* no outputs */
>           : "r" (pointer),         /* %0 is Input 0, a register, use C
> variable pointer */
>             "r" (string),          /* %1 is Input 1, a register, use C
> variable string */
>           : "r0", "r4"             /* and uses r0 and r4 as scratch */
>           );
>    }
> 
>    return(0);
> }
> 
> This lets the C compiler handle the assignment of the addresses to the
> registers which are passed as parameters to the asm function. Since the
> compiler seems to concatenate all the asm strings into one long string, I
> use a single asm statement and let it concatenate the strings before
> processing the asm.
> The last 3 lines of the asm routine define the Output parameters, Input
> Parameters and Registers affected by the asm routine. For more details on
> this look at the "Using GNU GCC" manual, Chapter 3 - "Extensions to the C
> language Family", section 3.30.
> 
> I have not tried to get very fancy with the parameter specifications, and
> usually just use register variables that are assigned in a surrounding
> block.
> 
> BTW, you could probably eliminate the " mov %0,r4;" and simply do " jsr
> @%0;".
> 
> Stan

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]