【问题标题】:How to create a jump table using a jr instruction?如何使用 jr 指令创建跳转表?
【发布时间】:2012-11-02 07:30:11
【问题描述】:

C++ 程序

 # include < iostream >
 # include <string >

 using namespace std;

 int main ()
 {
 int resistance ; // in Ohms
 string partNum ; // Part Number

 cout << " Enter resistance : " << endl ;
 cin >> resistance ;
 switch ( resistance )
 {
 case 3 : partNum = " OD30GJE "; break ;
 case 10 : partNum = " OD100JE "; break ;
 case 22 : partNum = " OD220JE "; break ;
 default : partNum = "No match "; break ;
 }    
 cout << " Part number : " << partNum << endl ;    
 return 0;
 }

将 C 代码转换为 MIPS 汇编代码,为您的代码添加匹配最近电阻的能力。请务必使用 jr 指令进行 switch 语句。让您的代码从用户那里获取电阻作为输入,并向控制台显示电阻的对应或最接近的零件编号。

Mips 汇编代码

.data
int_value: .space 20
.align 2
input:  .asciiz "Enter resistance.\n"       # declaration for string variable, 
string1:    .asciiz "OD30GJE\n" # declaration for string variable, 
string2:    .asciiz "OD100JE\n"
string3:    .asciiz "OD220JE\n"
string11:   .asciiz "No Match\n"
string12:   .asciiz "Enter resistance\n"
    .text
main:
li $v0, 4
la $a0, input                   # print for input 
syscall

la      $t0, int_value
li  $v0, 5          # load appropriate system call code into register $v0;

syscall             # call operating system to perform operation
sw  $v0, int_value      # value read from keyboard returned in register $v0;
                        # store this in desired location
lw  $s1, 0($t0)
condition1:
slt $t1, $s1, $zero # if $s1 < 0 $t1 = 1 else $t1 = 0
beq $t1, $zero, condition2 # if $t1 = 0; InvalidEntry 
bne $t1, $zero, invalid_entry

condition2:
sgt $t1, $s1, -1 # if $s1 > -1 then $t1 = 1 else $t1 = 0
beq  $t1, $zero, invalid_entry # if $t1 = 0; InvalidEntry 
sgt $t1, $s1, 9 # if s1 > 9 t1 = 1 else $t1 = 0 
bne $t1, $zero, condition3 # if $t1 does not equal = 0; condition3 

li  $v0, 4              
la  $a0, string1
syscall
j exit

condition3:
sgt $t1, $s1, 9 # if $s1 > 9 then $t1 = 1 else $t1 = 0
beq  $t1, $zero, invalid_entry # if $t1 = 0; InvalidEntry 
sgt $t1, $s1, 21 # if s1 > 21 t1 = 1 else $t1 = 0 
bne $t1, $zero, condition3 # if $t1 does not equal = 0; condition3 

li  $v0, 4              
la  $a0, string2
syscall
j exit

invalid_entry:
li  $v0, 4              
la  $a0, string11
syscall
j exit
exit:
li $v0, 10 # v0<- (exit)
syscall

【问题讨论】:

  • 我正在使用 Mars 汇编器
  • @PaulR 如何创建跳转表以便我可以使用 jr 指令?
  • 好的 - 你应该把它放在问题本身中,以明确你在问什么 - 否则大多数人会看到几页代码而没有明显的问题,然后继续前进下一个问题。

标签: assembly mips mips32


【解决方案1】:

下面展示了如何制作一个跳转表。跳转表内的偏移量假设在$s1.$s1必须是4的倍数(即一个字节偏移量)。

此代码未经测试!

        . . . .
        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"

【讨论】:

  • 我不明白为什么我现在被禁止获得这么多反对票。这个问题很直接,我得到了答案。请帮忙投赞成票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-14
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
相关资源
最近更新 更多