Assembly Program to Count Characters in a String


The program will prompt the user to input a string and will count the number of characters present in that string. The program will not count white spaces as white spaces are not characters.

The program takes a string and stores the string in STRING and will start iteration if there is a white space the program will continue without incrementing CHARACTERS and if there is a character the program will increment CHARACTERS. After counting the characters the program simply prints the number of characters.

 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
.MODEL SMALL
.STACK 100H
.DATA  
STRING DB 100 DUP(?)
MSG1 DB "ENTER A STRING: $"
MSG2 DB 0DH, 0AH, "NO. OF CHARACTERS: $" 
CHARACTERS 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: 
     CMP [STRING+BX], '$'
     JE EXIT
     CMP [STRING+BX], ' '
     JE SPACE
     INC BX
     INC CHARACTERS
     JMP COUNT
     
     SPACE:
     INC BX
     JMP COUNT
     
     EXIT:     
     
     MOV AH, 9
     LEA DX, MSG2
     INT 21H
     
     MOV AH, 2
     MOV DX, CHARACTERS
     ADD DX, 30H
     INT 21H
     
    MAIN ENDP
END MAIN

Output:

1 comment:

  1. Help for this code: enter a sentence and then display
    the frequency of each character in that given sentence

    ReplyDelete