Results 1 to 7 of 7

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

  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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: adding even or odd numbers in array, MIPS32

    Can't help you with your exact problem, but generally I would do it like this: (pseudo-assembly)
    Code:
    set $r1 if $userinput eq 0 (is there a seq instruction?)
    loop:
      load next num into $r2
      div $r2 by 2, remainder into $r3
      jump to loop if $r3 eq $r1 (in other words, if the remainder is 1 and the user entered
                                0, skip the counting step. Same if rem is 0 and ui is != 0)
      add $r2 to $total
      jump to loop unconditionally
    You still need loop termination in this, of course.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

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

    Re: adding even or odd numbers in array, MIPS32

    Thanks for the reply, this is how I ended up doing it:
    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, $0, 5
    	syscall
    	move $t0, $2
    
    	la $t2, item			#load base address of item array into $t2
    	beq $0, $t0, even		#if user inputed 0, go to even loop
    oloop:	lw $t3, ($t2)		#load item[i] into $t3
    	add $t1, $t1, $t3		#add t1 = t1+t3
    	beq $t3, 9, done		#if t3 == 9, jump to done
    	addi $t2, $t2, 8		#t2 = t2 + 8, jump to next odd word, which is 8 positons away 
    	j oloop
    even:	addi $t2, $t2, 4
    eloop:	lw $t3, ($t2)
    	add $t1, $t1, $t3
    	beq $t3, 10, done
    	addi $t2, $t2, 8
    	j eloop
    
    done: 	ori $2, $0, 4
    	la $4, str
    	syscall
    	ori $2, $0, 1
    	add $4, $t1, $0
    	syscall
    	ori $2, $0, 4
    	la $4, cr
    	syscall
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

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

    Ah, but of course that works only for this particular sequence. What about a random sequence of numbers?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

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

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

    hah i know i took the easy way out, if i kept working at it, i think i would have gotten it. But i asked the professor and she said don't mess
    div or $HI register yet, it was an exercise in understanding that words are 4 bits long, not 1.

    So if you want to go to the next memory location, you must jump +4 and not +1 or else you'll end up just moving 1 bit over. If i had more time I would work on it.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

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

    bytes, not bits
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

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

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

    oops, that would be bad if i wrote that on the exam, thanks!
    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