This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

using assembly and intrinsics in C code (Arm Compiler 6)

dear arm,

Is there a more detailed document that introduces how to use assembly and intrinsics in C code?

I found a breif introduction in migration_and_compatibility_guide_v6.18. But after reading it, I still don't know how to modify the format to meet Arm Compiler 6' requirements .

The code blow shows the example in migration_and_compatibility_guide_v6.18

// __asm in Arm Compiler 5
int add(int i, int j)
{
    int res;
    __asm
    {
        ADD res, i, j
        SUB res, i, res
    }
    return res;
}

// __asm in Arm Compiler 6
int add(int i, int j)
{
    int res = 0;
    __asm
    (
        "ADD %[result], %[input_i], %[input_j] \t\n"
        "SUB %[result], %[input_i], %[result] \t\n"
        : [result] "=&r" (res)
        : [input_i] "r" (i), [input_j] "r" (j)
        );
    return res;
}

But how should I modify the code below to meet Arm Compiler 6's requirements?

MOV sum, var1, LSL #16
QADD sum, sum, temp
MOV result, sum, ASR #16
MOV zero, #0
RSB var2_inv, var2, #0

many thanks!

Parents
  • Hello, I am unsure exactly what the above code does, it is seemingly part of a larger sequence, but I was able to compile the below:

    int example(int var1, int var2)
    {
    	int result, sum, temp=0, zero, var2_inv;
    	__asm
    	(
    	"MOV	%[sum],		%[input1],	LSL #16		\t\n"
    	"QADD	%[sum],		%[sum],		%[temp]		\t\n"
    	"MOV	%[result],	%[sum],		ASR #16		\t\n"
    	"MOV	%[zero],	#0						\t\n"
    	"RSB	%[var2_inv],%[input2],	#0			\t\n"
    	: [result] "=r" (result), [sum] "=r" (sum), [temp] "=r" (temp), [zero] "=r" (zero), [var2_inv] "=r" (var2_inv)
    	: [input1] "r" (var1), [input2] "r" (var2)
    	);
    	return result;
    }
    

    as:

        example
            0x00000000:    ea4f4100    O..A    LSL      r1,r0,#16
            0x00000004:    fa82f181    ....    QADD     r1,r1,r2
            0x00000008:    ea4f4021    O.!@    ASR      r0,r1,#16
            0x0000000c:    f04f0300    O...    MOV      r3,#0
            0x00000010:    f1c10c00    ....    RSB      r12,r1,#0
            0x00000014:    4770        pG      BX       lr
    

    Command line used:

    armclang -c --target=arm-arm-none-eabi -mcpu=cortex-m7 -O2 example.c
    fromelf -c example.o > example.txt
    

    The below documentation is more thorough than the Migration Guide:
    https://developer.arm.com/documentation/100748/0618/Using-Assembly-and-Intrinsics-in-C-or-C---Code/Writing-inline-assembly-code

Reply
  • Hello, I am unsure exactly what the above code does, it is seemingly part of a larger sequence, but I was able to compile the below:

    int example(int var1, int var2)
    {
    	int result, sum, temp=0, zero, var2_inv;
    	__asm
    	(
    	"MOV	%[sum],		%[input1],	LSL #16		\t\n"
    	"QADD	%[sum],		%[sum],		%[temp]		\t\n"
    	"MOV	%[result],	%[sum],		ASR #16		\t\n"
    	"MOV	%[zero],	#0						\t\n"
    	"RSB	%[var2_inv],%[input2],	#0			\t\n"
    	: [result] "=r" (result), [sum] "=r" (sum), [temp] "=r" (temp), [zero] "=r" (zero), [var2_inv] "=r" (var2_inv)
    	: [input1] "r" (var1), [input2] "r" (var2)
    	);
    	return result;
    }
    

    as:

        example
            0x00000000:    ea4f4100    O..A    LSL      r1,r0,#16
            0x00000004:    fa82f181    ....    QADD     r1,r1,r2
            0x00000008:    ea4f4021    O.!@    ASR      r0,r1,#16
            0x0000000c:    f04f0300    O...    MOV      r3,#0
            0x00000010:    f1c10c00    ....    RSB      r12,r1,#0
            0x00000014:    4770        pG      BX       lr
    

    Command line used:

    armclang -c --target=arm-arm-none-eabi -mcpu=cortex-m7 -O2 example.c
    fromelf -c example.o > example.txt
    

    The below documentation is more thorough than the Migration Guide:
    https://developer.arm.com/documentation/100748/0618/Using-Assembly-and-Intrinsics-in-C-or-C---Code/Writing-inline-assembly-code

Children