15

I'm having trouble deciphering this block of assembly code. What would the value of r1 be by the end and how would I get there?

3242ba66    f6454118    movw    r1, 0x5c18
3242ba6a        466f    mov     r7, sp
3242ba6c    f6c0415a    movt    r1, 0xc5a
3242ba70    f2460002    movw    r0, 0x6002
3242ba74    f6c0405a    movt    r0, 0xc5a
3242ba78        4479    add     r1, pc
3242ba7a        4478    add     r0, pc
3242ba7c        6809    ldr     r1, [r1, #0]
user1000039
  • 785
  • 1
  • 7
  • 19

1 Answers1

40

movw followed by a movt is a common way to load a 32-bit value into a register. It's the equivalent of OR-ing those two immediate values together, with the movt being the upper 16-bit. In this case, r1 = (movt immediate value << 16) | (movw immediate value)).

3242ba66    f6454118    movw    r1, 0x5c18   // r1 = 0x5c18
3242ba6a        466f    mov     r7, sp
3242ba6c    f6c0415a    movt    r1, 0xc5a    // r1 = (r1 & 0xffff) | (0xc5a << 16)
3242ba70    f2460002    movw    r0, 0x6002
3242ba74    f6c0405a    movt    r0, 0xc5a
3242ba78        4479    add     r1, pc       // r1 = r1 + pc
3242ba7a        4478    add     r0, pc
3242ba7c        6809    ldr     r1, [r1, #0] // r1 = *(r1 + 0)
Variable Length Coder
  • 7,958
  • 2
  • 25
  • 29
  • So in this case, what is that value or r1 going to be? – user1000039 Oct 17 '11 at 22:33
  • 4
    What part of the explanation are you having difficulty with? – Raymond Chen Oct 18 '11 at 04:41
  • The last two bits. So adding r1 to pc and the *(r1+0). What would the value of pc be? And does the * mean the same thing as in c++ (deference operator)? – user1000039 Oct 18 '11 at 06:40
  • The value of `pc` is the value of the program counter, which is the first column of the disassembly. (Though there's a quirk of ARM that you get to figure out.) And the asterisk is just VLC's shorthand for "the word stored at". VLC borrowed C++ notation to save space. (You didn't have trouble with the other C++ notation earlier...) – Raymond Chen Oct 21 '11 at 18:04
  • A better link is the [ARM blog on constants](http://community.arm.com/groups/processors/blog/2010/07/27/how-to-load-constants-in-assembly-for-arm-architecture) – artless noise May 20 '14 at 18:07