2013-10-01 3 views
0

의견이 너무 명확하지 않은 경우 이에 대한 설명을 드리겠습니다. 길이가 8 인 배열 두 개를 취해 각 해당 요소를 곱하고 곱을 새 배열에 저장합니다. 즉, array1 [1,2, ..., 8]과 array2 [1,2, ..., 8]에 대해 배열 3의 내용은 [1,4, ..., 64]와 같습니다.왜이 MIPS 어셈블리 언어 코드가 내 SPIM 시뮬레이터에 충돌입니까?

.data 
array1:  .byte 2, 2, 2, 2, 2, 2, 2, 2 
array2:  .byte 41, 3, 5, 7, 19, 2, 4, 4 
array3:  .space 40 #This must be declared this way 
result:  .asciiz "Product = " 
    .globl main 

    .text 
main: 
    la $s0, array1    #Load address of array1 to s0 
    la $s1, array2    #Load address of array2 to s1 
    la $s2, array3    #Load address of array3 to s2 
    lb $t0, 0($s0)    #Load first byte of array1 to t0 
    lb $t1, 0($s1)    #Load first byte of array2 to t1 
    li $t8, 8     #Load 8 to t0 for our loop. We'll call it N 

loop: 
    beq $t0, 0, next  #if the variable X is 0, go to next, else 
    add $t2, $t1, $t2  #add t2 and Y, store in t2 
    addi $t0, $t0, -1  #decrement X 
    j loop     #jump to loop 

next: 
    sw $t2, 0($s2)   #Store the product into array3 as a word. 
    li $t2, 0    #Load 0 to t2 to reset it before jumping back to loop 
    addi $t8, $t8, -1  #decrement t8 
    add $s0, $s0, 1  #Shift to next byte in array1 
    lb $t0, 0($s0)   #Load first byte of array1 to t0 
    add $s1, $s1, 1  #Shift to next byte in array2 
    lb $t1, 0($s1)   #Load first byte of array2 to t1 
    add $s2, $s2, 4  #Shift to next word in array3 
    bne $t8, 0, loop  #If t8 is not yet 0 (we haven't interated through all elements of the list), go to loop. 
    li $t8, 8    #Load 8 to t8 
    add $s2, $s2, -32  #Shift to first word of array3 
    j print1     #jump to print procedure 

print1: 
    li $v0, 4    #load system call code for print string 
    la $a0, result   #Load address of result to a0 
    syscall     #print result 
    j print2    #jump to print2 procedure 

print2: 
    li $v0, 1     #Load system call for print integer 
    lw $a0, 0($s2)    #Load first word of array3 to a0 
    syscall      #print Integer 
    bne $t8, 0, print2   #if t8 is not equal to 8, loop to print2 procedure 
    j close      #Else jump to close 

close: 
    li $v0, 10     #load system call code for terminate 
    syscall      #return control to system 
+0

실행합니다 벌금 - 작동하지만 잘 실행하지 않습니다. 어떻게 추락했다고 말했지만 오류 메시지를 제공하지 않았는지 고려하면 이것이 가장 많은 사람들이 말할 수 있습니다. –

+1

'print2' 루프에서'$ s2'를 진행하지 않고'$ t8'을 감소시키지 않습니다. 무한 루프가 발생하지만 충돌은 발생하지 않습니다. 그 외에는 분명히 잘못된 점을 발견하지 못했습니다. 업데이트 : 아,'$ t2'는 처음으로'loop'를 입력 할 때 제로화되지 않습니다. – Jester

+0

@Jester 네 말이 맞아. 8 시간의 수면이 명확성을 위해 무엇을 할 수 있는지 놀랍습니다. 귀하의 의견을 보내 주셔서 감사합니다. – WhyAyala

답변

0

또 다른 덜 복잡 솔루션 : 내 SPIM 시뮬레이터

.data 
    result: .asciiz "Product= " 
    space: .asciiz " " 
    .align 2  
    Array1: .word 1, 2, 3, 4, 5,6,7,8 
    Array2: .word 1, 2, 3, 4, 5,6,7,8 
    Array3: .space 32 
    N: .word 8 

    .text 
    main: 
     la $a0, Array1 
     la $a1, Array2 
     la $a2, Array3 
     lw $a3, N 
     jal mult_array1_array2 
     jal print_array3 
     jal close 

    mult_array1_array2: 
     li $t0, 0 
    buc1: bge $t0, $a3, final1 
     lw $t4, ($a0) 
     lw $t5, ($a1) 
     mul $t3, $t4, $t5 
     sw $t3, ($a2) 
     addi $a0, $a0, 4 
     addi $a1, $a1, 4 
     addi $a2, $a2, 4 
     addi $t0, $t0, 1 
     b buc1 
    final1: 
     jr $ra 

    print_array3: 
     li $v0, 4 
     la $a0, result 
     syscall 
     li $t0, 0 
     li $t1, 0 
    buc2: bge $t0, $a3, final2 
     lw $a0, Array3($t1) 
     li $v0, 1     
     syscall 
     la $a0, space 
     li $v0, 4     
     syscall 
     addi $t1, $t1, 4 
     addi $t0, $t0, 1 
     b buc2 
    final2:   
     jr $ra    

    close: 
     li $v0, 10 
     syscall