MIPS搜索数组,实现堆栈

时间:2014-11-13 19:29:15

标签: arrays search assembly stack mips

所以这来自Britton的MIPS汇编语言编程一书的练习6.2。我需要使用具有10个预设整数的字节大小数组,提示系统要求用户输入要搜索的数字。如果找到该号码,则打印存储该号码的元素,如果未找到则打印另一条消息。我需要使用堆栈这样做,我相信我得到了我的错误。我在堆栈中几乎是积极的,但我已经尝试了所有我能想到的东西。任何建议都将非常感激。谢谢!到目前为止,这是我的代码:

已解决:代码已更新。



X:		.byte 	1, 27, 92, 46, 72, 8, 13, 93, 65, 112
N:		.word	10
srch:	.asciiz "Please enter a number to search for: "
posi:	.asciiz	"That numbers location is in element "
fals:	.asciiz	"That number is not in the array."
nxln:	.asciiz	"\n"

	.text

main:	        la	$s0, X
	la  	$s1, N
	
	la		$a0, srch
	li		$v0, 4
	syscall
	li		$v0, 5
	syscall
	move	        $t2, $v0
	
	addiu 	        $sp, $sp, -24 # stack prep
	sw		$s0, 0($sp)
	sb		$s1, 4($sp)
	sb		$t2, 8($sp)
	jal		search
	lb		$t2, 12($sp)
	lb		$t3, 16($sp)
	
	addiu	        $sp, $sp, 24
	
	la		$a0, posi
	li		$v0, 4
	syscall
	li		$v0, 1
	move	        $a0, $t2
	syscall
	
	li		$v0, 10
	syscall
	
search:	
	lw		$t5, 0($sp)	# get &X
	lb		$t8, 4($sp)	# get N
	lb		$t7, 8($sp)	# get V
	move	$t6, $t8
	addi	$t6, $t6, -1 	# $t6 = N - 1
loop:	
	lb		$t4, 0($t5)	# get a character (always get my error here)
	addi	        $t5, $t5, 1	# increment pointer
	beq		$t4, $t7, found
	addi	        $t6, $t6, -1	# decrement loop counter
	bgez	        $t6, loop
	li		$t4, -1
	sw 		$t4, 16($sp)
	b		exit
found:
	sub		$t6, $t6, $t8
	sb		$t6, 12($sp)
	jr		$ra
exit:	
	la		$a0, fals
	li		$v0, 4
	syscall
	li		$v0, 10
	syscall
		




0 个答案:

没有答案