用于查找用户输入的MIPS代码

时间:2017-04-28 17:37:11

标签: arrays mips

所以这是我的阵列[1,3,6,2,8,4,9,11,5,14,9999,34]。我想编写MIPS代码来查找输入(来自用户)的数字是否包含在数组中。

如果包含输入的数字,则应输出其索引。

如果未包含输入的号码,则应输出"未找到"。

.data # this is a data structure in MIPS

Array:  .word  1, 3, 6, 2, 8, 4, 9, 11, 5, 14, 9999, 34 # this is an array

welcome: .asciiz "Enter an integer"

.text

li $vo, 4

la $a0, welcome

syscall

li $vo, 5

syscall

sw $v0, ($s0)     # saves user input into address at $s0
    addi $s0, $s0, 4          # increments address at $s0 by 4 bytes

   addi $t2, $t2, 1   

  .globl main

 main:
li $t2, 0 # let $t2 be 0

la $t0, Array # load the array "Array"'s base address to register $t0

lw $s0, 0($t0) # load the first element in the "Array", which is Array[0]. 
 loop:

1 个答案:

答案 0 :(得分:0)

您的代码有很多错误,我已经为您编辑并编写了一些简单的if/else来确定用户输入的数字在数组中是否相等,它一次只测试一个整数检查整个数组,你需要编写一个循环,使用我在这里写的相同的主体。祝你好运。

.globl main
.data # this is a data structure in MIPS

Array:  .word  1, 3, 6, 2, 8, 4, 9, 11, 5, 14, 9999, 34 # this is an array

welcome: .asciiz "Enter an integer"
notfound:.asciiz "Not found"

.text
 main:
li  $v0, 4          #print string

la  $a0, welcome
syscall

li  $v0, 5          #read integer
syscall
move  $t1, $v0


la  $t0, Array      # load the array "Array"'s base address to register $t0

lw  $s0, 0($t0)     # load the first element in the "Array", which is Array[0]. 
beq $t1,$s0, L1     #branch to L1 if t1 = s0
li  $v0,4
la  $a0, notfound
syscall
b  end
L1: 
move    $a1,$s0
li  $v0,4
syscall 
end:    
li $v0,10       #exit the program
syscall