A program in assembly language that only accepts letters as input.
In this program, when a letter is pressed, it is echoed to the screen. But when a non-alpha is pressed, the program loops back to the part where it accepts another input, thus rejecting the input in the process.
Code for Letters Only Input in Assembly Language
DATA SEGMENT
STR1 DB "ENTER YOUR STRING HERE ->$"
INSTR1 DB 40 DUP("$") ; FILLED WITH $ (OR NULL)
NEWLINE DB 10,13,"$"
DATA ENDS
START:
;INITIALIZE DATA
MOV AX,DATA
MOV DS,AX
;CLEAR CL
MOV CL, 00H
;GET STRING
MOV AH,09H
LEA DX,STR1 ;SAME AS MOV DX, OFFSET STR1
INT 21H
;SAVES INPUT TO INSTR1
MOV AH,0AH
MOV DX,OFFSET INSTR1
INT 21H
;NEXT LINE (ENTER)
MOV AH,09H
LEA DX,NEWLINE
INT 21H
;GET NUMBER OF CHARACTERS
MOV CL,INSTR1+1
;CHECK NON-ALPHA
MOV DX, 2 ;inital dx, para instr1+2
A1:
;ADD INSTR1, DL
MOV SI, DX
MOV AL, INSTR1+[SI]; for poiting addresses
;si=dx=2 initally, so parang instr1+2
ADD CL, 2;counter-balance dx =2
CMP DL, CL
JE A2
SUB CL, 2;restore value for cl
;COMPARE PROPER
CMP AL, 41H ;ascii of A
JS START ;if less than, it's non-alpha,else proceed
CMP AL, 5AH ;Z
JS A3 ;if less that, it's alpha, move to next character, else proceed
CMP AL, 61H ;a
JS START ;if less than, it's non-alpha, else proceed
CMP AL, 7AH ;z
JS A3 ;if less than, it's alpha, move to next letter,
JNS START;else, it's non-alpha, back to start
A3:
ADD DX,1;increment dx for si
JMP A1
A2:
POP DX;restore dx
;PRINT AT CENTER
MOV AX, CX
MOV BL, 2
DIV BL
MOV CX, 28H ;DECIMAL 40
SUB CX, AX
MOV AH, 02H ; set cursor option
MOV BH, 00H ;page 0
MOV DL, CL ;col position ;X-AXIS
MOV DH, 10 ; row position ;Y-AXIS
INT 10H
MOV AH,09H
;MOV DX, OFFSET INSTR1+2
LEA DX,INSTR1+2
INT 21H
END START