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
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.