问题
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.
The most important point about Aarch64 stack is that
SP
MUST BE 16 Byte aligned.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