BL 指令 ARM - 它是如何工作的 [英] BL instruction ARM - How does it work

查看:21
本文介绍了BL 指令 ARM - 它是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 ARM 汇编,但我现在被困在某事上.

I am learning ARM Assembly, and I am stuck on something right now.

我知道链接寄存器,如果我没记错的话,它会保存函数调用完成时返回的地址.

I know about the Link Register, which if I'm not wrong holds the address to return to when a function call completes.

所以如果我们有类似的东西(取自 ARM 文档):

So if we have something like that (taken from the ARM documentation):

0 | here
1 |   B there
2 |   
3 |   CMP R1, #0
4 |   BEQ anotherfunc
5 |
6 |   BL sub+rom ;  Call subroutine at computed address.

那么,如果我们把左边的那一列想象成每条指令的地址,那么在地址 1 的 B 之后,链接寄存器的值是 1,对吗?

So, if we think of the column at the left as addresses of each instruction, then after the B there at address 1, the Link Register holds the value of 1 right?

然后程序转到那里的方法,然后它使用链接寄存器的值来知道从哪里返回.

Then the program goes to the method there and then it uses the value of the Link Register to know where to return.

如果我们现在跳到地址 6,我被卡住了,我们知道 BL 将下一条指令的地址复制到 lr(r14,链接寄存器)中的内容.

If we skip to address 6 now, where I am stuck, we know what BL copies the address of the next instruction into lr (r14, the link register).

所以现在它会复制 sub 的地址,它是一个子程序(什么是子程序??)+ rom(这是一个数字?)或 sub+rom 的地址(我不知道这可能是什么).

So now it would copy the address of sub which is a subroutine (what is a subroutine??) + rom (which is a number?) or the address of sub+rom (I don't know what this could be).

但一般来说,我们什么时候需要BL?为什么我们要在上面的例子中使用它?有人可以举个例子说明我们真正需要它吗?

But in general, when would we need BL? Why do we want it in the example above? Can someone give me an example where we would really need it?

谢谢!

推荐答案

似乎有点混乱.这是一个解释:

It seems there is a bit of confusion. Here is an explanation :

B 指令将发生分支.它跳转到另一个指令,并且没有预期的返回.未触及链接寄存器 (LR).

The B instruction will branch. It jumps to another instruction, and there is no return expected. The Link Register (LR) is not touched.

BL 指令会分支,但也会链接.LR会加载内存中BL之后的指令地址,而不是BL之后执行的指令.然后就可以使用 LR 从分支返回了.

The BL instruction will branch, but also link. LR will be loaded with the address of the instruction after BL in memory, not the instruction executed after BL. It will then be possible to return from the branch using LR.

示例:

start:
01:  MOV r0, r2     ; some instruction
02:  B there        ; go there and never return !

there:
11:  MOV r1, r0         ; some instruction
12:  BL some_function   ; go to some_function, but hope to return !
                        ; this BL will load 13 into LR
13:  MOV r5, r0
14:  BL some_function   ; this BL will load 15 into LR
15:  MOV r6, r0


some_function:
     MOV r0, #3
     B LR               ; here, we go back to where we were before

如果你想在一个函数内部调用另一个函数,LR会被覆盖,所以你将无法返回.常见的解决方法是用PUSH {LR}将LR保存在栈上,在返回前用POP {LR}恢复.您甚至可以在单个 POP {PC} 中恢复和返回:这将恢复 LR 的值,但在程序计数器中,有效地返回了函数.

If you want to call another function inside a function, LR will be overwritten, so you won't be able to return. The common solution is to save LR on the stack with PUSH {LR}, and restore it before returning with POP {LR}. You can even restore and return in a single POP {PC} : this will restore the value of LR, but in the program counter, effectively returning of the function.

这篇关于BL 指令 ARM - 它是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆