ARM M3/M4汇编指令TBB TBH实现复杂表格跳转

一 简单介绍

TBB和TBH两个表格跳转指令通常用于实现C代码中switch语句。
这里只做简述,详细的信息可以查看文档:
The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors, 3rd Edition.pdf
1)TBB
跳转的最大偏移地址2*2^8=512字节
语法格式:

TBB  [Rn, Rm]

流程:
在这里插入图片描述
2)TBH
跳转的最大偏移地址2*2^16=128k字节
语法格式:

TBH  [Rn, Rm, LSL #1]

流程:
在这里插入图片描述

二 TBB简单跳转

R0=0,跳转到Dest0
R0=1,跳转到Dest1
R0=2,跳转到Dest2
R0=3,跳转到Dest3

				LDR     R0, =1
				TBB    [PC, R0]
Table_start
                DCB   ((Dest0-Table_start)/2)
				DCB   ((Dest1-Table_start)/2)
				DCB   ((Dest2-Table_start)/2)
				DCB   ((Dest3-Table_start)/2)
Dest0
                LDR     R1, =0
                B       Table_end
Dest1
                LDR     R1, =1
                B       Table_end
Dest2
                LDR     R1, =2
                B       Table_end
Dest3
                LDR     R1, =3
Table_end

三 TBH简单跳转

R0=0,跳转到Dest0
R0=1,跳转到Dest1
R0=2,跳转到Dest2
R0=3,跳转到Dest3

				LDR     R0, =3
				TBH    [PC, R0, LSL #1]
Table_start
                DCI   ((Dest0-Table_start)/2)
				DCI   ((Dest1-Table_start)/2)
				DCI   ((Dest2-Table_start)/2)
				DCI   ((Dest3-Table_start)/2)
Dest0
                LDR     R1, =0;
                B       Table_end
Dest1
                LDR     R1, =1
                B       Table_end
Dest2 
                LDR     R1, =2
                B       Table_end
Dest3
                LDR     R1, =3
Table_end

四 复杂表格跳转

条件跳转目标标号
R0[7:6]=00Dest0
R0[7:6]=01,R0[5:4]=00Dest1
R0[7:6]=01,R0[5:4]=01Dest2
R0[7:6]=01,R0[5:4]=10Dest3
R0[7:6]=01,R0[5:4]=11Dest4
R0[7:6]=10,R0[1]=0Dest5
R0[7:6]=10,R0[1]=1Dest6
R0[7:6]=11,R0[3:2]=00/01Dest7
R0[7:6]=11,R0[3:2]=10/11Dest8
                LDR     R0, =0x60
				;; 提取R0[7:6]到R1
				;; R1=(R0 & 0xC) >> 6
				UBFX    R1, R0, #6, #2    					
				TBB    [PC, R1]
				;; R1=0   Dest0     
                ;; R1=1   Sub_table0
                ;; R1=2   Sub_table1
                ;; R1=3   Sub_table2
Table_start
                DCB    ((Dest0     - Table_start) / 2)
			    DCB    ((Sub_table0 - Table_start) / 2)
				DCB    ((Sub_table1 - Table_start) / 2)
				DCB    ((Sub_table2 - Table_start) / 2)
								
Sub_table0
                UBFX    R1, R0, #4, #2
				TBB    [PC, R1]
Sub_table0_start
				DCB    ((Dest1 - Sub_table0_start) / 2)
				DCB    ((Dest2 - Sub_table0_start) / 2)
				DCB    ((Dest3 - Sub_table0_start) / 2)
				DCB    ((Dest4 - Sub_table0_start) / 2)
								
Sub_table1   
                ;; TST  R0&2,结果为0,则Z=1,EQ成立
                ;; R0[1]=0   Dest5
				;; R0[1]=1   Dest6
                TST     R0, #2  
			    BEQ     Dest5
                B       Dest6

Sub_table2
                UBFX    R1, R0, #2, #2 
			    TBB    [PC, R1]
                ;; R1=0/1   Dest7
				;; R1=2/3   Dest8		
Sub_table2_start
                DCB    ((Dest7 - Sub_table2_start) / 2)
			    DCB    ((Dest7 - Sub_table2_start) / 2)
			    DCB    ((Dest8 - Sub_table2_start) / 2)
			    DCB    ((Dest8 - Sub_table2_start) / 2)
;;///表格主体
Dest0
                LDR     R5, =0xFFFF0000
				B       Table_end
Dest1
                LDR     R5, =0xFFFF1111
				B       Table_end
Dest2
                LDR     R5, =0xFFFF2222
				B       Table_end
Dest3
                LDR     R5, =0xFFFF3333
				B       Table_end
Dest4
                LDR     R5, =0xFFFF4444
				B       Table_end
Dest5
                LDR     R5, =0xFFFF5555
				B       Table_end
Dest6
                LDR     R5, =0xFFFF6666
				B       Table_end
Dest7
                LDR     R5, =0xFFFF7777
				B       Table_end
Dest8
                LDR     R5, =0xFFFF8888
				B       Table_end
Table_end
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值