Is my MIPS program correct?

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

  • it must have at least one cycle
  • he must keep the amount in register R12

And here 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 it correct?

+4
source share
4 answers

I just finished my MIPs build class and I have a suggestion for you: Do not use PC Spim!

I used PC Spim, Mars and Qemu, and the best for general coursework is Mars (Mips Assembler and Runtime Simulator) . The editor is good, it crashes a lot less, and it makes it easy to debug and set breakpoints. It is free, open source, and created by Missouri State University.

It comes as a .jar file, so you can run it on both Windows and Linux. alt text
[ Mars Mips Emulator ]

In the general case, a simple way to determine if a number is even or odd is AND (bitwise) 1 with a number, and if the result is 0, then the number is even.

However, since we want all the even numbers in the series, we can just loop and increase our number by 2, as in your published code.

When adding an immediate value, you should use the addi or addu statements, not the add ones. You also said that you want to put the result in the register $ r12, but this is not a valid MIP register. Check out the MIPs wikipedia link for a list of all registries: MIPS - Registration.

I changed your program correctly. It stores the final result in $ t1 and then prints the final result.

  .text .globl main main: li $t0, 0 # $t0 = loop counter li $t1, 0 # $t1 = sum of even numbers loop: addi $t0, $t0, 2 # generating even numbers in register $t0 add $t1, $t1, $t0 # compute the sum bne $t0, 100, loop # if t0 reached 100 then go to loop. li $v0, 4 la $a0, result syscall # print out "Sum = " li $v0, 1 move $a0, $t1 syscall # print out actual sum exit: li $v0, 10 # terminate program run and syscall # Exit .data result: .asciiz "Sum = " 

After starting on Mars, I get the following:

Amount = 2550
- program completed -

+9
source

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

0
source

You should be able to use SPIM yourself. In addition, the line "b endloop" is not needed, because if you do not answer back to the top of the loop, the program will "fall into" endloop.

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

0
source

The code looks fine. As cunwold said, "b endloop" is unnecessary, since the goal of the branch is the first branch (bne ...). However, there is one mistake.

You use the same instruction (add) in two different ways. Instruction manual

 add $t0,$t0,2 

it should be

 addiu $t0,$t0,2 

Since you are adding an intermediate, not two registers.

So there it is. I replaced the syscall part with the actual function return (the value is returned in the register $ v0).

Hope this 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 is.

 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# 
0
source

All Articles