2016-11-26 17 views
0

我想创建一个由6个元素组成的数组,每次循环时用户将输入元素的值,然后通过循环将每个元素添加到4并打印出来。MIPS代码,执行因错误而终止?

这是它给我的代码“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 " , " 

回答

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 " , " 
+0

谢谢OldSurehand。 –