所以这是我的数组 [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:
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
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(73条)