Explanation of str in ARM assembly

2019-08-11 02:17发布

问题:

Strangely, I cannot find an explanation as to how str works in assembly language.

I understand that the following...

str r1,[r2]

...will store whatever is in register 1, r1, in the address of register 2, r2. However, I would like an explanation as to how str works when the square brackets are much more complicated. For example:

str r1,[r0,r3,lsl#2]

What is going on here? I want to know where the value of r1 will end up, and what will be its value. I can't test it, so I am going to take a guess as to what I think is going on.

r3 is logically shifted by 2. It is then added to r0. Finally, r1 is added to r0.

This is my guess, though even to me this does not sound correct. Can someone clarify this for me?

回答1:

str r1,[r0,r3,lsl#2]

What is going on here?

The instruction above basically says: r0 + (r3 << 2) = r1, or if we manually "expand" the bit-shift: r0 + (r3 * 4) = r1

The notation is <instruction> <src> [<dst>, <offset>, <shift>].

Note that offsets can also be negative (e.g. -r3, #lsl2).

I want to know where the value of r1 will end up, and what will be its value.

You need to know the actual values of r0 and r3 at a specific point in time during its execution before you can know any of this. This is like asking "What is the value of y in y = f(x) : x * 2?" without knowing anything about x.

r3 is logically shifted by 2. It is then added to r0.

Yes.

Finally, r1 is added to r0.

The datum in r1 is stored in (not added to) the location calculated by the left-hand side.



回答2:

r3 is logically shifted by 2. It is then added to r0. Finally, r1 is added to r0.

r1 isn't added to anything. It's stored at the address calculated by what you described in the first two sentences. I.e.:

[r0 + (r3 << 2)] = r1

See Load and Store Word or Unsigned Byte - Scaled register offset in the ARM Architecture Reference Manual for further information.

I want to know where the value of r1 will end up, and what will be its value.

If by the second part of that sentence you mean "I want to know what the value of r1 will be" then the answer is "That's impossible to say based on the given information". It will have whatever value that was last assigned to it.



标签: assembly arm