Hello, could anyone help me how can I make so the user could only enter numbers between 0...9 and letters a...f (A...F) on input? I have a code like this already

#MAKE_EXE#

DSEG SEGMENT 'DATA'

MSG DB 'Enter double digit hex number: $'

DSEG ENDS

SSEG SEGMENT STACK 'STACK'
DW 100h DUP(?)
SSEG ENDS

CSEG SEGMENT 'CODE'

;*******************************************

START PROC FAR

PUSH DS
MOV AX, 0
PUSH AX

; set segment registers:
MOV AX, DSEG
MOV DS, AX
MOV AH,09h
MOV DX, OFFSET MSG
INT 21h
XOR AX,AX
MOV AH,1H
INT 21H
MOV DL,AL
SUB DL,30H
CMP DL,9H
JLE M1
SUB DL,7H
M1:
MOV CL,4H
SHL DL,CL
INT 21H
SUB AL,30H
CMP AL,9H
JLE M2
SUB AL,7H
M2:
ADD DL,AL
RET
START ENDP

CSEG ENDS

END START

If this was code for our office or assignment it gets a failing grade due to no commenting. Sure you have one lonely comment on line 23 but then you make calls without any comment what you expect to happen.

To me this is the old "When I wrote this code, God and I knew what it did." Today, only God knows."

I don't know of any x86 BIOS calls that limit character input so you'll have to test your inputs later.

If you don't mind me asking, how are you running this? I am assuming that you are in a course on assembly language (or at the very least trying to learn from an old textbook), and that you are using DOSBox or something similar, but I am curious about the details. Note that INT 21H is specific to MS-DOS, and modern Windows doesn't support the old software interrupts at all.

As RProffitt said, you would want to read in the input as a pair of characters, then test both of them for their values to see if they are in the desired range.

I also agree on the point of the lack of comments. This is especially important in Assembly language (regardless of the ISA) given the lack of specificity in the individual instructions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.