I'm working on an iPhone/iPad project, and I want to update the status register during some (not all) arithmetic operations. By default, Xcode uses 'Compile for Thumb' and I don't want to change it.
The following GCC inline assembly code works fine under ARM, but results in a compile error under Thumb: 'instruction not supported in Thumb16 mode - adds r6,r4,r5'. The problem lies in the status register update. (I'm also aware that movcs
and strcs
will need to be changed).
Does Thumb have an ADD instruction which sets Overflow (V) or Carry (C) in the CPSR? If not, are there Thumb-specific assembly level workarounds to test for overflows and carries?
uint32_t result, a, b;
int no_carry = 1;
...
__asm__
(
"ldr r4, %[xa] ;" // R4 = a
"ldr r5, %[xb] ;" // R5 = b
"adds r6, r4, r5 ;" // R6 = R4 + R5, set status
"movcs r4, #0 ;" // set overflow (if carry set)
"strcs r4, %[xc] ;" // store it (if carry set)
"str r6, %[xr] ;" // result = R6
: [xr] "=m" (result), [xc] "=m" (no_carry)
: [xa] "m" (a), [xb] "m" (b)
: "r4", "r5", "r6"
);
...
EDIT: Registers also need to be moved around to take advantage of the ARM ABI at Application Binary Interface (ABI) for the ARM Architecture.