I am trying to count the number of 1's in a sixteen bit word for MC68HC12. The only way I can think of is to use 16 BITA instructions, one for each bit location, then increment a counter. I suspect there is a more elegant way to do this. Could someone share their ideas with me.

I'm VERY VERY rusty on a 68HC12

ldy   #16 ;Loop 16 times
Loop:
  asld            ; Carry <- A <- B <- 0           MSB in carry
  psha
  ?                 ; a = Sum
  adda a,0     ; a=a+0+carry
  ?                 ; Sum = a
  pula

  dey 
  bne  $Loop

Or branch around if carry is set and then increment inx with x=bit count

ldy   #16 ;Loop 16 times
  ldx   #0
Loop:
 asld            ; Carry <- A <- B <- 0           MSB in carry
  bcc $L1      ; Jump if bit clear

  inx             ; 1 more bit

$L1:
  dey 
  bne  $Loop
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.