0

I'm trying to store values 6 and 7 on stack, This is my code:

    randomNum:  .string "%d "

    add sp, sp, -8
    mov x27,    #6
    str x27,    [x29, -4]
    mov x27,    #7
    str x27,    [x29, -8]
    
    ldr x0, =randomNum
    ldr x1, [x29, -4]
    bl  printf
    ldr x0, =randomNum
    ldr x1, [x29, -8]
    bl  printf

    add sp, sp, 8

The output is always

0 7

So 6 seems aren't stored on the stack. How does the str instruction works? Should I use strh instead of str?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Z Han
  • 11
  • 1
  • 3
  • 1
    Where is `x29` initialized? – Michael Nov 23 '20 at 08:33
  • 1
    `str x27, [x29, -8]` is an 8-byte store that overwrites the low 4 bytes of the first store. Perhaps you meant to do `w27`? If you single-step with a debugger, you'd see the value there in memory before it's overwritten by the 2nd store. BTW, you could have stored both 32-bit int values with one 64-bit store, but it would take 2 instructions to get `0x0000000600000007` into an x register. Still, that's 2 ALU and 1 store, vs. 2 mov / 2 store. – Peter Cordes Nov 23 '20 at 08:38
  • I think you have mixed up the *stack pointer* `sp` with the *frame pointer* `x29`. To put stuff on the stack, use a store relative to `sp`. You *can* use the frame pointer to access the stack, but only if you have set it to point to a known position on the stack, which you didn't do (or if you did, you didn't show that part of your code). Also by the way, the stack is supposed to be kept aligned to 16 bytes, so you should subtract and add 16 instead of 8 from `sp`, even though you don't intend to do anything with the other 8 bytes. – Nate Eldredge Nov 23 '20 at 15:18

0 Answers0