-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcount_set_bits_in_16bit_binary.asm
More file actions
93 lines (79 loc) · 3.23 KB
/
count_set_bits_in_16bit_binary.asm
File metadata and controls
93 lines (79 loc) · 3.23 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
; =============================================================================
; TITLE: 16-bit Set Bit Counter (Population Count)
; DESCRIPTION: This program calculates the number of bits set to '1' in a 16-bit
; binary number. It demonstrates efficient bit manipulation
; using rotation and carry flag analysis.
; AUTHOR: Amey Thakur (https://github.com/Amey-Thakur)
; REPOSITORY: https://github.com/Amey-Thakur/8086-ASSEMBLY-LANGUAGE-PROGRAMS
; LICENSE: MIT License
; =============================================================================
.MODEL SMALL
.STACK 100H
; -----------------------------------------------------------------------------
; DATA SEGMENT
; -----------------------------------------------------------------------------
.DATA
VAL_TEST_DATA DW 0AAF0H
MSG_START DB 0DH, 0AH, "Counting set bits...", 0DH, 0AH, "$"
MSG_RESULT DB 0DH, 0AH, "Total set bits (Hamming Weight): $"
; -----------------------------------------------------------------------------
; CODE SEGMENT
; -----------------------------------------------------------------------------
.CODE
MAIN PROC
; --- Step 1: Initialize Data Segment ---
MOV AX, @DATA
MOV DS, AX
; --- Step 2: Print Introduction ---
LEA DX, MSG_START
MOV AH, 09H
INT 21H
; --- Step 3: Setup Registers for Counting ---
MOV AX, VAL_TEST_DATA
MOV BX, 0000H ; Bit-counter
MOV CX, 0010H ; 16 bits to check
; --- Step 4: Bit Rotation and Carry Analysis ---
L_COUNT_LOOP:
ROL AX, 1 ; Rotate MSB into Carry
JNC L_BIT_IS_ZERO
INC BX
L_BIT_IS_ZERO:
LOOP L_COUNT_LOOP
; --- Step 5: Display Result ---
LEA DX, MSG_RESULT
MOV AH, 09H
INT 21H
; ASCII conversion for single digit (0-9)
MOV DX, BX
ADD DL, '0'
MOV AH, 02H
INT 21H
; --- Step 6: Shutdown ---
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
; =============================================================================
; TECHNICAL NOTES & ARCHITECTURAL INSIGHTS
; =============================================================================
; 1. ROL vs SHL:
; - SHL (Shift Left) destroys the original number by filling with zeros.
; - ROL (Rotate Left) preserves the number; after 16 rotations, AX returns
; to its initial state.
;
; 2. THE CARRY FLAG (CF):
; Rotation and shift instructions are the primary way to interface bit-level
; data with hardware status flags in the 8086.
;
; 3. POPULATION COUNT (HAMMING WEIGHT):
; This algorithm calculates the "Hamming Weight," essential for
; error-correction codes and parity checks.
;
; 4. PERFORMANCE:
; ROL (immediate) takes 2 cycles, while LOOP takes 17 cycles. The
; branching logic adds variability based on bit distribution.
;
; 5. SCALABILITY:
; This pattern is easily adapted for 32-bit registers (EAX) on 386+
; processors by increasing the loop counter to 32.
; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =