【C语言常识】mdk:Inline assembler rules for compiler keywords __asm and asm

出处:官方编译器手册

The following rules apply to the __asm and asm keywords:

  • Multiple instructions on the same line must be separated with a semicolon (;).

  • If an instruction requires more than one line, line continuation must be specified with the backslash character (\).

  • For the multiple line format, C and C++ comments are permitted anywhere in the inline assembly language block. However, comments cannot be embedded in a line that contains multiple instructions.

  • The comma (,) is used as a separator in assembly language, so C expressions with the comma operator must be enclosed in parentheses to distinguish them:

    __asm
    {
        ADD x, y, (f(), z)
    }
    
  • Labels must be followed by a colon, :, like C and C++ labels.

  • An asm statement must be inside a C++ function. An asm statement can be used anywhere a C++ statement is expected.

  • Register names in the inline assembler are treated as C or C++ variables. They do not necessarily relate to the physical register of the same name. If the register is not declared as a C or C++ variable, the compiler generates a warning.

  • Registers must not be saved and restored in inline assembler. The compiler does this for you. Also, the inline assembler does not provide direct access to the physical registers. However, indirect access is provided through variables that act as virtual registers.

    If registers other than CPSR and SPSR are read without being written to, an error message is issued. For example:

    int f(int x)
    {
        __asm
        {
            STMFD sp!, {r0}    // save r0 - illegal: read before write
            ADD r0, x, 1
            EOR x, r0, x
            LDMFD sp!, {r0}    // restore r0 - not needed.
        }
        return x;
    }
    

    The function must be written as:

    int f(int x)
    {
        int r0;
        __asm
        {
            ADD r0, x, 1
            EOR x, r0, x
        }
        return x;
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值