News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

validaion help

Started by johnwales, March 18, 2007, 01:49:46 AM

Previous topic - Next topic

johnwales

I am have written some simple login code, I am trying to work out how to validate the two password inputs. Any pointers appreciated.
; This program reads a User name and password.

.model large ; specify a large memory model for this program
.stack 4096 ; allocate 4096 bytes to the stack
.386 ; use the 386 processor instruction set

.data ; define data variables used by the program

msg byte "Please input user name" ; msg is of type byte and overflows to 19 bytes
msg2 byte  0dh,0ah,"$" ; msg is of type byte and includes line feed, carr return and
; terminate

msg3 byte  0dh,0ah ; add line feed and carr return
byte "Welcome" ; add string as listed
byte  0dh,0ah,"$" ; add line feed and carr return and terminate char
msg4 byte "Please input password"
byte  0dh,0ah,"$"
msg5 byte  0dh,0ah ; add line feed and carr return
byte "Please re-input password" ; add string as listed
byte  0dh,0ah,"$" ; add line feed and carr return and terminate char
charst byte "*$"
char byte 00h ; allocate 0 to variable char

buffer  byte 40 DUP ('$') ; define buffer variable allocate 40 bytes DUP copies 40*$
buffer2 byte 40 DUP ('$') ; into buffer
.code

main PROC

mov ax,@data ; move the address of the data segment into ax
mov ds,ax ; copy the address into ds (the data segment
; register used  to store the segment used by this program)

mov ah,9 ; copy function 9 (display to screen) into the high bits of reg
; ax
mov dx,OFFSET msg ; copy the offset (within the segment in ds) of msg into dx (as
; dx req's the position of msg in the segment address stored in
; ds)
int 21h ; generate interrupt and display message to the screen

xor cx,cx ; clear register cx
cld ; clear the direction flag
lea di,buffer ; load the effective address of buffer , i.e. the offset within the data
;segment referenced by ds

L1:
mov ah,1 ; read a character, which is put in al or lower 8 bits of ax
int 21h

cmp al,1bh ; is the character <ESC>
je  Onward
cmp al,0dh ; or <RET>
je  Onward

mov [di],al ; store the character entered in the lower 8 bits of the offset di is pointing
;at
inc di ; increment the index register (di), i.e. point to the next 16 bits offset
jmp L1 ; loop


Onward:
mov ah,9
mov dx,OFFSET msg3 ; display message
int 21h

;mov ah,9 ; display the message read, by prefilling the message
;mov dx,OFFSET buffer ; buffer with "$" characters the message will be terminated
;int 21h ; by the first encountered.

mov ah,9
mov cx,4
mov dx,OFFSET buffer
int 21h
 
mov ah,9
mov dx,OFFSET msg2 ; output CRLF
int 21h

Pass:


mov ah,9 ; copy function 9 (display to screen) into the high bits of reg
; ax
mov dx,OFFSET msg4 ; copy the offset (within the segment in ds) of msg into dx (as
; dx req's the position of msg in the segment address stored in
; ds)
int 21h ; generate interrupt and display message to the screen
mov ah,9
mov dx,OFFSET msg2
int 21h

;xor cx,cx ; clear register cx
cld ; clear the direction flag
lea di,buffer ; load the effective address of buffer , i.e. the offset within the data
;segment referenced by ds

L2:
mov ah,8 ; read a character, which is put in al or lower 8 bits of ax
int 21h

cmp al,1bh ; is the character <ESC>
je  Onward2
cmp al,0dh ; or <RET>
je  Onward2

mov [di],al ; store the character entered in the lower 8 bits of the offset di is pointing
mov ah,9 ;at
mov dx,OFFSET charst
int 21h
inc di ; increment the index register (di), i.e. point to the next 16 bits offset
jmp L2 ; loop

Onward2:
mov ah,9
mov dx,OFFSET msg5 ; display message
int 21h
L3:
mov ah,8 ; read a character, which is put in al or lower 8 bits of ax
int 21h

cmp al,1bh ; is the character <ESC>
je  Fin
cmp al,0dh ; or <RET>
je  Fin

mov [di],al ; store the character entered in the lower 8 bits of the offset di is pointing
mov ah,9 ;at
mov dx,OFFSET charst
int 21h
inc di ; increment the index register (di), i.e. point to the next 16 bits offset
jmp L3 ; loop


Fin:
mov ah,4ch ; terminate program
int 21h

main ENDP
END main

MichaelW

Quotemov [di],al ; store the character entered in the lower 8 bits of the offset di is pointing at

I'm not sure how to interpret the comment. Offset addresses point to byte locations, and the size of the access at that location is encoded into the instruction. In this case MASM will effectively encode:

mov byte ptr[di], al

So the character is stored in the byte location specified by DI.

If you intend to use only a single buffer, then at label Onward2 you could again load the address of the buffer into DI, and for each character input compare it to the character stored at offset DI. For a match you could increment DI and input the next character, or for a mismatch report the error. If you intend to use two buffers, a better choice IMO, then at label Onward2 you could load the address of the second buffer into DI, and copy the input characters into the buffer. Then you could code a comparison routine that would compare the two buffers, and report any mismatch.
eschew obfuscation