This blog post presents an 8086 assembly program designed to count the number of 0s and 1s in a given 16-bit number. We’ll explore the bit manipulation techniques used and analyze the program’s execution flow.
DATA SEGMENT NO DW 5648H Z DW ? O DW ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX, DATA MOV DS, AX MOV AX, NO MOV BX, 00H MOV CX, 10H MOV DX, 00H UP: ROL AX,1 JC ONE INC BX JMP NXT ONE: INC DX NXT: DEC CX JNZ UP MOV Z, BX MOV O, DX INT 3 CODE ENDS END START
Binary may seem like a bunch of 0s and 1s, but in the right hands, it dances to the rhythm of amazing programs! 💻🎶 Let’s count those bits! 0️⃣1️⃣
Understanding the Code
This program utilizes bit manipulation to efficiently count 0s and 1s.
Data Segment:
- NO DW 5648H: Declares a 16-bit word variable NO (for “Number”), initialized to 5648H.
- Z DW ?: Reserves space for a 16-bit word variable Z to store the count of zeros.
- O DW ?: Reserves space for a 16-bit word variable O to store the count of ones.
Flowchart:

Code Segment:
- ASSUME CS:CODE, DS:DATA: Specifies segment registers.
- START: The program’s entry point.
- MOV AX, DATA; MOV DS, AX: Sets up the data segment.
- MOV AX, NO: Loads the input number (NO) into the AX register.
- MOV BX, 00H; MOV CX, 10H; MOV DX, 00H: Initializes counters: BX for zeros, CX for the loop (16 iterations for a 16-bit number), and DX for ones.
- UP: This is a loop label.
- ROL AX, 1: This instruction rotates the bits in AX to the left by one position. The least significant bit (LSB) is moved into the carry flag.
- JC ONE: This instruction (Jump if Carry) branches to the ONE label if the carry flag is set (meaning the LSB was a 1).
- INC BX: Increments the zero count (BX).
- JMP NXT: Jumps to the NXT label.
- ONE: This label is the target of the JC instruction.
- INC DX: Increments the one count (DX).
- NXT: This label is the target of the unconditional jump from UP.
- DEC CX: Decrements the loop counter.
- JNZ UP: Jumps back to the UP label if CX is not zero (loop continues).
- MOV Z, BX; MOV O, DX: Stores the final counts in Z and O.
- INT 3: Halts program execution for debugging.
High-Level Overview
- Initialization: Input number is loaded; counters are initialized; loop counter is set.
- Bit Iteration: The program iterates through each bit of the input number using ROL.
- Counting 0s and 1s: Based on the carry flag after ROL, the appropriate counter is incremented.
- Result Storage: The counts of 0s and 1s are stored in Z and O.
- Program Termination: Execution halts.
Output
C:\TASM>masm czo.asm Microsoft (R) Macro Assembler Version 5.00 Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved. Object filename [czo.OBJ]: Source listing [NUL.LST]: Cross-reference [NUL.CRF]: 50422 + 450234 Bytes symbol space free 0 Warning Errors 0 Severe Errors C:\TASM>link czo.obj Microsoft (R) Overlay Linker Version 3.60 Copyright (C) Microsoft Corp 1983-1987. All rights reserved. Run File [CZO.EXE]: List File [NUL.MAP]: Libraries [.LIB]: LINK : warning L4021: no stack segment C:\TASM>debug CZO.EXE -g AX=5648 BX=000A CX=0000 DX=0006 SP=0000 BP=0000 SI=0000 DI=0000 DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0025 NV UP EI PL ZR NA PE NC 0B98:0025 CC INT 3 -d 0b97:0000 0B97:0000 48 56 0A 00 06 00 00 00-00 00 00 00 00 00 00 00 HV.............. 0B97:0010 B8 97 0B 8E D8 A1 00 00-BB 00 00 B9 10 00 BA 00 ................ 0B97:0020 00 D1 C0 72 04 43 EB 02-90 42 49 75 F4 89 1E 02 ...r.C...BIu.... 0B97:0030 00 89 16 04 00 CC E8 77-63 83 C4 06 FF 36 24 21 .......wc....6$! 0B97:0040 B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90 ...P.G^...^..].. 0B97:0050 55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74 U.......^.&....t 0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^. 0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*[email protected]. -q
Understanding the Memory Dump
This is the memory dump starting from address 0B97:0000
, showing the contents of memory. Here is the breakdown:
0B97:0000 48 56 0A 00 06 00 00 00-00 00 00 00 00 00 00 00 HV…………..
In this memory dump:
- The first few bytes (
48 56 0A 00 06 00 00 00
) are the data segment where the program stores the values.48 56 0A 00
: These represent the value5648h
(the numberNO
in the program).06 00
: These represent the variableO
(ones count), which is6
(because there are 6 ones in5648h
).00 00
: These represent the variableZ
(zeroes count), which is10
(because there are 10 zeroes in5648h
).
- The rest of the dump represents the program’s machine code and data.
8086 assembly program to count number to count the number of 1’s from a string?