Assembly Program to Count Words in a String


This program will prompt the user to enter a string after getting string the program will count the number of words present in the string.

The basic idea in the program is that the program counts the number of white spaces present in the string. So there is one limitation to the program. If there are two white spaces between two words then the program will count one extra word.

 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
.MODEL SMALL
.STACK 100H
.DATA  
STRING DB 100 DUP(?)
MSG1 DB "ENTER A STRING: $"
MSG2 DB 0DH, 0AH, "NO. OF WORDS_FOUNDS: $"
WORDS DW 0     
.CODE
MAIN PROC
    
     MOV AX, @DATA
     MOV DS, AX
     MOV ES, AX
     
     MOV AH, 9
     LEA DX, MSG1
     INT 21H
     
     LEA DI, STRING
 
     MOV AH, 1
     READ:
     INT 21H
     CMP AL, 0DH
     JE ENDOFSTRING
     STOSB
     JMP READ
     
     ENDOFSTRING:
     MOV AL, "$"
     STOSB
     
     XOR BX, BX
     
     COUNT:
     MOV AL, STRING[BX]
     CMP AL, "$"
     JE EXIT
     CMP AL, " "
     JE WORD_COMPLETED      
     INC BX
     JMP COUNT
     
     WORD_COMPLETED:
     INC WORDS
     INC BX 
     JMP COUNT     
         
     EXIT:
     INC WORDS
     
     MOV AH, 9
     LEA DX, MSG2
     INT 21H
     
     MOV AH, 2
     MOV DX, WORDS
     ADD DX, 30H
     INT 21H
     
    MAIN ENDP
END MAIN

Output:


No comments