Search code examples
assemblyfunction-pointerssubroutine68000

Call subroutine by dynamic value


I'm quite new to 68k and I was wondering if it's possible to call a specific subroutine by values held in memory.

pseudo code example:

X: dc.w 0

routine1: 
code
rts

routine2:
more code
rts

and somewhere in the code something like:

move.w #2,X
JSR routine(X)

to have routine2 executed, ore move.w #1,X before for routine1

I have no idea and can't find any example, my guess is to make a label containing the routines then using an address register jump to the specific offset but don't know how.

Any help would be welcome!


Solution

  • So this is how I ended up doing this, combining Peter Corder solution with some external suggestions:

      TIMELINE:       DC.L __BLOCK_0,__BLOCK_1,__BLOCK_1
        DC.L __BLOCK_2,__BLOCK_2
        DC.L __BLOCK_2,__BLOCK_3 ... etc
    
    
     __BLOCK_0:
       ; SOME CODE
       RTS
    

    -- in mainloop --

    MOVE.W  P61_LAST_POS,D5
    LEA TIMELINE,A3
    MULU.W  #4,D5     ; OFFSET IN BYTES
    MOVE.L  (A3,D5),A4
    JSR (A4)        ; EXECUTE SUBROUTINE BLOCK#
    

    where P61_LAST_POS being and incremental index which changes over periods of time.

    This way I have a control of what is executed at any given point by just editing the LUT "TIMELINE".