How to create a jump table using a jr instruction?

眉间皱痕 提交于 2019-12-01 02:01:26

The following shows how to make a jump table. The offset within the jump table is assumed to be in $s1. $s1 must be a multiple of 4 (i.e. a byte offset).

This code has not been tested!

        . . . .
        b       1f            # jump past the jump table
        nop                   # branch delay slot
        # table of jump addresses
JTAB:   .word LABEL1
        .word LABEL2
        .word LABEL3
1:      la      $t0, JTAB     # load start address of the jump table
        add     $t0, $t0, $s1 # add offset to table address
        lw      $t1, 0($t0)   # load the address stored at JTAB + $s1
        jr      $t1           # and jump to that address
        nop                   # branch delay slot
LABEL1: # do something
        b       2f            # break
        nop
LABEL2: # do something else
        b       2f            # break
        nop
LABEL3: # do a different thing
        b       2f            # break
        nop
2:      # after the end of the "case statement"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!