Counting 1's in 16 bit word

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 8
Reputation: laguna92651 is an unknown quantity at this point 
Solved Threads: 0
laguna92651 laguna92651 is offline Offline
Newbie Poster

Counting 1's in 16 bit word

 
0
  #1
Oct 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark
 
0
  #2
Oct 24th, 2009
I'm VERY VERY rusty on a 68HC12

  1. ldy #16 ;Loop 16 times
  2. Loop:
  3. asld ; Carry <- A <- B <- 0 MSB in carry
  4. psha
  5. ? ; a = Sum
  6. adda a,0 ; a=a+0+carry
  7. ? ; Sum = a
  8. pula
  9.  
  10. dey
  11. bne $Loop

Or branch around if carry is set and then increment inx with x=bit count
  1. ldy #16 ;Loop 16 times
  2. ldx #0
  3. Loop:
  4. asld ; Carry <- A <- B <- 0 MSB in carry
  5. bcc $L1 ; Jump if bit clear
  6.  
  7. inx ; 1 more bit
  8.  
  9. $L1:
  10. dey
  11. bne $Loop
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC