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

允我心安 提交于 2019-12-20 02:54:16

问题


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

Is STR X30, [SP, #8] correct ? could you please explain the concept of maintaining stack alignment ? I am relatively new to ARMv8 so excuse me.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/27941220/push-lr-and-pop-lr-in-arm-arch64

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!