So I'm trying things out with assembly and I have a problem that involves counting set bits in a register. I have the code below but I can't seem to figure out how I'll stop this loop cause it goes on and on shifting the bits and ends up with an invalid count.
addi x9, x0, 0xff00
addi x10, x0, 1 # adding counter for AND function
addi x11, x0, 0 # bit counter count
addi x12, x0, 0 # to compare 0
and x13, x9, x10
#bge x10, x9, func_c # this somehow works with positive numbers only
bne x13, x12,func_a # go to func_a if not equal to 0
beq x13, x12, func_b # else go to funcb_b
func_a:
addi x11, x11, 1 # add one to bit count
slli x10, x10, 1 # shift left
and x13, x9, x10 # new x13
bne x13, x12, func_a # not equal to 0
beq x13, x12, func_b
func_b:
slli x10, x10, 1 # shift left
and x13, x9, x10
#jal x0, -40 # go to line 7 # this jumped to the commented `bge` instruction above
jal x0, -36 # go to line 8
func_c:
add x20, x0, x11 # store final count