Search code examples
assemblyarmcortex-m

How to understand this embedded assembly code?


#define SVC_ARG2(call, arg1, arg2)  ({ \
uint32_t ret = 0;   \
asm volatile ("mov  r0, %[ar1]  \n"  \
              "mov  r1, %[ar2]  \n"  \
              "svc  %[code]  \n"    \
              "mov  %[ret], r0  \n" \
              :[ret] "+r" (ret)     \
              :[code] "I" (call), [ar1] "r" (arg1), [ar2] "r" (arg2)     \
              :"r0", "r1");               \
ret;    \
})

I can not understand

              "mov  %[ret], r0  \n" \
              :[ret] "+r" (ret)     \
              :[code] "I" (call), [ar1] "r" (arg1), [ar2] "r" (arg2)     \
              :"r0", "r1");         

this part of the code. Especially : [ret] "+r" (ret) \


Solution

  • This is gcc inline assembler syntax. The last 3 lines list outputs, inputs and clobbers, respectively. For output and input, the part in the square brackets is a symbolic name you can use to refer to the particular argument inside the asm block. The part in quotes is the constraint that tells the compiler what kind of argument it is: r means register, I means immediate. A + modifier normally tells the compiler that the argument is read-write, so I don't see much point in specifying that for the return value. Finally the part in the round parentheses is the value of the argument. Thus [ret] "+r" (ret) defines an output argument named [ret] which will be in a register and should be assigned to the variable ret.