Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

TASM, validate user input 5digit, how?

user input validate

  • Please log in to reply
14 replies to this topic

#1 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 01 February 2011 - 07:09 AM

HOW TO validate user input 5digit using variable in .data?

.model small
.stack 100h
.data
	promptmsg		db	'Enter Pin Code:$'
	promptvalid		db	'Pin Code Accepted!$'
	promptinvalid	db	'Pin Code Incorrect!$'
	prompttimeout	db	'3rd attempt..Session terminated!$'
	promptretry		db	'Do you want to try again? [ y / n ]:$'
;	pass			db	'12345'
.code
main proc
	mov cx, 3
start:
	call newline
	mov ax,@data
	mov ds, ax
	
	lea dx, promptmsg
	call promptterminate

	mov ah,1
	int 21h
	
	mov bh,al
	cmp bh, '1'	;i want to compare 5digit code using the value in .data , how?
	je accepted
	jne invalid
	
invalid:
	dec cx
	je timeout
	call newline
	lea dx, promptinvalid
	call promptterminate
	call newline
	lea dx, promptretry
	call promptterminate
	
	mov ah, 1
	int 21h
	
	mov bh, al
	
	cmp bh, 121	; if user keyboard press 'y' key
	je start
	jmp exit
	
	
timeout:
	call newline
	lea dx, prompttimeout
	call promptterminate	
	jmp exit
	
accepted:
	call newline
	lea dx, promptvalid
	call promptterminate
	jmp exit

promptterminate:
	mov ah, 9
	int 21h
	ret
	
newline:
	mov ah,2
	mov dl, 0ah
	int 21h
	ret
	
exit:
	mov ah, 4ch
	int 21h
	
	main endp
end main

Edited by dargueta, 01 February 2011 - 06:33 PM.
Changed quote tags to code

  • 0

#2 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 01 February 2011 - 05:31 PM

So are you trying to compare the five digits and accept if all five digits are correct?

Something like?:
if (input() == '1') 
  if (input() == '2') 
    if (input() == '3') 
      if (input() == '4') 
        if (input() == '5') goto accept; 
goto invalid;


With input() replaced with the code that gets a character from the keyboard.

Note: This is not assembly nor C; this is just some syntax.

Or do you want to input a string and then check it for the right data?
  • 0

#3 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 01 February 2011 - 07:24 PM

So are you trying to compare the five digits and accept if all five digits are correct?

Something like?:

if (input() == '1') 
  if (input() == '2') 
    if (input() == '3') 
      if (input() == '4') 
        if (input() == '5') goto accept; 
goto invalid;


With input() replaced with the code that gets a character from the keyboard.

Note: This is not assembly nor C; this is just some syntax.

Or do you want to input a string and then check it for the right data?



the pseudo code is

1. input 5 digit
2. compare inputted 5 digit to "12345"
3. if 12345 inputted as one time read..then accept
4. if 123, or any inputted as one time read..then invalid
5. if 3 times attemp..it will display info then exit


is there any remedy which would one time compare those 5 digit which compare to the .data pass db "12345"

i think your sample codes requires a lot of 5 read (mov ah, 1) , 5 save (mov bh, bl) and 5 compare (cmp bh, ? ) which will result in long lines of codes..

something goes like
.data
pass db "12345"

.
.
.

mov ah, 1
.
.
mov bh, bl
.
.
cmp bh, pass




possible codes..
  • 1

#4 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 02 February 2011 - 06:39 AM

It would work if you use a few functions (by the way, the endl character is usually supposed to be "\r\n" (13, 10), not "\n" (10), but "\n" is okay too).

You can make your own puts(), gets(), and strcmp() and use them for your program. I tried it with my functions and it did work.
  • 0

#5 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 03 February 2011 - 06:12 AM

I made a .com file; it did work.

I attached the file in the .zip folder.

Attached Files


Edited by RhetoricalRuvim, 05 February 2011 - 09:52 PM.

  • 1

#6 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 05 February 2011 - 08:18 PM

I made a .com file; it did work.

I attached the file in the .zip folder.


can u post the tasm code? thank you in advance
  • 0

#7 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 05 February 2011 - 09:48 PM

I have to see your code first. We're not supposed to do homework for people on this forum (I posted the .com file to show that it is possible). If you need help with coding it, we'll try to help, but it might not help you too much, unless you try it yourself.

I could, however, tell you how the program I made works.

It has three functions: puts(), gets(), and strcmp();

puts() does something like this:
step 1: take the address of the string, passed to puts() as a parameter, and store it in a register
step 2: get a byte from the memory location of the address from that register
step 3: compare that byte to 0 (0, not '0')
step 4: if it is 0, go to step 7
step 5: output that byte to the screen using int 21h ah= 2
step 6: increment the register with the memory address and go to step 2
step 7: return

gets() does something like this:
step 1: take the function parameter (the address of the string to write to) and store it in two registers (one for pointing and one for checking the other)
step 2: input a character from the keyboard using int 21h ah= 1
step 3: compare that character to 13 (return character)
step 4: if it is equal, go to step 12
step 5: compare that character to 8 (backspace character)
step 6: if it is not equal, go to step 10
step 7: compare the register with the pointer to the other register with the address of the string to write to
step 8: if greater than, decrement the register with the pointer
step 9: go to step 2
step 10: move that character to the memory location at the address pointed to by the register with the address of the string
step 11: increment the pointer register and go to step 2
step 12: return from the function

strcmp() works something like this:
step 1: get the address of the first string and put it into a register
step 2: get the address of the second string and put it into a register
step 3: move a byte from the memory location at the address pointed to by the register with the first-string address to a register
step 4: compare that byte to 0, and if equal then go to step 10
step 5: subtract the byte at the memory location at the address pointed to by the register with the second-string address from the register with the byte from step 3
step 6: if not zero (the JNZ instruction), go to step 9
step 7: increment both registers that contain the pointers to the strings
step 8: go to step 3
step 9: return whatever's in the register that you just subtracted from
step 10: return 0

These steps do not exactly match the actual code from the program I made, but this is very close.

I think you can figure out the parts of the program that call these functions.

Once again, if you need help figuring these steps out or if something's wrong with your code, you can post on this forum and ask about it.


Edit: By the way, I forgot to include a step in the .com file I posted earlier; I re-uploaded the .zip folder to the earlier post. The thing I forgot to include, earlier - in the .com file - , is the part from steps 7 and 8 of the gets() function. So yeah, I forget things too.
  • 0

#8 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 05 February 2011 - 10:17 PM

i have just posted the code in the first post..we have not taken up yet those puts(), gets(), and strcmp(); that is what all i know in my first post..im just a newbie..i 've been searching for TASM 5.0 borland references but its hard to find here in search engines..

but thank you for your effort in teaching those pseudo codes.. our teacher dont specifically teach properly those instructions as what you wrote..we are not specialize in programming courses, because this is only the pre-requisites in our BSIT course..my head is aching just trying to debug this codes..sometimes the computer is hanging...
  • 0

#9 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 05 February 2011 - 10:19 PM

for addendum, we have not yet discuss the if then else statement..only je, jne, jnz, jmp
  • 0

#10 paslang

paslang

    CC Lurker

  • Just Joined
  • Pip
  • 8 posts

Posted 05 February 2011 - 10:35 PM

I have to see your code first. We're not supposed to do homework for people on this forum (I posted the .com file to show that it is possible). If you need help with coding it, we'll try to help, but it might not help you too much, unless you try it yourself.

I could, however, tell you how the program I made works.

It has three functions: puts(), gets(), and strcmp();

puts() does something like this:
step 1: take the address of the string, passed to puts() as a parameter, and store it in a register
step 2: get a byte from the memory location of the address from that register
step 3: compare that byte to 0 (0, not '0')
step 4: if it is 0, go to step 7
step 5: output that byte to the screen using int 21h ah= 2
step 6: increment the register with the memory address and go to step 2
step 7: return

gets() does something like this:
step 1: take the function parameter (the address of the string to write to) and store it in two registers (one for pointing and one for checking the other)
step 2: input a character from the keyboard using int 21h ah= 1
step 3: compare that character to 13 (return character)
step 4: if it is equal, go to step 12
step 5: compare that character to 8 (backspace character)
step 6: if it is not equal, go to step 10
step 7: compare the register with the pointer to the other register with the address of the string to write to
step 8: if greater than, decrement the register with the pointer
step 9: go to step 2
step 10: move that character to the memory location at the address pointed to by the register with the address of the string
step 11: increment the pointer register and go to step 2
step 12: return from the function

strcmp() works something like this:
step 1: get the address of the first string and put it into a register
step 2: get the address of the second string and put it into a register
step 3: move a byte from the memory location at the address pointed to by the register with the first-string address to a register
step 4: compare that byte to 0, and if equal then go to step 10
step 5: subtract the byte at the memory location at the address pointed to by the register with the second-string address from the register with the byte from step 3
step 6: if not zero (the JNZ instruction), go to step 9
step 7: increment both registers that contain the pointers to the strings
step 8: go to step 3
step 9: return whatever's in the register that you just subtracted from
step 10: return 0

These steps do not exactly match the actual code from the program I made, but this is very close.

I think you can figure out the parts of the program that call these functions.

Once again, if you need help figuring these steps out or if something's wrong with your code, you can post on this forum and ask about it.


Edit: By the way, I forgot to include a step in the .com file I posted earlier; I re-uploaded the .zip folder to the earlier post. The thing I forgot to include, earlier - in the .com file - , is the part from steps 7 and 8 of the gets() function. So yeah, I forget things too.


.model small
.stack 100h
.data
promptmsg db 0ah, 0dh,'Enter Pin Code: $'
promptvalid db 0ah, 0dh,'Pin Code Accepted!$'
promptinvalid db 0ah, 0dh,'Pin Code Incorrect!$'
prompttimeout db 0ah, 0dh,'3rd attempt..Session terminated!$'
promptretry db 0ah, 0dh,'Do you want to try again? [ y / n ]: $'
.code
main proc
mov cx, 3 ;initialize counters of 3
start:
mov ax,@data ;initialize data
mov ds, ax

lea dx, promptmsg ;display data
call endpar
call readfirst ;read 1st digit input
call readsecond ;read 2nd digit input
call readthird ;read 3rd digit input
call readfourth ;read 4th digit input
call readfifth ;read 5th digit input
cmp bh, 49 ;compare BH to Decimal Number 1
je second ;if compare is equal to 1 then goto second
jne invalid ;if compare is not equal to 1 then goto invalid

second:
cmp ch, 50 ;compare BH to Decimal Number 2
je third ;if compare is equal to 2 then goto third
jne invalid ;if compare is not equal to 2 then goto invalid

third:
cmp dh, 51 ;compare DH to Decimal Number 3
je fourth ;if compare is equal to 3 then goto fourth
jne invalid ;if compare is not equal to 3 then goto invalid

fourth:
cmp bl, 52 ;compare BL to Decimal Number 4
je fifth ;if compare is equal to 4 then goto fifth
jne invalid ;if compare is not equal to 4 then goto invalid

fifth:
cmp cl, 53 ;compare CL to Decimal Number 5
je accepted ;if compare is equal to 5 then goto accepted
jne invalid ;if compare is not equal to 5 then goto invalid

invalid:
lea dx, promptinvalid ;print pincode is not correct
call endpar
jmp exit

retry:
dec cx ;decrement counter CX
je timeout ;if CX is equal to zero, read attempt aborted
lea dx, promptretry ;print retry statement
call endpar

mov ah, 1 ;read input data
int 21h

mov bh, al ;save AL to BH

cmp bh, 121 ; if user keyboard press small letter 'y' key
je start
jmp exit


timeout:
lea dx, prompttimeout ;print session terminated
call endpar
jmp exit

accepted:
lea dx, promptvalid ;print pincode is correct
call endpar
jmp exit

endpar:
mov ah, 9
int 21h
ret

readfirst:
mov ah,1 ;read input
int 21h
mov bh,al ;save 1st AL to BH register
ret

readsecond:
mov ah,1 ;read input
int 21h
mov ch,al ;save 2nd AL to CH register
ret

readthird:
mov ah,1 ;read input
int 21h
mov dh,al ;save 3rd AL to DH register
ret

readfourth:
mov ah,1 ;read input
int 21h
mov bl,al ;save 4th AL to BL register
ret

readfifth:
mov ah,1 ;read input
int 21h
mov cl,al ;save 5th AL to CL register
ret

exit:
mov ah, 4ch ;exit DOS
int 21h

main endp
end main

Im having a problem with this codes, just because it does not meet the thrice attempt..it should exit the program if the 3rd attempt to input pass is wrong..
  • 0

#11 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 05 February 2011 - 10:42 PM

When is this assignment due? If you still have time, you can learn some instructions and try making those functions; if your functions, that you come up with, don't work then you could ask for help here. If it's totally impossible to do that then you could try things like
mov ah, 1
int 21h
cmp al, '1'
jnz not_equal
int 21h
cmp al, '2'
.....

I would say, though, that it is more efficient to use the functions.
  • 0

#12 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 05 February 2011 - 10:52 PM

.model small
.stack 100h
.data
promptmsg db 0ah, 0dh,'Enter Pin Code: $'
promptvalid db 0ah, 0dh,'Pin Code Accepted!$'
promptinvalid db 0ah, 0dh,'Pin Code Incorrect!$'
prompttimeout db 0ah, 0dh,'3rd attempt..Session terminated!$'
promptretry db 0ah, 0dh,'Do you want to try again? [ y / n ]: $'
.code
main proc
mov cx, 3 ;initialize counters of 3
start:
mov ax,@data ;initialize data
mov ds, ax

lea dx, promptmsg ;display data
call endpar
call readfirst ;read 1st digit input
call readsecond ;read 2nd digit input
call readthird ;read 3rd digit input
call readfourth ;read 4th digit input
call readfifth ;read 5th digit input
cmp bh, 49 ;compare BH to Decimal Number 1
je second ;if compare is equal to 1 then goto second
jne invalid ;if compare is not equal to 1 then goto invalid

second:
cmp ch, 50 ;compare BH to Decimal Number 2
je third ;if compare is equal to 2 then goto third
jne invalid ;if compare is not equal to 2 then goto invalid

third:
cmp dh, 51 ;compare DH to Decimal Number 3
je fourth ;if compare is equal to 3 then goto fourth
jne invalid ;if compare is not equal to 3 then goto invalid

fourth:
cmp bl, 52 ;compare BL to Decimal Number 4
je fifth ;if compare is equal to 4 then goto fifth
jne invalid ;if compare is not equal to 4 then goto invalid

fifth:
cmp cl, 53 ;compare CL to Decimal Number 5
je accepted ;if compare is equal to 5 then goto accepted
jne invalid ;if compare is not equal to 5 then goto invalid

invalid:
lea dx, promptinvalid ;print pincode is not correct
call endpar
jmp exit

retry:
dec cx ;decrement counter CX
je timeout ;if CX is equal to zero, read attempt aborted
lea dx, promptretry ;print retry statement
call endpar

mov ah, 1 ;read input data
int 21h

mov bh, al ;save AL to BH

cmp bh, 121 ; if user keyboard press small letter 'y' key
je start
jmp exit


timeout:
lea dx, prompttimeout ;print session terminated
call endpar
jmp exit

accepted:
lea dx, promptvalid ;print pincode is correct
call endpar
jmp exit

endpar:
mov ah, 9
int 21h
ret

readfirst:
mov ah,1 ;read input
int 21h
mov bh,al ;save 1st AL to BH register
ret

readsecond:
mov ah,1 ;read input
int 21h
mov ch,al ;save 2nd AL to CH register
ret

readthird:
mov ah,1 ;read input
int 21h
mov dh,al ;save 3rd AL to DH register
ret

readfourth:
mov ah,1 ;read input
int 21h
mov bl,al ;save 4th AL to BL register
ret

readfifth:
mov ah,1 ;read input
int 21h
mov cl,al ;save 5th AL to CL register
ret

exit:
mov ah, 4ch ;exit DOS
int 21h

main endp
end main

Im having a problem with this codes, just because it does not meet the thrice attempt..it should exit the program if the 3rd attempt to input pass is wrong..


You can use a memory variable, such as:

.data 
count            dw 0 
.code 

.....  

;; When you need to increment the count: 
inc word ptr [count] 

.....  

;; When you need to check if the count is less than three: 
cmp word ptr [count], 3 
jl count_is_less_than_3            ;; go to the 'count_is_less_than_3' label if word [count] is less than three 
jg count_is_greater_than_3      ;; .....  
jmp count_is_3 

;; Note:  On MASM, the syntax for the word at memory location 'count' looks like 'word ptr [count]', in NASM it's 'word [count]', however I don't know what it is for TASM. 

.....

So you could use a memory variable. Keep in mind, though, that you can only use one memory operand for addition, subtraction, comparing, and moving. As far as I know, you can't use memory operands for multiplication (or at least for older processors such as Intel8088).
  • 0





Also tagged with one or more of these keywords: user input, validate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download