MIPS代码,执行因错误而终止?

时间:2016-11-26 19:44:52

标签: c mips

我想创建一个包含6个元素的数组,通过for循环,用户每次都会输入元素的值,然后通过for循环将每个元素添加到4并打印出来。

这是它给我的代码“Go:执行因错误而终止。”

.text 

#Show the Hello Message :D

li $v0 , 4      #4 because it is a string , 1 if it is integer message , V0 is function register
la $a0 , Message    #add the Hello message in the reserved assembler register a0
syscall         #execute the V0 --->4 function with a0 parameter

#for loop to take the values
add $t0,$zero,$zero

For :
    slti $t1,$t0,24
    beq $t1,$zero,Exit

    #Display Prompt message
    li $v0,4
    la $a0, Prompt
    syscall

    #Get the iput
    li $v0,5    #5 for int input
    syscall 

    #Move the input from the the function to a register

    move $t2,$v0

    add $s0,$zero,$t2
    #save value to the array
    sw $s0,MyArray($t0)

    addi $t0,$t0,4

    j For

Exit:       # End for loop

    add $t0,$zero,$zero

    addi $t4,$zero,4
While:
    beq $t0,24,Exit2

    lw $t6, MyArray($t0)

    addi $t0,$t0, 4

    add $t6,$t6,$t4     
    add $t6,$t6,$t4
    add $t6,$t6,$t4
    add $t6,$t6,$t4


    li $v0 , 1
    move $a0 , $t6
    syscall 


    li $v0 , 4
    la $a0 , Space
    syscall 

    j While

Exit2:


    li $v0 , 4      #4 because it is a string , 1 if it is integer message , V0 is function register
    la $a0 , Message2   #add the End message in the reserved assembler register a0
    syscall         



.data #This for all the data for the program like variables in c++

Message : .asciiz "Hello world !\n" #Display this message on the simulator
MyArray : .space 24
Prompt : .asciiz "Enter the value\n "
Message2: .asciiz "End world !\n"
Space : .asciiz " , "

1 个答案:

答案 0 :(得分:0)

我不确定你在这里想要完成什么。您的代码基本上会为数组的每个元素添加16。

您收到的错误是由内存错误排列引起的。当你从MIPS32中4字节的内存中加载字时,地址需要被4整除。为了保证这一点,你需要在声明数据部分之前添加 .align 4 你的阵列。

.data #This for all the data for the program like variables in c++
   Message : .asciiz "Hello world !\n" #Display this message on the simulator
   .align 4
   MyArray : .space 24
   Prompt : .asciiz "Enter the value\n "
   Message2: .asciiz "End world !\n"
   Space : .asciiz " , "