PUSH {lr} and POP {lr} in ARM Arch64

后端 未结 2 1819
天命终不由人
天命终不由人 2021-01-22 16:20

What is the equivalent instruction for PUSH{lr} and POP{lr} in ARM Arch64 instruction set .

Is STR X30, [SP, #8] correct ? could y

相关标签:
2条回答
  • 2021-01-22 16:33

    If you ask the C compiler to generate an assembly language listing from your source, you'll see how it handles pushing data on the stack for ARMv8. This might not be the only way to do it, but GCC does it this way:

       sub  sp, sp, #32     \\ Open up some temp stack space
       stp  x19, x20, [sp]  \\ save 2 pairs of registers
       stp  x21, x30, [sp,#16]
     <your code>
       ldp  x19, x20, [sp]  \\ restore 2 pairs of registers
       ldp  x21, x30, [sp,#16]
       add  sp, sp, #32     \\ "free" the temp stack space
    
    0 讨论(0)
  • 2021-01-22 16:40

    STR X30, [SP, #8] is totally wrong.

    1. The most important point about Aarch64 stack is that SP MUST BE 16 Byte aligned.

    2. Stack is descending. So SP should be moved left. sub sp, sp, #CONST. In your example you actually mess up data of parent function.


    If you need to preserve LR which is actually x30 in Aarch64 use

    str         x30,        [sp,#-16]!
    


    Technically, it's possible to preserve on register only by

    str         x30,        [sp,#-8]  // sp is not changed here! but data is written in permitted area
    

    but with assumption that your function doesn't call any other subfunctions. But why on Earth save LR in this case?

    Also Aarch64 could use any other register to perform return from a function. For example:

    mov x7, x30 // preserve LR
    blr .L.my.bloody.subroutine   // blr will mess up LR/x30
    ...
    ret x7      // returning from function by using preserved req
    


    In case you need to preserve more than 2 registers use example provided by @BitBank


    Finally, you could not modify pc, so there is only one way to return by using ret

    0 讨论(0)
提交回复
热议问题