I defined a function called multiply and it takes arguments R0 and R1 and saves the result in R3. This sucks because whenever I call multiply I have to put the operands in R0 and R1 and move what was in R0, R1 and R3 somewhere else. Is there a way to make the function take the form multiply R4, R5, R6 similar to how add R1, R2, R3
ldr R0, =snakes
ldr R1, [R0], #4
mov R2, #15
mov R3, #6
If I want to multiply R2 and R3 this would require extra work. I call multiply often and am wondering if there’s a better way?
Multiply:
stmfd sp!,{r0-r2, lr}
mov R2, #1
mov R3, #0
repeat:
add R3, R1, R3
add R2, R2, #1
cmp R2, R0
ble repeat
mov R2, R3
LDMFD sp!,{r0-r1, pc}
You can use macros:
Values of parameters in the body are avaliable by
\A, \B, \Cthere is a little mistake in your code:
When R0 is 0, the result would be R1 instead of 0. So you have to check it before it enters the loop. The correct code will be:
But this is not the most optimal method because there are 5 instructions in loop’s body instead of 4, and 2 branches instead of 1, so it will be difficult to predict them both. That’s why we going to do the following:
And finally, with macros it will look like this:
But I don’t know why you want to write your own multiply macro, I hope you know, that there is a set of multiply instructions…