## LSU EE 3755 Fall 2001 Homework 4 Problem 0
#

# The population count procedure (pop) below is supposed to set
# register v0 to the number of 1's in the binary representation of the
# contents of register a0.
#
# For example, if a0 -> 3 then after calling pop v0 -> 2.
#
# The population count is supposed to do this, but it doesn't.  Find
# and fix the problems.


 ## To run the code using the class account Emacs:
#
# 1: Load this text into an Emacs buffer from a class account.
#
# 2: Press [f9]
#    The SPIM simulator window should pop up.
#    If the buffer is modified but not saved you will be prompted to
#    save the buffer, after answering the window will pop up.
#
# 3: Click the Run button.
#    A Run Program dialog should pop up.
#
# 4: From the Run dialog click OK.
#    The dialog should disappear and the program should have ran.
#    If it ran "properly" (meaning with the pop bug) the window should
#    show the following values for s0, s1, and s2:
#
#     R16 (s0) = 00000005
#     R17 (s1) = 00000007
#     R18 (s2) = 00000009


 ## To single-step through the program:
#
# 1: Start SPIM (Steps 1 and 2 above)
#
# 2: Click the step button.
#    A Step Program dialog should pop up.
#
# 3: Click step.
#    The highlighted instruction in the Text Segments window should move
#    and register values should be up dated.
#
# 4: Continue clicking as needed.

################################################################################
## Problem 0

# The code below calls the buggy pop procedure with several
# different values.


        .globl __start
__start:

        # Find population of 0x655.
        #
        jal pop           # Call population routine.
                          # Return address put in $ra by jal instruction.
                          # Argument passed to routine placed in $a0 (below).
                          # Return value put in $v0 (by pop routine).

        # Instruction below is in branch delay slot and so it will
        # be executed BEFORE the first instruction of the pop routine,
        # NOT after it returns.
        #
        li $a0, 0x655     # Put call argument in a0
        add $s0, $v0, $0  # Move result in to s0.  Should be 6.

        # Find population of 0x111
        #
        jal pop
        li $a0, 0x111
        add $s1, $v0, $0  # Should be 3.

        # Find population of 0xfab4fab4
        #
        jal pop
        li $a0, 0xfab4fab4
        addi $s2, $v0, 0  # Should be 20 (0x14)

        # End by using "exit" system call, #10.
        addi $v0, $0, 10
        syscall

################################################################################
## Buggy Population Count Procedure
#

# The routine below does not work (or else its been fixed without changing
# this comment).

        .globl pop
pop:

        ## Register Usage
        #
        # $a0:  Input value, the integer to find the population of.
        # $v0:  Return value, the population of the integer.
        #
        # $t1:  A working copy of the integer.

        add $t1, $a0, $0
LOOP:
        srl $t1, $t1, 1
        andi $t0, $t1, 1
        bne $t1, $0, LOOP
        add $v0, $v0, $t0
        jal $ra
        nop