I am new to ARM Cortex and facing difficulty to understand how TBB
branches.
Code snippet attached:
TBB [PC,R1]
BrTable1
DCB ((P0 - BrTable1)/2)
DCB ((P1 - BrTable1)/2)
Now, my understanding is as follows:
TBB [PC,R1]
will try to look for BrTable1 at PC + 4
(next instruction). Instead of PC
, I can also give BrTable1
pointer here. R1
is counter and for next N
cycles depending on R1
possible values, it will have cases.
Branching will happen as : PC_new = (BrTable1_pointer) + 2 * (data written at BrTable1_pointer + R1))
whereas R1
will be as a up-counter from 0 to X
(For 8 bit, X = 255
)
For R1 = 0
, DCB ((P0 - BrTable1)/2)
will
- Branch my code to
(BrTable1 + 2 * ((P0 - BrTable1)/2))
- Branch my code to
(BrTable1 + (P0 - BrTable1))
- Branch my code to
P0
Now this is as per expectation also.
Query
Intention is to compare the value of R1
and accordingly branch.
- Is my understanding correct?
- If I want to write only 2 cases for
R1 == 0
andR1 == 10
then should I write myDCB
instruction at( BrTable1 + 10 )
location or is there any method to write it in next instruction only? - Do I have any control on
R1
?
R1
is used, not just the low 8 bits.TBB
is particularly useful for dense branch tables. If your table is sparse, it might be better to just use a conditional branch or two.