Arm cortex m0 LDR instruction

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.

  • 1

    what part of the arm documentation do you not understand? you dont need a simulator you can disassemble and compare with documentation.

    – 

  • 1

    godbolt.org/z/EE994ae16 shows that the last two assemble, but unfortunately Godbolt’s language=assembly support for ARM is not very good; the menu option for showing the machine code as part of disassembly is grayed out. You can still see that the first form errors, though. (I think it’s trying to treat r2 as a symbol name, since it can’t be a register in that context: outside [].) In general for playing around with an instruction set, get yourself a compiler and assembler so you can at least see if it’s valid. Probably there’s a better online ARM assembler somewhere.

    – 




The first form you give is wrong. All forms of LDR and STR (except for the label form of LDR) 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,

LDR r0, [r2, #4]

adds 4 to the contents of r2 and then loads into r0 from the calculated address. On the other hand

LDR r0, [r2], #4

uses the unmodified contents of r2 for the load address, and then adds 4 to r2 (“post-increment”). For completeness the final constant offset form is

LDR r0, [r2, #4]!

which does the same as the first example, but then also writes the calculated address back into r2 after the load (“pre-increment”). The exclamation mark is the “writeback character” and appears optionally in some other instructions too, like LDM and STM.

Finally, for register-to-register moves, MOV is 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.

Leave a Comment