Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM SUB Instruction Operands

Tags:

assembly

arm

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.

like image 460
Zeke Avatar asked Dec 09 '22 14:12

Zeke


2 Answers

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.

like image 158
jcoder Avatar answered Dec 29 '22 14:12

jcoder


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

like image 31
Martin Avatar answered Dec 29 '22 14:12

Martin