Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 208
jkg Avatar asked Sep 12 '25 12:09

jkg


2 Answers

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
like image 149
BitBank Avatar answered Sep 14 '25 23:09

BitBank


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

like image 43
user3124812 Avatar answered Sep 15 '25 00:09

user3124812