- 2068 Bhadra · 8 marks
8086 ALP: count vowels, consonants, numerals
Short answer
Use four counters NV, NC, ND, NO. Classify each character as vowel, consonant, or not a letter; for non-letters, test '0' to '9' as a numeral, otherwise count as other. Display all four counts in decimal using PRINT_DEC.
Write a program in 8086 to read a string and count the number of vowels, consonants, numerals and other characters and display the count.
Method: four counters (NV, NC, ND, NO) hold vowels, consonants, numerals and other characters. For each character CLASSIFY returns 1 (vowel), 2 (consonant) or 0 (not a letter); for 0 the character is tested for '0'..'9', otherwise it is counted as "other" (space, punctuation, symbols). The four counts are displayed in decimal.
.MODEL SMALL
.STACK 100H
DISPSTR MACRO MSG ; display a '$'-terminated string
LEA DX,MSG
MOV AH,09H
INT 21H
ENDM
CLRSCR MACRO ; clear screen and home the cursor
MOV AX,0600H ; scroll whole window (AL=0 -> blank it)
MOV BH,07H ; normal attribute
MOV CX,0000H ; upper-left corner (row 0, col 0)
MOV DX,184FH ; lower-right corner (row 24, col 79)
INT 10H
MOV AH,02H ; set cursor position
MOV BH,00H ; page 0
MOV DX,0000H ; row 0, col 0
INT 10H
ENDM
.DATA
MSG1 DB 'Enter a string: $'
MV DB 13,10,'Vowels = $'
MC DB 13,10,'Consonants = $'
MD DB 13,10,'Numerals = $'
MO DB 13,10,'Others = $'
NV DB 0
NC DB 0
ND DB 0
NO DB 0
BUF DB 80, ?, 80 DUP(0) ; DOS buffered-input area (max, actual, chars)
CRLF DB 13,10,'$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
DISPSTR MSG1 ; prompt
LEA DX,BUF ; read the string with DOS function 0Ah
MOV AH,0AH
INT 21H
LEA SI,BUF+2
MOV CL,BUF+1
MOV CH,0
JCXZ SHOW
NEXT:
MOV DL,[SI]
CALL CLASSIFY
CMP AL,1
JE ISV
CMP AL,2
JE ISC
CMP DL,'0' ; not a letter: digit?
JB ISO
CMP DL,'9'
JA ISO
INC ND ; numeral
JMP SKIP
ISV:INC NV ; vowel
JMP SKIP
ISC:INC NC ; consonant
JMP SKIP
ISO:INC NO ; other character
SKIP:
INC SI
LOOP NEXT
SHOW:
DISPSTR MV
MOV AL,NV
XOR AH,AH
CALL PRINT_DEC
DISPSTR MC
MOV AL,NC
XOR AH,AH
CALL PRINT_DEC
DISPSTR MD
MOV AL,ND
XOR AH,AH
CALL PRINT_DEC
DISPSTR MO
MOV AL,NO
XOR AH,AH
CALL PRINT_DEC
MOV AH,4CH ; return to DOS
INT 21H
MAIN ENDP
; ---- classify the character in DL
; returns AL = 1 vowel, 2 consonant, 0 not a letter (DL unchanged)
CLASSIFY PROC
MOV AL,DL
OR AL,20H ; fold to lower case (valid for letters)
CMP AL,'a'
JB NOTLET
CMP AL,'z'
JA NOTLET
CMP AL,'a'
JE VOW
CMP AL,'e'
JE VOW
CMP AL,'i'
JE VOW
CMP AL,'o'
JE VOW
CMP AL,'u'
JE VOW
MOV AL,2 ; consonant
RET
VOW:MOV AL,1 ; vowel
RET
NOTLET:MOV AL,0 ; not a letter
RET
CLASSIFY ENDP
; ---- print the unsigned number in AX in decimal (registers preserved)
PRINT_DEC PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
MOV BX,10
XOR CX,CX ; digit counter
PD1:XOR DX,DX
DIV BX ; AX = quotient, DX = remainder (digit)
PUSH DX
INC CX
OR AX,AX
JNZ PD1
PD2:POP DX ; digits come out in correct order
ADD DL,30H ; to ASCII
MOV AH,02H
INT 21H
LOOP PD2
POP DX
POP CX
POP BX
POP AX
RET
PRINT_DEC ENDP
END MAIN
Example: Room 101 ! → Vowels = 2, Consonants = 2, Numerals = 3, Others = 3 (the 2 blanks and ! are the 3 others).
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- 8086 ALP: count vowels and display string · Microprocessor, chapter 3
- 8086 ALP: vowels and consonants separately · Microprocessor, chapter 3
- 8086 ALP: display only alphabetic characters · Microprocessor, chapter 3
- 8086 ALP: vowels to uppercase, counts · Microprocessor, chapter 3
- 8086 ALP: words on lines and word count · Microprocessor, chapter 3
- 8086 ALP: alternate case display · Microprocessor, chapter 3
- Display N characters using default arguments · Object Oriented Programming, chapter 3