WEB开发网
开发学院软件开发汇编语言 如何在C/C++使用内联汇编[英文版] 阅读

如何在C/C++使用内联汇编[英文版]

 2010-01-10 09:37:23 来源:WEB开发网   
核心提示:then qualify the reference to common_name with the tag name:asm mov [bx]first_type.common_name,10You need not qualify a reference to a unique member name. In th

then qualify the reference to common_name with the tag name:asm mov [bx]first_type.common_name,10You need not qualify a reference to a unique member name. In the following example, unique_name is an anonymous structure member because it is a member of first_type.asm mov [bx].unique_name,10This statement generates the same instruction whether or not a qualifying name or type is present. For more information, see the section "Making anonymous references to structure members" later in this chapter. Functions in inline assembly languageBecause ASM blocks do not require separate source file assembly steps, writing a function using ASM blocks is easier than using a separate assembler. In addition, the compiler generates function prolog and epilog code.

The expon2 function is an example of a function written in inline assembly language:

int expon2(int num, int power)
  {
    asm
    {  mov AX,num  // get first argument
    mov CX,power  // get second argument
    shl AX,CL  // AX = AX * (2 to the power of CL)
    }
}

An inline function refers to its arguments by name and may appear in the same source file as the callers of the function.

Refer to Using Assembly Language Functions for a description of the register stacks used by inline assembly instructions.

Making anonymous references to structure membersYou can make anonymous references to members of a given structure, as in the following:

struct x {
    int i;
    int j;
    int k;
  } foo;
You can refer to these members i, j, and k anonymously, for example, with the assembly instruction:asm
  {  mov BX,4
    mov AX,foo[BX]; Refers to member j of foo
  }

Using register variablesDigital Mars C++ supports register variables. Register variables are useful with inline assembly. If asm statements place results in registers, you can use register variables to access those values.

For more information see "Using Register Variables" in Chapter 5, "Using Assembly Language Functions."

Using the __LOCAL_SIZE symbolWhen using the inline assembler, the special symbol __LOCAL_SIZE expands to the number of bytes used by all local symbols. __LOCAL_SIZE is useful in combination with __declspec(naked), as __LOCAL_SIZE is the amount of space to reserve on the stack.

For example:

__declspec(naked) int test()
  {
    int x, y, z;
    _asm
    {  push BP
    mov BP,SP
    sub SP,__LOCAL_SIZE
    mov BX,__LOCAL_SIZE[BP]
    mov BX,__LOCAL_SIZE+2[BP]
    mov AX,__LOCAL_SIZE
    mov AX,__LOCAL_SIZE+2
    }
    _AX = x + y + z;
    _asm
    {
    mov SP,BP
    pop BP
    ret
    }
  }

上一页  1 2 3 4 5  下一页

Tags:如何 使用 内联

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接