Instructions
Requirements and Specifications
- Gets a 32-bit number (up to 8 hex digits) from the console
- 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
- Output the frequency counts to the console
- 1 = 1,
- 2 = 2,
- 3 = 1 + 2,
- 4 = 4
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
ENDL1: ret ; Return to main
FREQUENCIES ENDP
MAIN PROC
lea EDX, TITLEMSG ; Load addess of title in EDX
call WriteString ; Write string on console
lea EDX, MESSAGE ; Load addess of message in EDX
call WriteString ; Write string on console
call ReadHex ; Read number in hexadecimal
call FREQUENCIES ; call frequencies subroutine with EAX = read number
lea EDX, OUTPUT1 ; Load addess of output message in EDX
call WriteString ; Write output string on console
mov EAX, [FREQ1] ; load frequency value
call WriteDec ; Write value on console
lea EDX, OUTPUT2 ; Load addess of output message in EDX
call WriteString ; Write output string on console
mov EAX, [FREQ2] ; load frequency value
call WriteDec ; Write value on console
lea EDX, OUTPUT4 ; Load addess of output message in EDX
call WriteString ; Write output string on console
mov EAX, [FREQ4] ; load frequency value
call WriteDec ; Write value on console
lea EDX, OUTPUT8 ; Load addess of output message in EDX
call WriteString ; Write output string on console
mov EAX, [FREQ8] ; load frequency value
call WriteDec ; Write value on console
lea EDX, OUTPUT16 ; Load addess of output message in EDX
call WriteString ; Write output string on console
mov EAX, [FREQ16] ; load frequency value
call WriteDec ; Write value on console
lea EDX, OUTPUT32 ; Load addess of output message in EDX
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