NOWS/MEMO

LDR vs. MOV

emzei 2014. 4. 27. 23:04


11 posts
by Noah Caldwell » Wed Sep 05, 2012 4:08 pm
http://www.cl.cam.ac.uk/freshers/raspbe ... /ok01.html

I've been reading the Baking Pi course (see link above), and I've come to the 4th 'part': Enabling Output. I am confused as to the difference between 'ldr' and 'mov'. Even the definitions given are identical:

mov reg,#val puts the number val into the register named reg.
ldr reg,=val puts the number val into the register named reg.

Besides the slightly different syntax, what is the difference? (And yes, I RTFM (or at least his explanation) several times. I didn't get it.)
Posts: 7
Joined: Wed Jan 11, 2012 8:59 pm
by DexOS » Wed Sep 05, 2012 4:16 pm
6. Question on differences between LDR and MOV

What is the difference between MOV and LDR, they seem to have the same function of
saving information into registers?
Answer part 1: How different are they?
Note: “#” for mov, “=” for ldr. To define an immediate value
o MOV can only move an 8-bit value (0x00->0xff=255) into a register
while LDR can move a 32-bit value into a register. The immediate value is
prefixed by different characters in mov and ldr: “#” for mov, “=” for ldr.
E.g.
Mov r1,#255 ; ok, 255 is the biggest number you can mov
Mov r1,255 ; is wrong , missing #
Mov r1,#256 ; is wrong, the number is bigger than 255
Mov r1,#0x12340000 ; is wrong, the number is bigger than 255

Ldr r1,=255; you can do this,
Ldr r1,=256; you can do this,
Ldr r1,=0x12340000; you can do this,
o MOV can run faster than LDR.
o LDR can move a data from a memory address to a register, MOV can only
i) move data between two registers or ii) save a 8-bit immediate value to a
register. e.g.

value1 DCD 0; this define an integer variable “value1” with address “=value1”
:
;A standard pair of statements for moving a data into a register
Ldr r0,=value1 ; 1) save the address of the variable value1 in r0
Ldr r1,[r0]
;2)use r0 as the address (pointer) to get value1 to r1

Note: Mov cannot be used to achieve the same result, because mov r1,[r0] is not allowed
Answer part 2 : How similar are they?.
MOV is a real instruction (a 32-bit instruction) , LDR is a pseudo instruction (the
assembler will use multiple 32-bit instructions to achieve the goal). For data move, if
the immediate value is small, the assembler will use “mov” to implement it, so mov and
ldr are exactly the same if the immediate value less or equal to 255. For example, for
ldr r0,=14; the immediate value is 14 and is <255, so it will be implemented using mov
r0,#14 (see the use of # and = ).

However, for a large immediate value (>255) “mov” does NOT work, e.g. ldr
r0,=0x55555555; it is not ok to use mov r0,#0x55555555 because it is not allowed. Then,
the assembler will generate some code to place the constant 0x55555555 in a nearby
table in the code area. Then it uses an instruction to load a data from that table pointed by
the program counter and an offset to fill up r0. The reason is because there is no way to
fit a 32-bit data into a 32-instruction (an instruction must have the instruction-code-part
and the data-part, if the data-part is 32-bit long, there is no room to store the instruction-
code-part). Details can be found at http://www.keil.com/support/man/docs/armasm/
armasm_chdcegci.htm

4

see also the directive “LTORG” for how to setup the table. Usually it is placed at the near
by code area. You don’t need to worry too much because everything is automatic; you
only need to place “LTORG” at the end of the code area as shown in the example at
Keil.

https://docs.google.com/viewer?a=v&q=ca ... 5ajfQm39GQ


'NOWS > MEMO' 카테고리의 다른 글

cyclictest _ numa.h : no such file or directory  (0) 2014.05.21
리눅스 파티션 매뉴얼  (0) 2014.05.01
terminologies 140406  (0) 2014.04.06
coupon collector's problem  (0) 2014.03.11
Tamper resistance (탬퍼링 방지 기술)  (0) 2014.02.20