Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help with counting set bits in each byte

[b][red]This message was edited by oldandtired at 2006-4-29 10:10:18[/red][/b][hr]
I'm trying to count how many set bits in each byte of "MSG", defined at the top. Could you please look at my code and tell me where I'm going wrong and explain please? The output I'm getting is 3. It seems to be reading only the first char. Thanks for your help. C.

DATA SEGMENT
;directive to define byte data
;string data definition
;20 unassigned bytes and prints out buffer when done
TITLE1 DB 'Hello world',13,10,'$'
BUFFER DB 20 DUP(?),13,10,'$'
MSG DB 054h, 0C8h, 045h, 020h, 04Ch, 0C1h, 0DAh, 0D9h, 020h, 0C2h

DATA ENDS
;*************
STACK SEGMENT STACK
;generates a repeated storage allocation, using constant expression as counter
DB 32 DUP ('STACK ')
STACK ENDS
;***********
CODE SEGMENT
; registers to hold 16-bit data
ASSUME CS:CODE,DS:DATA,SS:STACK ; Basic Program Execution Registers
PROG16 PROC FAR
***
***
***
*
LEA SI,MSG ;source index register
MOV AL,[SI]
; counts how many bits set
MOV CX,8 ;loop counter
XOR BL,BL ;zeroes out register
;
TOP:
;ROR shifts each bit to the right while the lowest bit is copied o the
;carry flag and into the highest bit at the same time
ROR AL,1
JNC PAST ;jump if carry bit is not set
INC BL ;if it is, increment BL by one bit set
INC SI
;
PAST:
LOOP TOP ;return to TOP and loop again
OR BL,30h ;convert binary BL to ASCII
MOV BUFFER,BL ;move Bl into buffer area to display


Sign In or Register to comment.