How to create a jump table using a jr instruction?

C++ Program

 # 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;
 }

Translate the C code to MIPS assembly code, add to your code the capability to match the closest resistor. Be sure to use the jr instruction for the switch statement. Have your code get the resistance as input from the user and display to the console the resistor's corresponding or closest part number.

Mips Assembly Code

.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

The following shows how to make a jump table. The offset within the jump table is assumed to be in $s1. $s1 must be a multiple of 4 (ie a byte offset).

This code has not been tested!

        . . . .
        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/19220.html

上一篇: 将MIPS二进制代码翻译成MIPS程序集

下一篇: 如何使用jr指令创建跳转表?