I am working on a program that takes an integer from the user and then outputs how many 1's there are in it's binary equivalent. So first I believe I need to convert it to binary and then use a loop and check all 32 bits to find how many 1's there are.
I have been browsing around for hours and trying different things to first convert the integer to binary. What's the best way to do this? Is there a way to to read a register value in binary directly, or do I need to convert it first? This is all the code I have so far.
.data
EnterInteger: .asciiz "Enter an integer: "
.text
# Print the first message
li $v0, 4
la $a0, EnterInteger
syscall
# Prompt the user to enter the first integer
li $v0, 5
syscall
# Store the first integer in $t0
move $t0, $v0
Here is the code I have so far but it's not working. When I input 4673 I should get "4", instead I only get "1"
.data
Msg: .asciiz "Enter an integer: "
.text
# Print the first message
li $v0, 4
la $a0, Msg
syscall
# Prompt the user to enter the first integer
li $v0, 5
syscall
# Store the first integer in $t0
move $t0, $v0
addi $t3, $zero, 0
main:
bgt $t3, 32, exit
andi $t0, $v0, 1
bne $t0, $zero, count
count:
addi $t3, $t3, 1
addi, $t1, $zero, 1
# Shift to the next bit and then go back to main
srl $t0, $t0, 1
j main
exit:
# Tell the interpreter to get read to print an integer
li $v0, 1
add $a0, $zero, $t1
#Print the integer
syscall
# End the program
li $v0, 10
syscall