本文介绍了MIPS中的Strncpy行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的strncpy代码.从理论上讲,它应该可以工作,但是当我对其进行测试时,它就会产生垃圾.
Here's my code for strncpy. In theory it should work, but when I run tests on it it gives out garbage.
Arguments:
$a0 = pointer to destination array
$a1 = source string
$a2 = number of characters to copy
返回:目标数组
strncpy:
beqz $a2, out
lb $t0, 0($a1) #load byte
beqz $t0 out
subiu $a2, $a2, 1
sb $t0, 0($a0)
addiu $a0, $a0, 1
addiu $a1, $a1, 1
j strncpy
out:
lb $0 0($a0)
move $v0 $a0
jr $ra
推荐答案
复制目标数组的原始地址($ a0)并将其加载到$ a0-> $ v0的"out:"中. (在您的版本中,除了上面提到的问题之外,您总是会在最后一个插入符后面找到字符:)
coppy original address of destination array ($a0) and load it in "out:" in $a0->$v0. (in your version you would always get the char behind the last insert... in addition to the above mentioned problem in out:)
addi $a3 $a0 0
strncpy:
(...)
out:
move $a0 $a3
move $v0 $a0
这篇关于MIPS中的Strncpy行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!