2013-04-13 94 views
2

我正在使用跳轉表爲我的工作做菜單。 Everyting看起來不錯,但下面的代碼不起作用。在「JR $ S0」指令火星後,給了我這樣的錯誤:在MIPS中使用跳轉表(如何在JT陣列中跳轉標籤)

Error in : invalid program counter value: 268501840

我知道十進制地址268501840是L1標籤的實際地址和代碼應該去那個標籤,但在這一點上我接受這個錯誤。爲什麼?

main: 
.data 
jTable: .word L0,L1,L2,L3,L4,L5,L6,default  #jump table definition 
msg: .asciiz "\nEnter Your Choice;\n [1] for build,\n [2] for insert,\n [3] for  find,\n [4] for findMinMax,\n [5] for delete,\n [6] for print\n [0] for Exit\nYour choice:#" 
.text 
userInteraction: 
li $v0,4   #print string 
la $a0,msg   #get string address 
syscall 

li $v0,5   #get a menu option from user(0 to 6) 
syscall 
move $s0,$v0   #get index in $s0 

sll $s0,$s0,2  #$s0=index*4 
la $t0,jTable  #$t0=base address of the jump table 
add $s0,$s0,$t0  #$s0+$t0 = actual address of jump label 

**jr $s0**   #jump to label 

L0: Todo 
j finish 
L1: Todo 
j userInteraction 
L2: Todo 
j userInteraction 
L3: Todo 
j userInteraction 
L4: Todo 
j userInteraction 
L5: Todo 
j userInteraction 
L6: Todo 
j userInteraction 
default: Todo 
j userInteraction 
finish: 
li $v0,10  #Exit 
syscall   #Exit 

回答

2

你想跳轉到存儲地址的數組,這是沒有意義的。您需要將jr指令之前從表中加載目標地址:

sll $s0,$s0,2  #$s0=index*4 
la $t0,jTable  #$t0=base address of the jump table 
add $s0,$s0,$t0  #$s0+$t0 = actual address of jump label 
lw $s0,($s0)  # <-- load target address 
jr $s0    #jump to label 
1

可以完成同樣的事情,邁克爾用更少的代碼提示使用jTable作爲直接爲您的負載話通話抵消。

sll $s0,$s0,2  # $s0=index*4 
lw jTable($s0)  # load the target address 
jr $s0    # jump to the lable