Invalid Operands with Inline assembly

Stan Katz stan@ctc-control.com
Thu Oct 26 12:12:00 GMT 2000


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



More information about the crossgcc mailing list