0

How do I calculate the sum of odd positive integers in MIPS? I have a MIPS simulator at home and I use to book to help verify my work. My university has a computer lab that has hardware provided by an outside company. The idea I suppose is that the University "pimps out" the hardware to students through the classes. Part of the problem for me is that I want to verify my code work properly, while using the board at school but it seems easier to verify the code works at home. Anyway, I think the code should read something like this:

andi $t8, $s0, 1  #value from $s0 and add 1 to it. Place in $t8 register
bnez $t8 #This should determine if its odd
beqz $79 #This should determine if its even
even:
  addi $t7, $t8, -1
  bnez $t7, odd
odd:
  addi $t6, $t7, -2
  Rt6, loop

Is there an easier way to do this? I need to write a main routine in which at the end of the execution perform v0=the sum of odd positive integers between 1 and 1000. $t8 is my $v0 in this case. Any helpful suggestions would be considered very closely.

Rahn
  • 4,787
  • 4
  • 31
  • 57
Jeremiah
  • 11
  • 2
  • 9
  • Which assembler are you using? A lot of the code makes no sense to me. What is `beqz $79` supposed to do? What about `Rt6, loop`? If `$t8` is supposed to hold the sum, why do you use it as the destination operand in `andi $t8, $s0, 1`? – Michael Sep 13 '16 at 07:43
  • Yeah, I don't admit that I know what I am doing. I use Qtspim at home. I am trying to figure out on how to write a code that adds up all positive integers from 1 through a 1000. – Jeremiah Sep 13 '16 at 15:24
  • `int sum = 0; for (int i = 1; i <= 1000; i++) if (i & 1) sum += i;` Something like that. Should be easy enough to transcribe to MIPS assembly by hand. The loop could be optimized to `for (int i = 1; i <= 1000; i+=2) sum += i;` – Michael Sep 13 '16 at 15:26
  • It's not easy for me. I got something like this: li $t0, 1000; li $t1,1; addi $t1, $t1, 2; (do this 500 times. idk how to write in MIPS.) Something along those lines, I suppose. I appreciate your input Michael. Having a conversation about this stuff is helpful. Right now, I can not see the forest through the trees. – Jeremiah Sep 13 '16 at 15:37
  • Completed! Thanks for your help ^^ – Jeremiah Sep 13 '16 at 20:37

1 Answers1

1

Here's some annotated code that does the sum of both odd and even values. It also has an example of a subroutine.

    .data
array:
    .word   17767, 9158, 39017, 18547
    .word   56401, 23807, 37962, 22764
    .word   7977, 31949, 22714, 55211
    .word   16882, 7931, 43491, 57670
    .word   124, 25282, 2132, 10232
    .word   8987, 59880, 52711, 17293
    .word   3958, 9562, 63790, 29283
    .word   49715, 55199, 50377, 1946
    .word   64358, 23858, 20493, 55223
    .word   47665, 58456, 12451, 55642
arrend:

msg_odd:    .asciiz     "The sum of the odd numbers is: "
msg_even:   .asciiz     "The sum of the even numbers is: "
msg_nl:     .asciiz     "\n"

    .text
    .globl  main
# main -- main program
#
# registers:
#   t0 -- even sum
#   t1 -- odd sum
#   t2 -- current array value
#   t3 -- isolation for even/odd bit
#   t6 -- array pointer
#   t7 -- array end pointer
main:
    li      $t0,0                   # zero out even sum
    li      $t1,0                   # zero out odd sum
    la      $t6,array               # address of array start
    la      $t7,arrend              # address of array end

main_loop:
    bge     $t6,$t7,main_done       # are we done? if yes, fly

    lw      $t2,0($t6)              # get value
    addiu   $t6,$t6,4               # point to next array element

    andi    $t3,$t2,1               # isolate LSB
    beqz    $t3,main_even           # is is even? if yes, fly

    add     $t1,$t1,$t2             # add to odd sum
    j       main_loop

main_even:
    add     $t0,$t0,$t2             # add to even sum
    j       main_loop

main_done:
    # output the even sum
    la      $a0,msg_even
    move    $a1,$t0
    jal     print

    # output the odd sum
    la      $a0,msg_odd
    move    $a1,$t1
    jal     print

    # terminate program
    li      $v0,10
    syscall

# print -- output a number
#
# arguments:
#   a0 -- pointer to message
#   a1 -- number to output
print:
    # output the message
    la      $v0,4
    syscall

    # output the number
    li      $v0,1
    move    $a0,$a1
    syscall

    # output a newline
    la      $a0,msg_nl
    li      $v0,4
    syscall

    jr      $ra                     # return

If you'd like some tips on writing clean asm, based on my own experience, see my answer: MIPS linked list

I've used spim, QtSpim, and mars for simulators. Personally, I prefer mars where possible. See: http://courses.missouristate.edu/KenVollmar/mars/

Community
  • 1
  • 1
Craig Estey
  • 30,627
  • 4
  • 24
  • 48