views:

975

answers:

2

I'm working on some MIPS code for my Computer Organizations class, and well I just can't seem to get the MIPS to work correctly and there's not that many MIPS resources online. I'm running the code on PCSPIM. The code is supposed to add 10 to the contents of array2 and store them in array1 and then print array 1. Reworked the code works properly now.

.text
main:
 la $t0, array1
 la $t1, array2
 la $s0, valuec
 li $s2, 6
 add $t6, $zero, 1 #i = 1

Loop:
 addi $t6, $t6, 1 #i++

 lw $t2, ($t0)
 lw $t5, ($t1)
 lw  $s1, ($s0)
 addu $t2, $t5, $s1
 sw $t2, ($t0)
 add $t0, $t0, 4
 add $t1, $t1, 4
 li $v0, 1
 move $a0, $t2
 syscall
 blt $t6, $s2, Loop

 li $v0, 10
 syscall
.data
 array1: .space 20
 array2: .word 1,2,3,5,9
 valuec: .word 10

PCSPIM prints 0 5 times and returns Exception 7 [Bad data Address] occured and ignored

A: 

There's a number of possible issues, but the most obvious/likely is that you aren't returning from main before the start of your data section.

jr ra
HUAGHAGUAH
I think that's handled by the final syscall with the command code $v0 set to 10.
paxdiablo
+2  A: 

This is homework so I'm only going to give you clues for now and add to it as you go. A couple of things:

1/ You need to tell us what it's supposed to do. That's the most important thing.

2/ You store array1 address into t0 then reuse t0 within the first loop.

3/ You appear to be confused about addresses and the contents of those addresses ("la $s0, valuec" and "addu $t0, $t1, $s0").

UPDATE:

Actually I have to sign off for a while, so I'll post my solution so as to not leave you in the lurch.

The confusion I referred to before was the fact that you're loading up two addresses into $t1 and $s0, then adding them together to get another address - this is likely to be well beyond your data area (you should really be adding an address and an offset).

That's basically the problem you have with your code (both the zeros being printed and the crash). Your best bet would be to fix that and refer to my code below only as a last resort to see how I would have done it. Copying code will not help you in the long term and you would be wise to assume your educator is also checking all web sites for plagiarism.

This is the code I've come up with (quickly, so you'll need to test it - it may have bugs). I suggest you read the comments in great detail to understand what it's doing.

I'll be back in a few hours to see how you're doing. Cheers.

.text
main:

# Initialization of array pointers and loop

    la      $t0, array1       # address of array 1
    la      $t1, array2       # address of array 2
    li      $t2, 1            # element number
    li      $t3, 6            # upper limit of elements

# Process each word in array 2, adding 10 and placing
# into array 1.

Loop:
    lw      $t3, 0($t1)       # get word from array 2
    addi    $t3, $t3, 10      # add 10 to word
    sw      $t3, 0($t0)       # store word into array 1
    addi    $t0, $t0, 4       # move to next entry in array 1 and 2
    addi    $t1, $t1, 4
    addi    $t2, $t2, 1       # increment element number
    blt     $t2, $t3, Loop    # loop until all elements done

# Initialize printing loop by going back to start of array 1

    la      $t0, array1       # address of array 1
    li      $t2, 1            # element number

# Loop through array 1, printing each element.

pLoop:  
    lw      $t2, 0($t0)       # get word from array 1
    li      $v0, 1            # 'print' command code
    move    $a0, $t2          # needs value in $a0
    syscall                   # print it
    addi    $t0, $t0, 4       # move to next entry in array 1
    addi    $t2, $t2, 1       # increment element number
    blt     $t2, $t3, Loop    # loop until all elements done

    li      $v0, 10           # 'terminate' command code
    syscall                   # exit

# Data arrays for array 1 and 2

.data
array1: .word 0,0,0,0,0
array2: .word 1,2,3,4,5
paxdiablo