The MASM Forum

General => The Campus => Topic started by: markallyn on September 17, 2017, 03:16:20 AM

Title: inkey hangs
Post by: markallyn on September 17, 2017, 03:16:20 AM
Hello everyone,

Can anybody suggest how to stop the inkey command from hanging.  It prints out the message, but then hangs.  To get out I have to do CTRL C.

Thanks,
Mark Allyn
Title: Re: inkey hangs
Post by: aw27 on September 17, 2017, 03:47:12 AM
Can anybody suggest how to stop the inkey command from hanging.  It prints out the message, but then hangs.  To get out I have to do CTRL C.
inkey is a macro that calls a masm32 library function which in turn calls MSVCRT functions.
Probably, you should post a proof that it hangs.
Title: Re: inkey hangs
Post by: markallyn on September 17, 2017, 04:33:41 AM
Hi aw27,

Here's the program.  It is just a test run of the masm32 macros to do an dwtoa function.  Try it out, if you don't mind.  I just did and as I say, it hangs.

Quote
include \masm32\include\masm32rt.inc
option casemap:NONE


.DATA
num1   DWORD   123456
frmt1   BYTE   "The ascii version of num is : %s",13,10,0

.DATA?
buff   DWORD 20 DUP (?)

.CODE
start:
   inkey
   mov   eax, num1
   INVOKE   dwtoa, eax, ADDR buff
   INVOKE   crt_printf, ADDR frmt1, ADDR buff
   INVOKE   ExitProcess, 0
end start

Thanks,
Mark
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 04:37:45 AM
it's waiting for a keypress :)
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 04:41:09 AM
... if what you are after is a non-blocking "inkey" (similar to the old basic "inkey"), let me know
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 04:44:08 AM
this may not be my latest one - lol
MSVCRT.INC + LIB req'd

Code: [Select]
InKyb   PROC

;Polled Keyboard Input - DednDave 8, 2010
;
;This function returns a keystroke in EAX if there is one in the buffer.
;If the buffer is empty, the function returns immediately.
;
;If the keyboard buffer is empty, AH = 0, AL = 0, ZF = 1.
;If the stroke is a regular key, AH = 0, AL = key char, ZF = 0.
;If the stroke is an extended key, AH = extended key, AL = E0h, ZF = 0.
;If the stroke is a function key, AH = function key, AL = 0, ZF = 0.
;
;ECX, EDX are not preserved.

        call    crt__kbhit
        or      eax,eax
        jz      InKyb1

        call    crt__getch
        and     eax,0FFh
        jz      InKyb0

        cmp     al,0E0h
        jnz     InKyb1

InKyb0: push    eax
        call    crt__getch
        pop     edx
        shl     eax,8
        or      eax,edx

InKyb1: retn

InKyb   ENDP
Title: Re: inkey hangs
Post by: markallyn on September 17, 2017, 04:46:27 AM
Hi Dave,

I have been pressing "enter" and nothing happens.  Are you saying that it should be some other key?

BTW, I linked this with the /SUBSYTEM:console switch.

The printf() macro works fine.

I think I am indeed looking for what you  refer to as a "non-blocking 'inkey'.  What's the difference?

Thanks for responding.  Much appreciated.

Regards,
Mark Allyn
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 04:53:35 AM
the enter key should work
but, yah - i figured you wanted a polled or non-blocking routine (posted above)
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 04:57:17 AM
i added another inkey before exit so you can see the result before the console window closes
the enter key works fine, here
Title: Re: inkey hangs
Post by: jj2007 on September 17, 2017, 05:45:22 AM
Mark, how does this one behave on your machine?
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
  inkey "hit a key"
  print "bye"
  invoke Sleep, 2000
  exit

end start
Title: Re: inkey hangs
Post by: markallyn on September 17, 2017, 05:57:14 AM
Hi Dave and jj2007,

Dave:  Did you send me the write file in test.zip?  The asm code looks just like the one I sent you.

jj:  I ran your code and it behaves just like mine.  It prints out "hit a key", then hangs.  I have to hit CTRL C to get out (the process is still running), and when I do I clobber the shell so that it doesn't respond to the keyboard and have grab a new one.

Thanks for helping.

Mark
Title: Re: inkey hangs
Post by: dedndave on September 17, 2017, 06:29:24 AM
it is the same as yours except i added another "inkey" line

the command lines to build it should look something like this:

Code: [Select]
ml /c /coff test.asm
Link /SUBSYSTEM:CONSOLE /OPT:NOREF test.obj

the 32-bit linker should be used
Title: Re: inkey hangs
Post by: aw27 on September 17, 2017, 07:41:40 AM
Mark,
You must test it in the command prompt, not a shell.
Title: Re: inkey hangs
Post by: hutch-- on September 17, 2017, 03:44:58 PM
Mark,

This is the type of code that "inkey" is used in.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                     Build this console app with
                  "MAKEIT.BAT" on the PROJECT menu.
        ----------------------------------------------------- *

    .code

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

    call main
    inkey "Press any key to exit 'inkey' :]"
    exit

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

main proc

    print "Do what you want to do here.",13,10,13,10

    ret

main endp

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

end start
Title: Re: inkey hangs
Post by: markallyn on September 17, 2017, 10:52:40 PM
Hutch,

Thanks.  I'll give it a try!  I'll let you know what happened.

Mark
Title: Re: inkey hangs
Post by: markallyn on September 17, 2017, 11:43:17 PM
Good morning, Hutch,

OK, I ran your code and again it failed to read my keystroke.  And I had to terminate the session ungracefully.  Perhaps I should note that I am running masm/masm32 under WINE on a Linux box.  To date I have had no problems although doing this involves a few more keystrokes than would a WIN 32/64 session. 

As noted above, the masm32 printf macro works nicely.

Regards,

Mark
Title: Re: inkey hangs
Post by: hutch-- on September 17, 2017, 11:59:11 PM
Mark,

It may be the case the Wine does not respond to the MSVCRT dll in the same way that it does in Windows 32/64. From memory you can construct a similar effect with an API function call but its been years since I have done one like that so I don't remember how its done.
Title: Re: inkey hangs
Post by: markallyn on September 18, 2017, 01:10:51 AM
Hutch,

I'm going to work under that assumption.  Since printf macro works fine it will do as a substitute.

This question is off-topic.  I will post it to The Campus if you think I should do so.  Here goes.

I have been attempting to install the 64 bit version of masm32 on another Linux box that is 64 bits.  The unzipped version I was able to download came with a file buildx64 containing a makeall.bat.  I've run into problems doing the build, again using Wine.  Gets partway thru and asks me (Yes|No) if I want to overwrite the lib file in buildx64.  When I hit the Yes response it crashes. 

Is there a more recent version of the 64 bit package that has the lib/include files already built?

Thanks,
Mark
Title: Re: inkey hangs
Post by: dedndave on September 18, 2017, 03:01:22 AM
this program calls a function that uses ReadConsoleInput
it displays the values returned for any keystroke
to exit the program, press the Esc key
the function may be used in a similar way to inkey...
Title: Re: inkey hangs
Post by: hutch-- on September 18, 2017, 04:25:28 AM
Mark,

The problem here is I am developing 64 bit on Win 10 64 bit and have had to do a massive amount of testing to make it compatible with what works on Win 10. I check the apps on Win7 64 bit and have had no problems there but I am not willing to install Linux on a machine to further install Wine to test apps as I only have one life left and I don't have to time to learn a variant operating system to do the testing. I only undertake to make this 64 bit stuff so it works correctly on Win64 systems.
Title: Re: inkey hangs
Post by: nidud on September 18, 2017, 06:20:27 AM
I've tested this code on Linux Mint 64 using Wine.
Code: [Select]
include \masm32\include\masm32rt.inc

.code
start:
    inkey "Hello!"
    exit

    end start

Works fine using a win32 file-manger or right-click Open With->Wine Windows Program Loader to execute.

However, using the Linux terminal or the Linux MC file-manger fails.
Title: Re: inkey hangs
Post by: nidud on September 18, 2017, 06:55:42 AM
This works thought.

Code: [Select]
include \masm32\include\masm32rt.inc
.data
format db "hit any key to continue...",10,0
.code
start:
    invoke crt_printf,addr format
    invoke crt__getch
    invoke crt_exit,0
    end start
Title: Re: inkey hangs
Post by: markallyn on September 19, 2017, 01:48:36 AM
Good morning Hutch and Nidud,

Hutch:  Do you have a masm64 package (I'm happy with whatever runs on Win 7) that I can download that is fully "made" in the form in which masm32 is built?  If so, how may I download it?

nidud:  Your last sentence appears to be fully consistent with what I am experiencing.

Thanks, folks.

Mark
Title: Re: inkey hangs
Post by: markallyn on September 19, 2017, 01:54:20 AM
Hutch,

I forgot that 64 bit Win 7 probably doesn't exist!  My bad.

Mark
Title: Re: inkey hangs
Post by: hutch-- on September 19, 2017, 09:30:19 AM
You sound like someone out of the Unix world. Win7 64 bit Ultimate certainly exists and I have it on my last machine which still runs perfectly.

RE : 64 bit MASM, I do not have the right to distribute Microsoft 64 bit binaries and don't intend to drag through negotiation with Microsoft to try and get it. Your best chance is to use JWASM or UASM of Nidud's version as it may be better suited for the Linux platform you are using.
Title: Re: inkey hangs
Post by: dedndave on September 20, 2017, 04:17:12 AM
i am curious to know if this works

Code: [Select]
;***********************************************************************************************

AnyKey  PROC

;Wait for Any Console Key Press - DednDave
;version 1, 12-2011
;version 2, 2-2013
;
;  This function returns when any console key is pressed (bKeyDown = 1).
;A possible drawback is that all input event records are removed from
;the console input queue until a key is pressed. In many cases, this is
;not an issue. The virtual key code, virtual scan code, TCHAR character,
;and control key state values are returned in registers.

;Call With: Nothing
;
;  Returns: EAX = TCHAR character (high word of EAX = 0)
;           ECX = control key state flags
;               Bit   Name                Meaning
;                0    RIGHT_ALT_PRESSED   Right ALT key is pressed
;                1    LEFT_ALT_PRESSED    Left ALT key is pressed
;                2    RIGHT_CTRL_PRESSED  Right CTRL key is pressed
;                3    LEFT_CTRL_PRESSED   Left CTRL key is pressed
;                4    SHIFT_PRESSED       SHIFT key is pressed
;                5    NUMLOCK_ON          NUM LOCK light is on
;                6    SCROLLLOCK_ON       SCROLL LOCK light is on
;                7    CAPSLOCK_ON         CAPS LOCK light is on
;                8    ENHANCED_KEY        Key is enhanced
;           EDX:
;           low word (DX) = virtual key code
;               high word = virtual scan code
;
;           all other registers are preserved

;-------------------------------------------------

    LOCAL   ir          :INPUT_RECORD
    LOCAL   uRecCnt     :UINT
    LOCAL   hStdInp     :HANDLE

;-------------------------------------------------

    INVOKE  GetStdHandle,STD_INPUT_HANDLE
    mov     hStdInp,eax
    .repeat
        INVOKE  ReadConsoleInput,hStdInp,addr ir,1,addr uRecCnt
        movzx   edx,word ptr ir.EventType
    .until (edx==KEY_EVENT) && (edx==ir.KeyEvent.bKeyDown)
    movzx   eax,word ptr ir.KeyEvent.UnicodeChar
    mov     ecx,ir.KeyEvent.dwControlKeyState
    mov     edx,dword ptr ir.KeyEvent.wVirtualKeyCode
    ret

AnyKey  ENDP

;***********************************************************************************************
Title: Re: inkey hangs
Post by: nidud on September 20, 2017, 09:36:15 AM
i am curious to know if this works

Yes that works  :t