【问题标题】:Using jump tables in MIPS (how to jump a label in JT array )在 MIPS 中使用跳转表(如何在 JT 数组中跳转标签)
【发布时间】:2013-04-13 16:20:15
【问题描述】:

我正在尝试使用跳转表为我的工作制作菜单。 Everyting 对我来说看起来不错,但下面的代码不起作用。在“jr $s0”指令之后,火星给了我一个错误,比如:

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

【问题讨论】:

    标签: mips mips32 smips


    【解决方案1】:

    您试图跳转到存储地址的数组,这是没有意义的。您需要在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
    

    【讨论】:

      【解决方案2】:

      您可以使用jTable 作为加载字调用的直接偏移量,以更少的代码完成与迈克尔建议相同的事情。

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

      【讨论】:

      • 没有可以直接传递标签(地址)的lw指令。如果jTable 位于大于0x7fff 的地址,则无论如何都不可能,因为偏移量是有符号的16 位整数。
      • 事实证明,我错了。可以将地址直接传递给lw。我很抱歉。
      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      相关资源
      最近更新 更多