Assembly Program to Read a Character and Convert it to Lower or Upper Case


This program will take a character from the user and convert it to lower case if it was in upper case or convert it to upper case if it was in lower case.

The basic idea in the program is when a lower case character entered by user we subtract 20 of Hexa from it and when an upper case character entered by user we add 20 of Hexa in it.

 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
.MODEL SMALL
.STACK 100H
.DATA
STR1 DB "ENTER A CHARACTER OR 0 TO EXIT: $"
STR2 DB "RESULT IS: $" 
INVALID_STRING DB "INVALID CHARACTER ENTERED $"
NEWLINE DB 0DH, 0AH, "$"  
.CODE
MAIN PROC                                        
    
    MOV AX, @DATA
    MOV DS, AX
    
    READ:
    
    LEA DX, STR1
    MOV AH, 9
    INT 21H  
    
    MOV AH, 1
    INT 21H
    
    CMP AL, 30H
    JE EXIT
    
    MOV BH, AL
    
    LEA DX, NEWLINE
    MOV AH, 9
    INT 21H 
    
    CMP BH, 65
    JL INVALID
    CMP BH, 90
    JG LOWER_CASE
    ADD BH, 20H
    JMP PRINT
    
    LOWER_CASE:
    
    CMP BH, 97
    JL INVALID
    CMP BH, 122
    JG INVALID
    SUB BH, 20H   
    
    PRINT:
    
    LEA DX, STR2
    MOV AH, 9
    INT 21H
    
    MOV AH, 2
    MOV DL, BH
    INT 21H 
    
    LEA DX, NEWLINE
    MOV AH, 9
    INT 21H
    
    JMP READ
    
    INVALID:
    
    LEA DX, INVALID_STRING
    MOV AH, 9
    INT 21H
    
    LEA DX, NEWLINE
    MOV AH, 9
    INT 21H
    
    JMP READ
    
    EXIT:    

    MAIN ENDP
END MAIN

Output:


No comments