10

I've been reverse-engineering a 6303/6800 binary from the early 1980s, which includes a subroutine for jumping to one entry in a table of relative offsets in memory, based upon whether the value of a variable is lower than an offset's associated index value. It's similar to C's switch statement, but not quite the same.

In this implementation the table of two byte entries is defined adjacent to the subroutine call, so that the table address will be pushed onto the stack as the subroutine return address. This address is then popped off the stack in the subroutine, and then iterated over.

Please forgive what may be an ignorant question. I'm somewhat new to 8-bit programming. I'm wondering if this technique had a common name at one point. Since I'm working backwards from the binary, I'm left to add my own names.

In my work with it, someone referred to it as a 'jumpoff', and for the lack of a name the term stuck. See below for the definition, and an example usage.

; Definition
jumpoff:                                        SUBROUTINE
    PULX

.test_table_entry_offset:
; If the current jump table entry number is '0', the end of the jump table has
; been reached, so exit.
    TST     1,x
    BEQ     .load_offset_and_jump

; If the value in the entry 'index' is higher than the value in ACCB being
; tested, jump to the relative offset contained in this entry.
    CMPB    1,x
    BCS     .load_offset_and_jump
    INX
    INX
    BRA     .test_table_entry_offset

.load_offset_and_jump:
; Load the relative offset in the current entry, add this to the return
; address popped from the stack, and jump to it.
    PSHB
    LDAB    0,x
    ABX
    PULB
    JMP     0,x
; Invocation.
    LDAA    ui_mode_memory_protect_state
    JSR     jumpoff

    DC.B input_slider - *
    DC.B 1
    DC.B input_button_yes_no - *
    DC.B 3
    DC.B input_button_main - *
    DC.B 8
    DC.B input_button_numeric - *
    DC.B 0

If anyone is wondering where this came from, it came from the Yamaha DX7/9 ROM binaries. The annotated assembly is from a project of mine working with them: https://github.com/ajxs/yamaha_dx97

4
  • 5
    Does it have to have a name? It's simply a jump table which jumps to a different subroutine based on the input index, but instead of each index having a jump address, the table is scanned for range of indexes which match to a single jump address. In this case the table is just compacted to contain the index and address in one byte to save space, as the jump addresses are expressed in byte offset. So kind of specific way. Likely done like that, as the ROMs are few bytes short of full.
    – Justme
    Mar 26, 2023 at 9:06
  • 3
    I do need to give the subroutine a name. I'm just wondering if this is a common technique that people had a specific name for back when this was written.
    – ajxs
    Mar 26, 2023 at 9:25
  • 1
    @ajxs Call it whatever you like. it's your program.
    – Raffzahn
    Mar 26, 2023 at 12:39
  • 3
    Or more like, name it anything you want because you are the one who is disassembling the binary, so it matters to you how you understand it the best. I have to agree that the DX jump table construct was clever and tricky to disassemble when I first saw it. But it's just a jump table still, or for a C programmer, equivalent to having a switch with multiple cases jumping to same subroutine. Could be handmade assembly or macro, or in a pinch, generated by some compiler.
    – Justme
    Mar 26, 2023 at 16:40

2 Answers 2

18

The term that comes to mind is "dispatch table". This is a table containing pointers to functions, one of which will be chosen based on an index that has been provided or gets calculated.

This is a technique that was used from time to time in the 1960s or 1970s. Judging by this Wikipedia article, it was still in use when Javascript came into being.

A dispatch table can be used as the target of a call instruction. When the routine invoked by the dispatch table completes the current action, the call can be replaced by a jump. Your case looks like the second situation.

8
  • 2
    Dispatch tables have been used since I started programming decades ago, and they're still used. The only slightly unusual aspect of this one is the need to scan rather than directly indexing into a table.
    – dave
    Mar 26, 2023 at 12:29
  • 5
    @Justme A dispatch table is a jump table - Naming depends where one learned the trade. In mainframe environments the name Dispatch Table was commonly used by various tools/language packages. To be named that it does not matter if it's implemented one way jumps or subroutine calls. In fact, it does not even have to be a subroutine call at all to return, as each of the routines can quite well return by simply jumping 'back' to a common label - something even simple C compilers use to handle selections.
    – Raffzahn
    Mar 26, 2023 at 12:44
  • 3
    @ErikEidt True, it contains no pointers directly, just byte offsets that need to be added to the current 16-bit absolute offset of the entry. Saves a byte and can still store near offsets.
    – Justme
    Mar 26, 2023 at 17:51
  • 1
    OP should check out FORTH which is simply jump tables linked to other jump tables linked to other jump tables until eventually you come out to some actual machine code (usually no more than 5-7 instructions down there). Or at least this is the way it looks for one point of view ... there are others ...
    – davidbak
    Mar 26, 2023 at 18:14
  • 2
    @Justme: I would not call all jump tables "dispatch tables", not all dispatch tables "jump tables". A jump table whose clients would generally be hard-coded to use particular entries (e.g. the entry points near the top of Commodore Kernel ROMs) is not a dispatch table, and a dispatch table whose entries are not usable directly via direct or indirect "jump" instructions is not a jump table.
    – supercat
    Mar 27, 2023 at 15:55
8

Is there a common name for this 'switch' like 6800 assembly routine?

No. There is no specific name for exactly this technique. In fact, even generic names vary depending on whom to ask.

I do need to give the subroutine a name. I'm just wondering if this is a common technique that people had a specific name for back when this was written.

Why not call simply '(function) switcher' - so, FSWTCH? After all, unless there are multiple versions in within the same program with different purpose and structure, people tend to use very generic terms - same way one would today.


There is also a possible flaw:

Is the question about the structural or implementational aspect?

Structural

Structure wise it is a CASE type structure, a variation/optimization of the generic Selection:

enter image description here

As such naming depends on the language used.

  • CASE for Algol
  • Switch for C
  • ON ... GOTO/GOSUB for BASIC
  • etc.

This includes 'no name' at all, if the language used doesn't support such a selection type, usually the case with Assembly.

Implementational

While often being a single construct in high level languages, compiler can use a broad range of techniques to generate (hopefully) best fitting code. Assembler of course offers all of that plus whatever one may come up with tailored for a specific case like yours.

Since this is based on a data structure holding values and targets, names like

  • Jump-Table
  • Branch-Table
  • Dispatch-Table
  • Lookup-Table
  • Computed Go-To

are common. Usage really differs mostly by taste and heritage.

For this it does not matter if the noted target is reached via

  • a Go-To (JMP), or
  • a Subroutine call (JSR)

nor does it really make a difference how the table is searched

  • Direct access,
  • Indexed access,
  • Linear scan, or
  • Binary scan

will all have their merits depending on the usual constraints of code size, table size, runtime and implementation effort.

9
  • 2
    Well, today I've learnt the German words for "value", "otherwise" (or "else" might be better, I guess), and "instruction" :-) It's a good job that I actually looked up "wert" as I had (wrongly) assumed that it meant "word". Mar 27, 2023 at 19:48
  • 1
    @Greenonline :)) Sorry, I was simply too lazy and the English wiki didn't have any useful example. Could have used the French :) Seems like NS is way more common in Germany, while most of the English world is still stuck with flowcharts.
    – Raffzahn
    Mar 27, 2023 at 22:46
  • @Raffzahn What is NS? That kind of chart? I have never seen it before, despite having lived for a while in Germany. Mar 28, 2023 at 16:17
  • @Героямслава NS stands for Nassi-Shneiderman-Diagramm (English). It's a set of drawing rules that follow the rules of structured programming. The result is very readable not at least as it makes drawing spaghetti code impossible - unlike Flowcharts. I use them since ca. 1980 whenever possible for documentation and especially all printed listings. Makes debugging so much more effortless as reading is always top down.
    – Raffzahn
    Mar 28, 2023 at 16:27
  • 1
    @Greenonline, you may have guessed this already, but “Wert” is cognate with English “worth”. English “word” is German “Wort”.
    – Carsten S
    Nov 2, 2023 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .