News:

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

szCmp and StdIn Problems

Started by scolby33, July 28, 2009, 05:34:55 PM

Previous topic - Next topic

scolby33

Hi! I am just getting started with ASM. After writing the obligatory Hello World program, I decided to try to make a program that asks for a password, and compares it with the stored "correct" password. Below is the code I have written so far. There are two problems. First, no matter what is entered as the password, szCmp returns no match (zero), and the password is "wrong." Second, I have tried to rewrite StdIn to not echo the password, but the assembler always says that there are invalid operands for the two mov instructions. Initially I tried to use a procedure, similar to StdIn, but I couldn't make that work either. Finally, two nagging issues: instead of turning echo off, could I display * for each character of the password? How could this be done? And how can I make the program wait at the end for any key press, instead of using StdIn and waiting for enter or ^c.
Thanks in advance :)

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; Begin File
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
.386
.model flat,stdcall
option casemap:none

;PassIn proto :DWORD,:DWORD

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    .data
      ;inputhd   db  NULL
      ;bRead     db  NULL

    .data?
      entry     db  ?
      success   db  ?

    .const
      challenge db  "Enter Password:",0
      proresp   db  "Password Correct!",0
      conresp   db  "Password Incorrect!",0
      exitmsg   db  "Press enter to exit...",0
      password  db  "password"

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    invoke ClearScreen
    invoke StdOut,addr challenge
    invoke locate,0,2
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; Begin PassIn (StdIn modified with echo off for password input)
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

;        invoke GetStdHandle,STD_INPUT_HANDLE
;        mov inputhd,eax

;        invoke SetConsoleMode,addr inputhd,ENABLE_LINE_INPUT or ENABLE_PROCESSED_INPUT

;        invoke ReadFile,addr inputhd,addr entry,128,ADDR bRead,NULL

;        mov eax,bRead

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; End PassIn
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    invoke StdIn,addr entry,128
;    invoke StdOut,addr entry       ;for debugging, remove
;    invoke StdOut,addr password    ;for debugging, remove
    xor eax,eax
    invoke locate,0,5
    invoke ucCmp,addr entry,addr password
    .if eax == 0
        invoke StdOut,addr conresp
    .else
        invoke StdOut,addr proresp
    .endif
    invoke locate,0,7
    invoke StdOut,addr exitmsg
    invoke StdIn,eax,0
    invoke ExitProcess,0
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; End File
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Slugsnack

you can do it a lot easier with macros..

include \masm32\include\masm32rt.inc

.code
    Start:

    invoke AllocConsole

mov edi, input("Enter Password: ")
    invoke szCmp, edi, chr$("password")

    .IF eax
                print "Password Correct!", 13, 10
    .ELSE
                print "Password Incorrect", 13, 10
    .ENDIF

        inkey

    invoke FreeConsole
    invoke ExitProcess, 0

    end Start

[attachment deleted by admin]

scolby33

Thank you. This is a very good solution. I still have one question--why did szCmp always return 0 to eax in my code?

dedndave

Quoteinvoke ucCmp,addr entry,addr password
you mean that one ? - lol
ucCmp is for unicode strings
szCmp is for zero-terminated ascii strings

jj2007

Quote from: Slugsnack on July 28, 2009, 05:43:03 PM
you can do it a lot easier with macros..

If you use macros, do it properly:

include \masm32\include\masm32rt.inc

.code
Start:
.IF rv(szCmp, input("Enter Password: "), chr$("password"))
print "Password Correct!", 13, 10
.ELSE
print "Password Incorrect", 13, 10
.ENDIF
inkey
exit
end Start

ChrisLeslie

Hi Scolby33

It could be that stdIn puts a carriage return and line feed (13,10) at the end of the input string. If so, there will never be a match unless you strip it off before the compare. Also, I would zero terminate the password constant to be safe.

Chris

scolby33

Sorry about szCmp/ucCmp.  I just changed it to see if it would work and forgot to change it back before I posted. Also, what is chr$? And can I obscure the password input? I suppose I could copy the input macro into my code and remove echo, but that uses stdIn, so I would have to incorporate both to remove the echo.

dedndave

hi scolby
you may hafta write your own p/w obscure
i think i would use GetStdHandle to open STD_INPUT_HANDLE, then use ReadConsoleInput or ReadFile, 1 char at a time
that'll be kinda fun - have to let them edit with backspace
chr$ is a masm32 macro that displays strings and chars
refer to \masm32\help\hlhelp.chm
look for macro catagories - string macros

hutch--

scolby33,

The problem is using the console to type passwords, to get the level of control you are after you need to be able to process each keystroke as it is entered at the console which StdIn cannot do for you. It can be done using console commands but be warned its no joy to do.

If alternately you can do the job in the normal Windows GUI, its an easy task that will work perfectly for you using either standard text boxes or custom subclassed ones that you control the character filtering with.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php