I've been learning Arm32 assembly on my Raspberry Pi recently and all is going well but I would like to create a jump table and have no idea how. The bl (branch and link) instruction needs an address label, but I would like to simulate a switch statement by creating a jump table of addresses that can be jumped to but I don't know how to do this. As I'm emulating an 8-bit computer with 256 possible opcodes I need to be able to define 256 opcode handlers and set up a jump table containing the address of these handlers so I can bl to them based on the opcode as an index. How may I accomplish this task?
Many thanks in advance!
Look for the table opcodes.
Do you mean TBB and TBH? I looked them up as well as "branch table" and very little comes up, just some abstract description and no example of how to actually implement it. Okay, so it lets you perform a relative byte or half-word branch... but how do I describe this on a per-label basis? Let's say I have four labels, ADD, SUB, MUL, DIV and I want to jump to them based on whether a register value is 0 to 3; how do I set this up in the assembler? How do I know what the individual offsets are going to be? How do I actually define a jump-table and assign each entry to correspond to ADD, SUB, MUL, DIV? How do I return from each subroutine? If the documentation is sufficient to explain this then I apologize for being daft, I just don't see it. Any examples?
Okay, I see how it's done almost 2/3 of the way through this web page : http://thinkingeek.com/2013/08/23/arm-assembler-raspberry-pi-chapter-16/
Is this correct? I'll try this out when I get home. It's frustratingly difficult to find an actual guide or doc on this.
Yup, the approach in the above link worked.
looks overly complicated
Pseudo code:
LD byte,[programcounter]
LD PC,[table+byte*8]
table:
long opcode0, opcode1, opcode2, opcode3
....
long opcode255
opcode0: