-2

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
4

1 回答 1

2

下面展示如何制作一个跳转表。假设跳转表内的偏移量$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"
于 2012-11-02T23:44:22.447 回答