I have this code snippet:

  for (i=0; i<=100; i++) {
           a[i] = b[i] + C ;
     }

I am trying to figure out what this would look like in MIPS assembly with these rules:

Assume that a and b are arrays of words and the base address of a is in $a0 and the base address of b is in $a1. Register $t0 holds the variable i and register $s0 the constant C. Write the code for MIPS. How many instructions are executed during the running of your code? How many memory data references will be made during execution?

I don't even know how to start on this.. Can someone help me figure this out? Im so stuck. Thanks

Dani AI

Generated

A compact, efficient solution is to walk the two word arrays with pointers (this follows 's hint). The version below assumes $a0 = base(a), $a1 = base(b), and $s0 contains the constant C. It does not use the loop index register $t0 (index-based code is possible but slower because of the multiply/shift or extra instructions).

addi $t3, $a1, 404      # end = b + 4*101
loop:
    lw   $t1, 0($a1)
    add  $t1, $t1, $s0
    sw   $t1, 0($a0)
    addi $a1, $a1, 4
    addi $a0, $a0, 4
    bne  $a1, $t3, loop

Explanation and counts (assumptions: 101 iterations for i = 0..100 inclusive; count only dynamic execution of the shown real MIPS instructions; branch-delay slots ignored; data memory references = lw/sw only). Pointer version: loop body = 6 instructions/iteration; plus 1 initialization instruction to compute end => total = 1 + 6101 = 607 instructions executed. Data memory references = 2 per iteration (one load, one store) => 2101 = 202 data references.

For comparison, a straightforward index-based loop that updates i each time and computes addresses (or shifts i by 2) typically costs about 8 instructions per iteration, giving roughly 1 + 8*101 = 809 instructions, while still performing the same 202 data references. Pseudo-instructions like ble or la will expand into real instructions, increasing counts, so use real instructions when doing exact accounting.

Notes: confirm $s0 actually holds C and that arrays are word-aligned. If this code runs inside a function that must preserve registers, save/restore any callee-saved registers used; $t1/$t3 are temporaries (caller-saved), so they can be used without saving in many calling conventions.

This looks an awful lot like an exam question, which makes me wonder if I should bother, but here goes.

Regarding the loop itself, do you know how to a) set a register's value to zero, b) compare two values for equality, and c) add one to a register and store the result in the same register? This is assuming a naive implementation; there are ways to do this that would be more efficient, but I figure I'll keep this simple.

Inside the loop, you have the bases of the two arrays given to you in the $a0 and $a1 registers. You will need to increment the registers on each pass of the loop, to walk through the arrays. You will also have to add the value in the second array at a given index to $s0, and store it in the offset value of $a0. This should be trivial, if you have done what has already been mentioned.

Oh BTW, does the instructor allow you to use psuedo-instructions such as la and move? That will make things much easier if you can.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.