Assembly Program to Count the Number of 1 Bits in BX
This program will calculate the number of 1 bit in BX without changing BX. The basic logic of the program is it copies the BX into AX and takes the AND of 1 with AX if the result is 1 then there is 1 at LSB and if it's 0 then there is 0 at LSB. If it's one then we count it otherwise not. Also, rotate BX by one to check for the next bit. This will loop 16 times.
In this program, the value I have moved in BX is 0EF0h which means 0000110111110000b so this has 7 ones so the program will output 7 and If you change BX the output will be different according to your number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | .MODEL SMALL .STACK 100H .DATA .CODE MAIN PROC MOV DX, 0 MOV BX, 0EF0H MOV CX, 16 LABEL: DEC CX MOV AX, BX AND AX, 1 ROL BX, 1 CMP AX, 0 JG INCREMENT CMP CX, 0 JG LABEL JMP END INCREMENT: INC DX CMP CX, 0 JG LABEL END: ADD DX, 48 MOV AH, 2 INT 21H MAIN ENDP END MAIN |
No comments