.data
p1: .asciiz "\nEnter the data for a sorted array: "
p2: .asciiz "\nEnter a value to search for: "
p3: .asciiz " is not found"
p4: .asciiz " is found at "
array: .space 404
.text
main:
la $t0, array # a1 = addr of first int
#Store Array Values
store:
li $v0, 4 # system call code for print_str
la $a0, p1 # address of string to print
syscall # print the first prompt
li $v0, 5 # system call for read_int
syscall # read int to store
move $t1, $v0 # store int
beq $t1, 0, bsearch # once sentinel val hit, go to search
sub $t9, $t0, 4 # set index to the first bit of the last number
j storeVal # store s0 in array
storeVal:
sw $t1, array($t0) # store value in array
addi $t0, $t0, 4 # inc index
j store # continue to add integers
#Binary Search
bsearch:
li $v0, 4 # system call code for print_str
la $a0, p2 # address of string to print
syscall # print the second prompt
li $v0, 5 # system call for read_int
syscall # read int to find
move $a0, $v0 # store int to find
beq $a0, 0, end # once sentinel val hit, go to end prog
la $a2, array # a1 = addr of first int
la $a2, ($t9) # a2 = addr of last int
subu $sp, $sp, 4 # 4 bytes 4 stack
sw $ra, 4($sp) # save return addr
subu $t0, $t9, $s0 # size of array
bnez $t0, searchVal # if not 0, search
move $v0, $a1 # addr of only entry
lw $t0, ($v0) # load entry
beq $a0, $t0, found # if = to int, print found
j notFound # not found
searchVal:
sra $t0, $t0, 3 # comp offset of middle m:
sll $t0, $t0, 2 # t0 = 4(t1/8)
addu $v0, $a1, $t0 # middle m
lw $t0, ($v0) # t0 = m
beq $a0, $t0, found # if = to int, print found
blt $a0, $t0, left # search left
j right # search right
right:
addu $a1, $v0, 4 # search right
jal bsearch
beq $a0, $t0, found # if = to int, print found
j notFound # not found
left:
move $a2, $v0 # search left of m
jal bsearch
beq $a0, $t0, found # if = to int, print found
j notFound # not found
#Print
found:
li $v0, 1 # system call code for print_int
move $a0, $s2 # integer to print (counter
syscall # print it
li $v0, 4 # system call code for print_str
la $a0, p4 # address of string to print
syscall # print the answer part 2
j bsearch # continue to search for more integers
notFound:
li $v0, 1 # system call code for print_int
move $a0, $s2 # integer to print (counter)
syscall # print it
li $v0, 4 # system call code for print_str
la $a0, p3 # address of string to print
syscall # print the answer part 2
j bsearch # continue to search for more integers
#END PROG
end:
li $v0, 10
syscall
当我尝试将单词加载到我的数组中时,我一直收到错误,“/../Proj.asm第58行中的错误:0x00400040处的运行时异常:存储地址未在字边界0x200200c2上对齐。
57) storeVal:
58) sw $t1, array($t0) # store value in array
59) addi $t0, $t0, 4 # inc index
60) j store # continue to add integers
第58行是sw $ t1,数组($ t0)之一。我尝试按照显示的方式设置t0(.main之后的行),并使用
将其设置为$ zeroaddi $t1, $zero, 0
这两个选项都不允许我将一个项目设置到数组中,我找不到指南或教程来挽救我的生命......当我们在这里时,任何人都可以看到代码的其余部分是至少关闭?谢谢!