How do I calculate the sum of odd positive integers in MIPS? -


how calculate sum of odd positive integers in mips? have mips simulator @ home , use book verify work. university has computer lab has hardware provided outside company. idea suppose university "pimps out" hardware students through classes. part of problem me want verify code work properly, while using board @ school seems easier verify code works @ home. anyway, think code should read this:

andi $t8, $s0, 1  #value $s0 , add 1 it. place in $t8 register bnez $t8 #this should determine if odd beqz $79 #this should determine if even:   addi $t7, $t8, -1   bnez $t7, odd odd:   addi $t6, $t7, -2   rt6, loop 

is there easier way this? need write main routine in @ end of execution perform v0=the sum of odd positive integers between 1 , 1000. $t8 $v0 in case. helpful suggestions considered closely.

here's annotated code sum of both odd and values. has example of 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 odd numbers is: " msg_even:   .asciiz     "the sum of numbers is: " msg_nl:     .asciiz     "\n"      .text     .globl  main # main -- main program # # registers: #   t0 -- sum #   t1 -- odd sum #   t2 -- current array value #   t3 -- isolation even/odd bit #   t6 -- array pointer #   t7 -- array end pointer main:     li      $t0,0                   # 0 out sum     li      $t1,0                   # 0 out odd sum     la      $t6,array               # address of array start     la      $t7,arrend              # address of array end  main_loop:     bge     $t6,$t7,main_done       # done? if yes, fly      lw      $t2,0($t6)              # value     addiu   $t6,$t6,4               # point next array element      andi    $t3,$t2,1               # isolate lsb     beqz    $t3,main_even           # is even? if yes, fly      add     $t1,$t1,$t2             # add odd sum     j       main_loop  main_even:     add     $t0,$t0,$t2             # add sum     j       main_loop  main_done:     # output sum     la      $a0,msg_even     move    $a1,$t0     jal     print      # output odd sum     la      $a0,msg_odd     move    $a1,$t1     jal     print      # terminate program     li      $v0,10     syscall  # print -- output number # # arguments: #   a0 -- pointer message #   a1 -- number output print:     # output message     la      $v0,4     syscall      # output number     li      $v0,1     move    $a0,$a1     syscall      # output newline     la      $a0,msg_nl     li      $v0,4     syscall      jr      $ra                     # return 

if you'd tips on writing clean asm, based on own experience, see answer: mips linked list

i've used spim, qtspim, , mars simulators. personally, prefer mars possible. see: http://courses.missouristate.edu/kenvollmar/mars/


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -