1

Section 3.10.4 of the Arm generic user guide (page 172) gives an example for using TBB, but the example uses Arm assembler. I would like to learn how to use TBB with gas, but can't seem to figure out how. How should I revise the example from the guide to implement a switch statement with gas instead of armasm?

ADR.W R0, BranchTable_Byte
TBB [R0, R1] ; R1 is the index, R0 is the base address of the
             ; branch table
Case1
; an instruction sequence follows
Case2
; an instruction sequence follows
Case3
; an instruction sequence follows
BranchTable_Byte
    DCB 0 ; Case1 offset calculation
    DCB ((Case2-Case1)/2) ; Case2 offset calculation
    DCB ((Case3-Case1)/2) ; Case3 offset calculation

I'm new to using gas and am not sure if I should be defining the branch table in a .data section at the beginning of the assembler file or if it should go after my switch statement in the .text section.

artless noise
  • 21,212
  • 6
  • 68
  • 105
user50420
  • 39
  • 1
  • 6

1 Answers1

1
.cpu cortex-m3
.thumb
.syntax unified

ADR.W R0, BranchTable_Byte
TBB [R0, R1] @; R1 is the index, R0 is the base address of the
             @; branch table
Case1:
@; an instruction sequence follows
    nop
Case2:
@; an instruction sequence follows
    nop
    nop
Case3:
@; an instruction sequence follows
    nop
    nop
    nop
BranchTable_Byte:
.byte 0 @; Case1 offset calculation
.byte ((Case2-Case1)/2) @; Case2 offset calculation
.byte ((Case3-Case1)/2) @; Case3 offset calculation

something like this perhaps. Need colons on labels. ; is sadly not a comment anymore @ is, got lucky on the label math.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • this should all be in the gas documentation. – old_timer Oct 03 '18 at 00:39
  • for some reason I was thinking the table would have to be declared in the .data section... I'm not sure why it doesn't have to be if it is considered initialized data – user50420 Oct 22 '18 at 01:51
  • you technically could have it in .data esp if you want it volatile (something you can change runtime), but you have to then build the rest of the code around that so it works, the code you started with was not written for that. If you simply want to have a jump table and have the toolchain do the work for you then you do the kind of thing you started with and ported/shown here. And like the code that makes up the table the jump table itself would want to be in .text along with it as they are intimately related. – old_timer Oct 22 '18 at 02:23
  • being an mcu you generally have more flash than ram, so something like this unless you need it to be changed at runtime, would want to be all in flash. – old_timer Oct 22 '18 at 02:24
  • thank you for returning to provide additional insight – user50420 Oct 22 '18 at 02:34
  • My guess is you are trying to make it more complicated than it is. Hopefully between the arm documentation and the example(s) here the instruction is pretty dumb (as are basically all instructions in an instruction set), so as a programmer you "simply" fill the operands to the instruction correctly so that it does what you want. the arm documentation will have some pseudo code that helps to describe what is going on although sometimes that can create as much confusion as it solves. – old_timer Oct 22 '18 at 02:41