Homework MIPS. Can't seem to find the error

Ok, the question is this: with MARS, create a program that:

  • Prompts the user to enter an integer,
  • Reads an integer from the keyboard (say n),
  • Calculates and displays the sum of odd numbers and the sum of even numbers from 1 to n.

Here is my attempt, please DO NOT SOLVE MY TOMORROW. I would like to learn from my mistakes. My question is where I am wrong, the registers seem to go to -MAXINT run it and you will see :(

    # Assignment 3, part 1
    # NAME  
    #The Formulas used in this assignments are visible in a text document 
    #attached in the zip. and their mathematical proofs. 
        # Odd or Even Checker by parity bit in binary format.
        # by anding it with the number N
        # if it is odd, the anding will result in 00000001
        # if it is even the ANDing will result in all 0
        # we can check if the result is ==0 or not
        # then use mathematical formulas to derive the sums
        # using the least number of instructions    
        # Without using the not-yet-learnt instructions.


----------------------------------------------- --- -----------------------

.data       #Data Declaration Section
    msg1: .asciiz "Hello, Enter an Integer:\n->"
    msgodd: .asciiz "Odd Sum is :\n"
    msgeven: .asciiz "Even Sum is :\n"  

    la $a0, msg1    #Load the address of label msg1
    li $v0,4    #Load int 4 to v0, and address the system call. 

   #Request a Syscall for integer input
    li $v0,5    #Load int 5 <int inp>

    move $a0,$v0    #copy the int entered
    move $t0,$a0    #will be used to test for odd or even
    move $a3,$a0    #will be used later
    andi $t0,00000001 # this is the parity bit i talked about above.
    beq $t0,$zero,even_n
    #s1 hold odd sum, s2 hold even sum, t1 is temp for odd, t2 is temp for even
    li $s5,1
move $t1,$a3
move $t2,$a3
addi $t2,$t2,-1

    add $s1,$s1,$t1
    addi $t1,$t1,-2
    bne $t1,$s5,ol

    add $s2,$s2,$t2
    addi $t2,$t2,-2
    bne $t2,$zero,el

move $t1,$a3
move $t2,$a3
addi $t1,$t1,-1

    add $s1,$s1,$t1
    addi $t1,$t1,-2
    bne $t1,$s5,eol

    add $s2,$s2,$t2
    addi $t2,$t2,-2
    bne $t2,$zero,eel

    li $v0,4
    la $a0,msgodd
    li $v0,1
    move $a0,$s1

    la $a0,msgeven
    li $v0,1
    move $a0,$s2

    li $v0,10



source to share

1 answer

There are a few bugs in the code, but after applying these changes, everything should be fine.

  • You initialize $s5

    after branching, which leaves $s5

    uninitialized when you have a starting number.

  • When the starting number is odd and your program finishes processing it, control flow drops to the even case unconditionally. You need to jump to the output of your program after completing odd_n

    your calculations.

  • You need to load 4

    in $v0

    before you render syscall

    for display msgeven

    (and while you're at it, consider fixing the placement of the newline in both posts).



All Articles