I am trying to translate this C++ code:
y = y+ x*32;
z = y+ x*x;
To ARM assembly assume (x is R1 register, y is R2, z in R3), and I should use only one assembly instruction for each case So, I suggest to do it using (MLA) but I don't know how, can you please help me!!
Firstly, put your code snippet in an function and create a complete code.
void x(void){
volatile int x = 1, y = 2, z = 3;
y = y+ x*32;
z = y+ x*x;
}
Then, compile that on Compiler Explorer.
Result is:
x:
mov r1, #1
mov r2, #2
mov r3, #3
sub sp, sp, #16
str r1, [sp, #4]
str r2, [sp, #8]
str r3, [sp, #12]
ldr r2, [sp, #4]
ldr r3, [sp, #8]
add r3, r3, r2, lsl #5
str r3, [sp, #8]
ldr r0, [sp, #4]
ldr r1, [sp, #4]
ldr r2, [sp, #8]
mla r3, r1, r0, r2
str r3, [sp, #12]
add sp, sp, #16
bx lr
After that, get the assignment of variables from the values. It looks like [sp, #4]
is x
, [sp, #8]
is y
, and [sp, #12]
is z
.
Finally, using this relation, construct the result.
The answer is:
add r2, r2, r1, lsl #5
mla r3, r1, r1, r2