MIPS-读取整数错误

时间:2014-11-25 14:08:13

标签: arrays input integer mips

# Programming Project 2
# Noah Heath
# @02685972

.data # Data declaration
      # A+B -(C-D x E)

string1: .asciiz "Please enter an integer from range 0 to 32768: "
string2: .asciiz "Next integer: "
string3: .asciiz "Invalid input. Start over. "
userinput: .space 100
var6: .asciiz "The result of A+B -(C-D x E) is: "
.text

main:
    la $a0, string1 #load string one and print
    li $v0, 4
    syscall

    la $a1, userinput 
    li $t1, 5 #set temporary variable to 5
    li $t0, 0 #start of counter

input:  
    beq $t0, $t1, exit
    li $v0, 5 # read integer
    syscall
    blt $v0, $zero, input # if input is less than zero
    bgt $v0, 32768, input # if input is greater than 32768
    sw $v0, 0($a1)
    addiu $a1, $a1, 4

exit:
    la $t3, userinput # stores base address of user input array into $t3
    lw $t4, ($t3) # load first number
    lw $t5, 4($t3) # load second number
    lw $t6, 8($t3) # load third number
    lw $t7, 12($t3) # load fourth number
    lw $t8, 16($t3) # load fifth number
    add $s1, $t4, $t5 # adds 1 and 2 into $t0
    mult $t7, $t8 # multiplies 2 and 3
    mflo $s2 # retrieves from register
    sub $s3, $t6, $s2 # subtracts 7 from 6
    sub $s4, $s1, $s3 # subtracts 1 from 3
    move $a0, $s4 # moves result into a0

    li $v0, 1 # instruction to print result
    syscall # call operating system to perform operation

    li $v0, 10 # exit instruction
    syscall

我一直在试图找出我的代码没有读取用户整数和存储的原因。我确信一切都在语法上是正确的。如果我使用寄存器错误,有人可以帮助我理解吗?

QTSpim表示异常发生在PC = 0x00400060。

然后继续说商店中的未对齐地址= 0x1001005b。

它继续以4为增量(我假设是因为数组被索引的方式。

最终错误消息是     发生了异常5 [存储中的地址错误]并忽略了

Exception 4 [Address error in inst/data fetch] occurred and ignored(repeats 4 times)

1 个答案:

答案 0 :(得分:2)

要求用户输入的代码没有任何问题。

我想你的问题是你试图存储用户在非对齐地址(发出sw $v0, 0($a1)的行)中输入的内容。

您应在.align 2标签前添加指令userinput,例如:

string3: .asciiz "Invalid input. Start over. "
.align 2
userinput: .space 100