这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » AQ430,Inline,Assembly,Code AQ430 —— Inli

共2条 1/1 1 跳转至

AQ430,Inline,Assembly,Code AQ430 —— Inline Assembly Code

院士
2006-09-17 18:14:16     打赏
AQ430,Inline,Assembly,Code AQ430 —— Inline Assembly Code



关键词: AQ430     Inline     Assembly    

院士
2006-12-22 22:43:00     打赏
2楼
问 The AQ430 C compiler makes it very easy to drop into assembly code inside a C function.

To put assembly code inside your C function, simply write /$ and then start writing assembly code. At the end of the assembly code, write $/ to revert back to C code. As you might expect, you CAN put as many lines of assembly code as you wish between the /$ and the $/.

When writing inline code, you CAN access any of the C variables currently in scope by entering

name

where name is the name of the variable. The string name will be replaced by a value depending on its storage class as follows:

Storage Class Value

extern _name

global _name

local static file_Vn (a generated name)

auto a positive offset from the stack pointer

argument frame size + argument offset

register the register name

If the variable is declared using the register keyword, and if the type of the variable is large enough to require it to be placed in two or more registers, then you CAN optionally select which register to which you wish to refer by suffixing the name with a dot following by a single digit. For instance, suppose you declare a long variable called x. Since a long is 32 bits, x will be in two registers. Suppose the compiler puts x into R12 and R13. Then, in inline code, x or x.0 will be replaced by R12 (which will contain the most signifiCANt bits), while x.1 will be replaced by R13 (which will contain the least signifiCANt bits).

When referring to variables in in-line assembly code, you will need to add a bit of extra syntax to make the result be correct assembler syntax. If the storage class is register, then you just use variable. If the storage class is auto, then you must write variable(sp). If the storage class is global or static, then you must write &variable. For instance,

int x;

void test( register int y )

{

int z;

/$

mov 0,&x

mov 0,y

mov 0,z(sp)

$/

}

If you need to put an actual in your code (to use register indirect source addressing), you must precede the character with a \ (backslash) character to prevent the compiler from thinking you want to refer to a variable. Here is an example:

void test( register int *p, register int *q )

{

/$

mov \p,0(q)

$/

}

You CAN also get at structure offsets by using the construct

tag.member_name[.member_name]*

where tag is the "structure tag" as defined in C and member_name is the name of one of the members of that structure. While the member name has struct or union type, you CAN append an additional member name. The whole thing is replaced by a constant which is the offset from the start of the original structure to the specified member.

For instance, if you have declared the structure

struct mystruct {

int a, b;

struct {

int x;

int y;

} c;

}

then you CAN obtain the constant offset to member y in in-line assembly code by writing

mystruct.c.y

You CAN also obtain the size of a C object in in-line assembly code by writing

sizeof( expression )

where expression is any C expression which is a legal argument to the C sizeof built-in function.

In order to refer to a variable in in-line code, you and the compiler must agree on where the variable is located. For variables declared outside of functions, this is not a problem, because the variable is always in memory unless you declared it as a global register variable, in which case it is in a register. For local variables, the compiler takes special actions to make sure that it puts the variable where you expect to find it. If you did not declare the variable with a register keyword, then the compiler will always make sure to put the variable in memory.

If you did use the register keyword, then the compiler will bind the variable to a ``hard'' register or pair of registers, if the required number of registers is available. Otherwise, it will issue a fatal error message telling you that it could not bind the variable to a register. Because the compiler uses hard registers, each register variable declared in a function and referred to in in-line code reduces the number of registers available. If you declare too many register variables, you may leave too few registers, with the result that the register allocator will generate an error message complaining that it does not have enough "colorable" registers.

When the compiler sees inline assembly code, it also makes worst case assumptions about the state of common sub-expressions (by marking all current common sub-expressions as if they were invalidated by the inline code) and of the state of the registers (by assuming all non-colorable registers are modified by the in-line code).

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]