2

This is a homework assignment, I've written the whole program myself, run through it in the debugger, and everything plays out the way I mean it to EXCEPT for this line:

sw $t1, counter($a3)

The assignment is to convert this snippet of C code to MIPS

    for(i = 0; i < a; i++) {
       for(j = 0; j < b; j++) {
          C[2 * i] = i – j; } }

All the registers change values the way they should in my program except for $a3 - It never changes.

Changes: An array needed to be declared and "pointed to" by a register and a label can't be used for an offset in the manner I started with

EDIT: Here's the finished, working code

Finished

13
  • Why did you expect $a3 to change ? It should be the address of your array, and thus it should not change. Actually I don't understand what is your problem. Commented Feb 12, 2015 at 9:14
  • @ElderBug I'm trying to store the result of i - j in each index of $a3, so shouldn't I be seeing some value popping up in the $a3 register value space? I'm not sure how better to explain myself, would a screenshot of my mars compiler help? I can show you what I'm looking at Commented Feb 12, 2015 at 9:49
  • 1
    If you have the line sw $t1, x($a3) with x=8, the value of t1 will be stored at $a3[2] (3rd int of your array, pointed by $a3). If it is not the case, then the line is never executed. Commented Feb 12, 2015 at 10:49
  • 1
    I'm not sure about the MIPS syntax, but to declare it should be like myarray: .space 40 for 10 words. Then you have to load the address into $a3, with la $a3, myarray, if I'm not mistaken. Commented Feb 12, 2015 at 11:35
  • 1
    Actually, you are right on this point, I forgot that you use i-1 later. Anyway, your loops are still wrong. Try to translate the while, that's really straightforward. Just translate what the while do step by step. The only not direct point is that the condition is a "continue" condition, when you need an "exit" condition (but that's just the opposite). And translate one loop at a time, it doesn't even matter if you have duplicate labels. Commented Feb 12, 2015 at 20:51

1 Answer 1

3

Recap answer from the comments

Your $a3 register, is supposed to be loaded with the address of an array defined in the .data section.

One big problem with your code is how you constructed your loops. The best way is to translate your loops step by step, and one loop at a time. Also, remember that :

for( i = 0; i < a; i++ )
{
    loop_content;
}

Is equivalent to :

i = 0;
while( i < a )
{
    loop_content;
    i++;
}

Which is easier to translate in assembly. The condition just have to be negated, has you need an "exit" condition, and not a "continue" condition as in a while loop. Your code will be much clearer and easier to understand (and less error prone).

Your "out of range" error comes from here : sw $t1, counter($a3). Here counter is a label, and therefore an address. Thus counter($a3) is doing "$a3 (=0x10010008) + address of counter (=0x100100f8)", giving 0x20020100, which is clearly not what you want (and non-sense).

Oh, and in the sw $r, offset($a) MIPS instruction, offset MUST be a 16-bit CONSTANT. Here, you use a 32-bit address, but it's just that the assembler kindly translate sw $t1, counter($a3) to $x = $a3 + counter; sw $t1, 0($x), which is why you may see a sw with 0 as offset.

Sign up to request clarification or add additional context in comments.

1 Comment

You're the man, that makes sense and I have a much better understanding of what is happening. Will post revised, polished code later for reference, let me know if there is anything I can do to make it more efficient or "cleaner". Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.