如何使用jr指令创建跳转表?

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汇编代码,将代码添加到匹配最近电阻的功能。 确保使用switch语句的jr指令。 让您的代码获得来自用户的输入阻抗,并向控制台显示电阻器对应或最近的部件号。

Mips汇编代码

.data
int_value: .space 20
.align 2
input:  .asciiz "Enter resistance.n"       # declaration for string variable, 
string1:    .asciiz "OD30GJEn" # declaration for string variable, 
string2:    .asciiz "OD100JEn"
string3:    .asciiz "OD220JEn"
string11:   .asciiz "No Matchn"
string12:   .asciiz "Enter resistancen"
    .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

以下显示如何制作跳转表。 跳转表内的偏移量假定为$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"
链接地址: http://www.djcxy.com/p/19219.html

上一篇: How to create a jump table using a jr instruction?

下一篇: Register length, Memory Size and 32/64 bit computing (MIPS)