4

I have some ARMv7 instructions that I do not understand, despite reading the reference at: ARM Information Center

In the context of:

  a7a4d8:   b530        push    {r4, r5, lr}
  a7a4da:   466c        mov r4, sp
  a7a4dc:   4605        mov r5, r0
  a7a4de:   682a        ldr r2, [r5, #0]
  a7a4e0:   ebad 0d02   sub.w   sp, sp, r2
  a7a4e4:   f104 0014   add.w   r0, r4, #20 ; 0x14
  a7a4e8:   4669        mov r1, sp
  a7a4ea:   b082        sub sp, #8
  a7a4ec:   466a        mov r2, sp
  a7a4ee:   462b        mov r3, r5
  a7a4f0:   f746 f1b8   bl  5c0864 <RoutineName>
  a7a4f4:   9800        ldr r0, [sp, #0]
  a7a4f6:   9901        ldr r1, [sp, #4]
  a7a4f8:   46a5        mov sp, r4
  a7a4fa:   bd30        pop {r4, r5, pc}

What does the following do? Can someone explain in terms of pseudo-code?

  a7a4de:   682a        ldr r2, [r5, #0]
  a7a4e0:   ebad 0d02   sub.w   sp, sp, r2
  a7a4e4:   f104 0014   add.w   r0, r4, #20 ; 0x14

  a7a4f4:   9800        ldr r0, [sp, #0]
  a7a4f6:   9901        ldr r1, [sp, #4]
5

ldr r2, [r5, #0]
means r2=*(r5+0)
which loads the value pointed to by r5 and places it in r2.

sub.w sp, sp, r2
means sp=sp-r2
which subtracts sp by the value in r2 (to allocate stack space).

add.w r0, r4, #20 ; 0x14
means r0=r4+20
which adds 20 (decimal) to r4 and places the result in r0.

ldr r0, [sp, #0]
means r0=*(sp+0)
which loads the value pointed to by sp and places it in r0.

ldr r1, [sp, #4]
means r1=*(sp+4)
which loads the value pointed to by (sp+4) and places it in r1.

In C pseudo-code it looks something like this:

x_a7a4d8(dword *ptr_allocsize, void *arg1)
{
  alloca(*ptr_allocsize)
  dword p2;
  qword p1;
  x_5c0864(&arg1, &p1, &p2, ptr_allocsize)
  return p1;
}

so it allocates some space for the value returned by the 5c0864 routine (because it uses the stack to return the value), calls the 5c0864 routine, and returns the value returned by 5c0864.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.