我要评分
获取效率
正确性
完整性
易理解

Common Syntax Errors

Immediate Constraint Error

In the following example, the input variable (12345) is an immediate, which should use the i or other immediate constraint. If it uses the r constraint, the GCC processes it as a 32-bit number by default and cannot assign the value to the 64-bit register corresponding to the output variable (output).

1
2
3
4
5
6
7
8
9
void imm_value_constraint_error()
{
    unsigned long long output = 0;
    __asm__(
        "mov %1, %0\n\t"
        :"=r"(output)
        :"r"(12345)
    );
}

Assembly Instruction Error

In the following example, the mov instruction is incorrectly used. The mov instruction does not apply to the scenario where memory operands are converted to memory operands. The input variable (input) and output variable (output) cannot use the memory constraint m at the same time.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
void instruction_op_use_error()
{
    unsigned long long input = 0x11223344;
    unsigned long long output = 0;
    __asm__(
        "movq %1, %0\n\t"
        :"=m"(output)
        :"m"(input)
    );
}