Basic choice command in Assembly

So this is pretty much like the choice command, not as advanced though. The only arguments you provide is a list of characters that the user can press as a choice. Just like the choice command, during the choice prompt it will keep looping for a character input until the user presses a valid key, that is, a one of the keys that you have specified in the list as the arguments.
(compile with fasm):


format MZ
mov si,0082h
xor cx,cx
xor dx,dx
getargc:
mov dl,[si]
cmp dl,0dh
je main
inc ch
inc si
jmp getargc
main:
mov bx,count
mov byte [bx],ch
input:
mov ax,0800h
int 21h
call validate
cmp dh,00
jne input
mov ah,4ch
mov al,cl
int 21h
validate:
mov si,0081h
mov bx,count
mov ch,byte [bx]
vloop:
cmp cl,ch
je exvloop
inc si
inc cl
cmp al,[si]
je success
jmp vloop
exvloop:
xor cx,cx
mov dh,01
ret
success:
mov dh,00
ret

count db 00

Usage example:

@echo off
echo Type a number from 1 to 5
choice 12345
echo You chose %errorlevel%

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: