我知道在函数调用开始时需要将链接寄存器压入堆栈,并在返回之前将该值弹出至程序计数器,以使执行能够从函数调用之前的位置继续。
但我不明白为什么大多数人会通过添加额外的寄存器来实现这一点。例如:
push {ip, lr}
...
pop {ip, pc}
例如,这是一段ARM汇编中的Hello World代码,由官方ARM博客提供:
.syntax unified
@ --------------------------------
.global main
main:
@ Stack the return address (lr) in addition to a dummy register (ip) to
@ keep the stack 8-byte aligned.
push {ip, lr}
@ Load the argument and perform the call. This is like 'printf("...")' in C.
ldr r0, =message
bl printf
@ Exit from 'main'. This is like 'return 0' in C.
mov r0, #0 @ Return 0.
@ Pop the dummy ip to reverse our alignment fix, and pop the original lr
@ value directly into pc — the Program Counter — to return.
pop {ip, pc}
@ --------------------------------
@ Data for the printf calls. The GNU assembler's ".asciz" directive
@ automatically adds a NULL character termination.
message:
.asciz "Hello, world.\n"
问题1: 为什么要使用所谓的“虚拟寄存器”?为什么不直接使用push{lr}和pop{pc}?他们说这是为了保持堆栈8字节对齐,但堆栈不是4字节对齐吗?
问题2: "ip"寄存器是什么(即r7还是其他什么)?