Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement switch case in MIPS

i'm trying to implement switch case in mips using jump table as an array of labels to jump to:

    .data
JumpTable:
    .word L0, L1, L2, L3

    .text
    .globl main
main:
    la $t4, JumpTable      # address of JumpTable
    sll $t1, $s5, 2     # s5 is value of the variable witch we want to switch on it.
    add $t1, $t1, $t4 
    lw $t0, 0($t1)
    jr $t0

    L0:
        ...
    L1:
        ...
    L2:
        ...
    L3:
        ...

my question is, is there any way to do this for not sequential variables?(like 1,4,19,40)

like image 788
Branky Avatar asked Sep 02 '25 03:09

Branky


1 Answers

You can add doubles to your table

.word L0, L0, L0, L0, L1, L2, L2, L2, L2, L2, L2, L3, ...

OR

use conditional branching. It depends on your intended application.

like image 92
Patrik Avatar answered Sep 04 '25 23:09

Patrik