What is the difference between these instructions in the ARM Cortex M0?
LDR r1, r2
LDR r1, [r2]
MOV r1, r2
Is any of them wrong?
If none of them is wrong, why would I use the second one to load from memory when I can type it in the first expression?
And if the first one means load from register to register, what about the third?
Get confused with the second instruction, as if I use a register I mean the value on it.
Note: I had no simulator.
The first form you give is wrong. All forms of
LDRandSTR(except for the label form ofLDR) use square brackets around the expression used for calculation of the target address.This is consistent across all the forms, which makes reading them easier once you're familiar with them. For example,
adds 4 to the contents of
r2and then loads intor0from the calculated address. On the other handuses the unmodified contents of
r2for the load address, and then adds 4 tor2("post-increment"). For completeness the final constant offset form iswhich does the same as the first example, but then also writes the calculated address back into
r2after the load ("pre-increment"). The exclamation mark is the "writeback character" and appears optionally in some other instructions too, likeLDMandSTM.Finally, for register-to-register moves,
MOVis the instruction you want.The documentation on these instructions is pretty good (here for ARMv6M devices like the Cortex-M0). There's a quick reference card here for all Thumb and Thumb-2 instruction sets, which is very useful but only if you can read it, which probably requires a bit of knowledge about the instruction set first.