# MIPS even or odd detection program

I am learning MIPS as a part of my Computer Organization class at school and I am writing a simple program that reads in a positive integer from the user and tells the user whether the number is even or odd. The program works, but the way I had to split the loop and conditionals into different labels concerns me a little bit.

I could not figure out another way to do it, but as loops and conditionals are very common in code, I want to see if there is a way to do it without creating so many labels. If and when I tackle a bigger project in MIPS, I do not want to have to have 10 different loop labels if there is a better way to do it.

.data
prompt: .asciiz "\nEnter a integer: "
even_msg: .asciiz "\nThe number is even.\n"
odd_msg: .asciiz "\nThe number is odd.\n"
goodbye: .asciiz "\nGoodbye!\n"
.text

main:
j loop

loop:
la $a0, prompt li$v0, 4
syscall

li $v0, 5 syscall # If number is negative, exit program. slt$t0, $v0,$zero
bne $t0,$zero, exit

# Call even_or_odd on value
add $a0,$v0, $zero jal even_or_odd j loop even_or_odd: addi$t0, $zero, 2 # Set divisor to 2 div$a0, $t0 mfhi$t0           # Save remainder
beq $t0,$zero, even
j odd

even:
la $a0, even_msg li$v0, 4
syscall
j return
odd:
la $a0, odd_msg li$v0, 4
syscall
j return

return:
lw $t0, 0($sp)
addi $sp,$sp, 4
jr $ra exit: la$a0, goodbye
li $v0, 4 syscall li$v0, 10
syscall

• If you look at the binary representations of the numbers, you'll notice a pattern that holds for all even and odd numbers that makes parity checking very simple. – kamoroso94 Feb 4 '18 at 21:01