【发布时间】:2012-03-31 23:56:39
【问题描述】:
我无法完全弄清楚我在汇编代码中做错了什么。我正在尝试编写一个程序来比较两个以 $a0 和 $a1 形式输入的以空字符结尾的字符串,在“main:”部分的某个时间,然后调用
jal hamming
启动程序。
基本上,对于本节,我希望将两个字符串逐个字符地进行比较,直到一个字符串到达空终止字符为止。然后程序停止并返回有多少字符不同,直到终止。
我认为这与我使用的跳跃有关,但我不太确定。该程序有点长,所以我选择了我认为是问题的主要部分(因此忽略 $a3 等已经初始化和定义的变量):
diffchar:
li $t4, 0
li $t5, 1
beq $a0, $a1, samechars
move $v0, $t5
j diffcharend
samechars:
move $v0, $t4
diffcharend:
jr $ra
hamming:
absvaluedone:
li $a2, 0
#li $v0, 0
move $t0, $a0
move $t1, $a1
hammingloopbegin:
lb $t2, 0($t0)
lb $t3, 0($t1)
beq $t2, $0, hammingdone
beq $t3, $0, hammingdone
la $a0, 0($t0)
la $a1, 0($t1)
jal diffchar **#this is the line that causes me problems, if I take this out it is fine**
beq $v0, $0, next
addiu $a2, $a2, 1
next:
addiu $t0, $t0, 1
addiu $t1, $t1, 1
j hammingloopbegin
hammingdone:
add $v0, $a2, $a3
jr $ra
当我运行我的程序时,我的输出看起来像一个无限循环,一直在说:
Exception occurred at PC=0x00400144
Bad address in data/stack read: 0x10021226
Exception 7 [Bad address in data/stack read] occurred and ignored
Exception occurred at PC=0x00400140
Bad address in data/stack read: 0x1002121b
Exception 7 [Bad address in data/stack read] occurred and ignored
我认为 diffchar 或我用来跳转到 diffchar 的过程有问题。这是我第一次编写汇编代码,所以我认为我错过了一些非常基本的东西,那就是搞砸了。任何指针都会很棒。
感谢您的帮助
【问题讨论】: