  # Name: Liam Kirsh, Kevin Yang
  # Section: 1
  # Description: If a number contains an even number of one bits, your function
  # should print out a 1; otherwise it should print out a 0.
  
  # #include <stdio.h>

  # void printParity(int num) {
  #    int res = 1;
   
  #    while (num) {
  #       res ^= num & 1;
  #       num >>= 1;
  #    }

  #    printf("Parity: %d", res);
  # }

  # int main() {
  #    int num;
  #    printf("Please enter a number: ");
  #    scanf(" %d", &num);
  #    printParity(num); 
  # }

  
# declare global so programmer can see actual addresses.
.globl prompt_num
.globl parityText

#  Data Area (this area contains strings to be displayed during the program)
.data

prompt_num:
	.asciiz " Please enter a number: "

parityText: 
	.asciiz " \n Parity = "

#Text Area (i.e. instructions)
.text

main:
	# printf(" Please enter a number: ");
	ori     $v0, $0, 4
	lui     $a0, 0x1001
	syscall

	# scanf(" %d", &num);
	ori     $v0, $0, 5
	syscall
    # $v0 now has the value of the first integer

	# Clear $s1 for num storage
	ori     $s1, $0, 0

	# Add integer to s1
	addu    $s1, $v0, $s1
    
    # int res = 1
    addi    $s0, $0, 1 # initialize to 1
    
loop:
    # while (num)
    beqz    $s1, done
    
    # res ^= num & 1;
    andi    $s2, $s1, 1 # store num & 1 in temporary register
    xor     $s0, $s0, $s2
    
    # num >>= 1
    srl     $s1, $s1, 1
    
    # unconditional branch to loop condition
    b loop
    
done:
	# printf("Parity = %d\n", res);
	ori     $v0, $0, 4			
	lui     $a0, 0x1001
	ori     $a0, $a0, 0x19 # 0x1001 += decimal 25
	syscall
	
	# Display the parity
	ori     $v0, $0, 1	# load 1 into $v0 to display an integer		
	add 	$a0, $s0, $0
	syscall
	
	# Exit (load 10 into $v0)
	ori     $v0, $0, 10
	syscall
