# Assembly Language Program to Get Hex Number and Display Count of Bits Set Assignment Solution.

## Instructions

Objective
Write a program to get hex number and display count of bits set in assembly language.

## Requirements and Specifications

Description:
Write an x86 assembler assignment program that does the following:
1. Gets a 32-bit number (up to 8 hex digits) from the console
2.  Determines the frequencies for each “power of two” that contributes to every bit set in that number.
• The powers of two to 32 bits positions are: 1, 2, 4, 8, 16 and 32
1. Output the frequency counts to the console
This program must include at least one procedure beyond MAIN.
Example:
For a hex input of 0F (0x1111b), only the low 4 bits of a 32 bit word (bits 1, 2, 3 and 4) are set. To set this sequence of bits, you need to use 1 twice, 2 twice and 4 once. The below shows the particular power of two sums for each bit position set.
• 1 = 1,
• 2 = 2,
• 3 = 1 + 2,
• 4 = 4
Screenshots of output

Source Code

TITLE CPSC 232 - Program #4

INCLUDE IRVINE32.INC

.DATA

TITLEMSG BYTE "CPSC 232 - Program #4", 0DH, 0AH, 0DH, 0AH, 0

MESSAGE BYTE "Input a 32-bit word (8 hex digits) >> ", 0

OUTPUT1 BYTE 0DH, 0AH, "1's frequency = ", 0

OUTPUT2 BYTE 0DH, 0AH, "2's frequency = ", 0

OUTPUT4 BYTE 0DH, 0AH, "4's frequency = ", 0

OUTPUT8 BYTE 0DH, 0AH, "8's frequency = ", 0

OUTPUT16 BYTE 0DH, 0AH, "16's frequency = ", 0

OUTPUT32 BYTE 0DH, 0AH, "32's frequency = ", 0

FREQ1 DWORD 0

FREQ2 DWORD 0

FREQ4 DWORD 0

FREQ8 DWORD 0

FREQ16 DWORD 0

FREQ32 DWORD 0

.CODE

FREQUENCIES PROC

mov EBX, 1 ; Bit to test

LOOP1: cmp EBX, 32 ; If the bit is above the 32

jg ENDL1 ; end the loop

shr EAX, 1 ; shift input

jnc SKIP1 ; If not set, skip

mov EDX, EBX ; Copy bit position to EDX

shr EDX, 1 ; test first bit

adc DWORD PTR [FREQ1], 0 ; increment frequency 1 if the bit was set

shr EDX, 1 ; test second bit

adc DWORD PTR [FREQ2], 0 ; increment frequency 2 if the bit was set

shr EDX, 1 ; test first bit

adc DWORD PTR [FREQ4], 0 ; increment frequency 4 if the bit was set

shr EDX, 1 ; test first bit

adc DWORD PTR [FREQ8], 0 ; increment frequency 8 if the bit was set

shr EDX, 1 ; test first bit

adc DWORD PTR [FREQ16], 0 ; increment frequency 16 if the bit was set

shr EDX, 1 ; test first bit

adc DWORD PTR [FREQ32], 0 ; increment frequency 32 if the bit was set

SKIP1: inc EBX ; Increment bit position

jmp LOOP1 ; Repeat the loop

FREQUENCIES ENDP

MAIN PROC

call WriteString ; Write string on console

call WriteString ; Write string on console

call FREQUENCIES ; call frequencies subroutine with EAX = read number

call WriteString ; Write output string on console

mov EAX, [FREQ1] ; load frequency value

call WriteDec ; Write value on console

call WriteString ; Write output string on console

mov EAX, [FREQ2] ; load frequency value

call WriteDec ; Write value on console

call WriteString ; Write output string on console

mov EAX, [FREQ4] ; load frequency value

call WriteDec ; Write value on console

call WriteString ; Write output string on console

mov EAX, [FREQ8] ; load frequency value

call WriteDec ; Write value on console

call WriteString ; Write output string on console

mov EAX, [FREQ16] ; load frequency value

call WriteDec ; Write value on console

call WriteString ; Write output string on console

mov EAX, [FREQ32] ; load frequency value

call WriteDec ; Write value on console

call CRLF ; Print CR/LF

Exit ; Terminate program

MAIN ENDP

END MAIN