views:

1438

answers:

4

Write a MIPS program that generates and adds up all even numbers from 1 to 100.

  • it must have at least one loop
  • it should store the sum in register R12


And this is what I wrote:
main:
    li      $t0, 0               # clear register $t0 to zero
    li      $t4, 0               # clear register $t4 to zero
loop:
    add     $t0, $t0, 2          # generating even numbers in register $t0
    add     $t4, $t4, $t0        #  compute the sume
    bne     $t0, 100, loop       # if t0 reached 100 then go to loop.
    b endloop                    # branch to endloop
endloop:
    li      $v0, 10              # terminate program run and
    syscall                      # Exit

Is this correct?

A: 

Try this emulator. When I took Computer Organization, I used SPIM and it was fairly easy to use. You can also find tutorials on MIPS online. Remember, Google is your friend.

tundal45
please could you help me to run and see if i got it correct..thanks
but is it suppose to add even numbers from 1 to 100?
A: 

You should be able to use SPIM yourself. Also the line "b endloop" is unnecessary because if you don't branch back up to the top of loop the program will "fall into" endloop.

Download SPIM here:
http://pages.cs.wisc.edu/~larus/spim.html

Corey Sunwold
+3  A: 
Simucal
but is it suppose to add even numbers from 1 to 100?
@kenny9999999, Yes, it is.
Simucal
THanks...very much it did work.
@Simucal, I dont think they mean $r12 by R12, but general purpose register 12, which is $t4.
Tom
A: 

Code looks ok. As cunwold said, the "b endloop" is unnecesary, since the branch target is the first branch (bne...) fallthrough. There is one mistake though.

You are using the same instruction (add) in two different ways. The instruction

add $t0,$t0,2

should be

addiu $t0,$t0,2

Since you are adding an inmediate, not two registers.

So, here it goes. I replaced the syscall part with an actual return to a function (value returns in $v0 register).

Hope it helps.

File main.c

#include <stdio.h>

extern int addEven();

int main(){


        printf("%d\n",addEven());
        return 0;
}

File addEven.S (assembly)

#include <mips/regdef.h>

  /*
   * int addEven();
   * Adds even numbers between 0 and 100.
   * 0 + 2 + 4 + 6 +....+100 = 2550
   */

        .text
        .align 2
        .globl addEven

addEven:
        li      t0,0            # clear register $t0 to zero
        li      t4,0            # clear register $t4 to zero
loop:
        addiu   t0, t0,2          # generating even numbers in register $t0
        add     t4, t4,t0          #  compute the sume (R12 = t4)
        bne     t0, 100, loop      # if t0 reached 100 then go to loop.
endloop:
        move    v0,t4
        jr      ra

I compiled and linked these files. Here it goes.

root@:~/stackoverflow# gcc -c -g addEven.S
root@:~/stackoverflow# gcc -c -g main.c
root@:~/stackoverflow# gcc main.o addEven.o -o prog 
root@:~/stackoverflow# ./prog 
2550
root@:~/stackoverflow#
Tom
Do you use MIPs often in this way? If so, what do you use it for? I'm curious because I like MIPs but don't see how I would use it very often.
Simucal
I use it quite often, but only for educational purposes. I'm trying to get some boards to take it one step beyond.
Tom