装配 - 在运行时打印错误的行

时间:2014-02-27 17:14:12

标签: assembly runtime-error mips

这是我的第二个MIPS组装计划。我最常翻译Java代码,所以我意识到这可能看起来有些混乱,我为此道歉,我只是想获得组装的直觉。并且赞赏输出错误的原因。 :)代码是第一个,然后输出:

    .text
# Load variables s=$t2, m=$t3, i=$t4, j=$t5 (second loop i), $t7=for mult 2 in first loop
li $t2,4
li $t3,1
li $t4,1
li $t5,1
li $t7,2

# User enters positive int > 1
la $a0,prompt
li $v0,4
syscall

# Put input into $t1 (p)
li $v0,5
syscall
move $t1,$v0

# $t6 is (q) or p-2 for second loop
la $t6,($t1)
subi $t6,$t6,2

firstloop:
# (i=1; i<=p; i++) m *= 2
mult $t3,$t7
mflo $t3
addi $t4,$t4,1
bgt $t4,$t1,subm
b firstloop

subm:
# Subtract 1 from m, move on to second loop
subi $t3,$t3,1
b secondloop

secondloop:
# (i=1; i<=p-2; i++)...
mult $t2,$t2
mflo $t2
subi $t2,$t2,2
div $t2,$t3
mfhi $t2
addi $t5,$t5,1
bgt $t5,$t6,printline
b secondloop

isPrime:
la $a0,print4 # Is a...
li $v0,4
syscall
b end

isNotPrime:
la $a0,print3 # Is not a...
li $v0,4
syscall
b end

printline:
la $a0,print1 # 2^
li $v0,4
syscall
la $a0,($t1) # p
li $v0,4
syscall
la $a0,print2 # -1=
li $v0,4
syscall
la $a0,($t3) # m
li $v0,4
syscall
beqz $t2,isPrime
b isNotPrime

end:
# End program
li $v0,10
syscall

.data
prompt:
.asciiz "Enter an odd postive integer greater than 1: "
print1:
.asciiz "2^"
print2:
.asciiz "-1="
print3:
.asciiz " is not a Mersenne prime. :("
print4:
.asciiz " is a Mersenne prime. :)"

输出:

Go: running lltest.asm

Error in G:\School\Computer Organization\lltest.asm line 70: Runtime exception at 0x004000c8: address out of range 0x00000003

Go: execution terminated with errors.

1 个答案:

答案 0 :(得分:3)

la $v0,print4 # Is a...
li $v0,4

字符串指针应该进入$a0,而不是$v0(代码中有多个这样的错误)。