tags:

views:

44

answers:

2

I'm working with the llvm-clang compiler, compiling simple C functions down to assembly on an ARMv7-A processor. I am trying to figure out what this instruction does.

SUB sp, sp, #65, 30

Obviously it's making room on the stack pointer for some local variables, but I've never seen an ARM SUB instruction with four operands. I'm guessing that the 30 modifies the #65 somehow, but I don't know how, and I haven't been able to find details in the ARM Architecture Reference Manual. Any suggestions?

For the curious, this is at the beginning of a program that creates an 8 x 8 identity matrix of integers, so I would expect the sp to need to make room for at least 8 x 8 x 4 bytes on the stack.

+4  A: 

The 30 is a rotate right operation on the 65

Rotating right 30 bits is the same as rotating left 2 bitswhich is the same as a multiply by 4. 65 * 4 = 260

So this subtracts 260 from the stack pointer.

John Burton
Thanks for the quick reply. Any idea why there would be a shifted immediate rather than just the number 260 (the ARM instruction specifies the immediate to be 12 bits wide, clearly enough for the constant 260)?
Zeke
@Zeke: Martin's reply spells out why
Stephen Canon
+3  A: 

The arm design allocates 12 bits for immediate values, 8 bits for the value and the remaing 4 bits are a rotate right (represeting rotates of 0 2 4 etc. places). Since 260 cant be represented in 8 bits its constructed as 65*4.

This spreads out the immediate values available to the programmer over the whole 32bit range rather than restrict it to 0 to 4095

Martin