Results 1 to 7 of 7

Thread: [Resolved]adding even or odd numbers in array, MIPS32

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    [Resolved]adding even or odd numbers in array, MIPS32

    Hello everyone. I go this program to add up all the elements in the array and dispaly its value, but now i'm trying to add up only even numbers if the user enters 0. If they don't enter 0, then it will add up all the odd and dispaly the sum. Store result in register $t1.

    Here is the code i have to add up all the elements in the array:
    PHP Code:
    .data
    .align 2
    item
    : .word 1
    .word 2
    .word 3
    .word 4
    .word 5
    .word 6
    .word 7
    .word 8
    .word 9
    .word 10
    .align 0
    str
    : .asciiz "The answer is "
    cr: .asciiz "\n"
    .text
    .align 2
    .globl main
    main
    ori $2, $05
    syscall
    move $t0
    , $2


    li $t2
    0            #i=0

    loopsum:
        
    bge $t210done    #while (i < 10)
        
    mul $t3$t24        #4*i
        
    la $t4item        #calculate &item[i] address
        
    add $t3$t3$t4
        lw $t5
    0($t3)        #load item[i] into $t5
        

        
    add $t1,$t1,$t5        #$t1 = $t1 + item[i]
        
    add $t2$t21        #i++
    b    loopsum




    done
    ori $2, $04
    la 
    $4str
    syscall
    ori 
    $2, $01
    add 
    $4$t1, $0
    syscall
    ori 
    $2, $04
    la 
    $4cr
    syscall 
    This will print The answer is: 55.

    Now i'm having alot of trouble on how i can implement this in asm. What i want is:

    if(userinput == 0)
    loop through array if(item[i] % 2 == 0) { $t1 = $t1 + item[i] };
    else
    loop through array if(item[i] % 2 != 0) { $t1 = $t1 + item[i] };

    So if the user inputs 0, that means sum up all the even numbers in the array
    else
    sum up all the odd numbers in the array.

    Will this work to check to see if the remainder is 0? (if it is 0, then u konw its an even number.)

    PHP Code:
    lw $t50($t3)        #load item[i] into $t5
        
        
    ori $t6, $0$t5    #t6 = $t5
        
    div $t6,2        #$HI = $s % $t    
    beq $HI,0,loopeven    # if ($t6 % 2) == 0, even number 

    THanks!
    Last edited by voidflux; Sep 28th, 2006 at 10:23 AM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width