2011/07/03

To write assembly code in c code

http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html

I need to write some assembly code in c code for embedded system. It's simple
if only pure assembly code is needed. Just write op code with registers in a .s file. Unfortunately, it has to link variables in c code to assembly code, so I need more advanced knowledge.



pure assembly for MIPS:

asm volatile("lw t0, (t1)");

But how do you know the address in t1 of a variable?

In c code there's a template (sec. 5):


asm ( assembler template 
           : output operands                  /* optional */
           : input operands                   /* optional */
           : list of clobbered registers      /* optional */
           ); 

The document is very detailed. If there is no operands, just place two consecutive columns.


        int a=10, b;
        asm ("movl %1, %%eax; 
              movl %%eax, %0;"
             :"=r"(b)        /* output */
             :"r"(a)         /* input */
             :"%eax"         /* clobbered register */
             );       

"r" means that compiler selects arbitrary registers to save the variable value and keep it in the register. "=" means it is output value. %0 means 0th parameter(b here) and %1 means 1st parameter(a hear).

asm volatile("lw t0, (t1)"); 
-> 
asm volitile("lw t0, (%0)"::"r" (VAR));


asm volitile("lw %0, (ADDR)":"=r"(VAR));

沒有留言:

張貼留言