News:

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

getc and gets

Started by DanWebb314, April 16, 2012, 02:57:59 AM

Previous topic - Next topic

dedndave

my bad - i keep thinking this is masm32.com   :P

hutch--

I confess I don't see what the big deal is about, the services requested are provided by the OS and OS only, you can access those services through the C runtime functions, the C runtime DLL or just plain roll your own variant from the API. I would not advise a person writing in assembler to use the C runtime function as it sucks in all of the overhead associated with them, MSVCRT works OK allowing that getting character input from the console is hardly a demanding application and there is nothing stopping anyone from writing their own from the API.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

for almost everything else, i prefer to roll my own, as you say
because of the buggish behaviour of the console, i like the msvcrt for single keys
what mucks up the works is that the user can make a mess with the right-click context menu (paste, most notably)
the msvcrt seems to handle it as well as anything

i did write this routine using the API
it seems to work fairly well
;******************************************************************************

AnyKey  PROC

;Wait for Any Console Key Press - DednDave 12-2011
;
;  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

        push    ebx
        push    ebp
        sub     esp,(sizeof INPUT_RECORD+3) and -4
        mov     ebp,esp
        INVOKE  GetStdHandle,STD_INPUT_HANDLE
        xchg    eax,ebx
        push    ecx

AnyKy0: INVOKE  ReadConsoleInput,ebx,ebp,1,esp
        movzx   edx,word ptr [ebp].INPUT_RECORD.EventType
        cmp     edx,KEY_EVENT                             ;KEY_EVENT EQU 1
        jnz     AnyKy0

        cmp     edx,[ebp].INPUT_RECORD.KeyEvent.bKeyDown
        jnz     AnyKy0

        pop     ecx
        movzx   eax,word ptr [ebp].INPUT_RECORD.KeyEvent.UnicodeChar
        mov     edx,dword ptr [ebp].INPUT_RECORD.KeyEvent.wVirtualKeyCode
        mov     ecx,[ebp].INPUT_RECORD.KeyEvent.dwControlKeyState
        add     esp,(sizeof INPUT_RECORD+3) and -4
        pop     ebp
        pop     ebx
        ret

AnyKey  ENDP

;******************************************************************************

SteveAsm

Quote from: dedndave on April 17, 2012, 03:01:54 AM
my bad - ...  

No, not at all.  :clap:
I was just trying to answer his question in the same context in which he presented it.   :U

Quote
...i keep thinking this is masm32.com

I think this is the best forum around for the general discussion of x86 Assembly Language.
And, a noob probably has no knowledge of Masm32 and is wanting to learn Masm programming.  :clap:

You're the best Dave.  :U

dedndave

#19
nah - plenty of guys in here top me   :P

but - i would say that if you are a newbie to asm, particularly win32 asm, the masm32 package is the best way to start out
if you want to tackle the other packages or methods, it is best if you have some experience under your belt

DanWebb314

This work like a dream:

Quote from: jj2007 on April 16, 2012, 10:20:53 PM
include \masm32\include\masm32rt.inc

.code
start: print "press Return to exit", 13, 10
.Repeat
getkey
push eax
print esp
pop eax
.Until eax==13
exit

end start


It assembles without error.   What is 'esp'?  I can see that 'print esp' echos back the key you hit.

Next,  what is as simple as this but returns a string?

dedndave

        include \masm32\include\masm32rt.inc

        .code

start:  mov     edx,input("Enter a String: ")
        print   edx
        print   chr$(13,10)
        inkey
        exit

end start


i didn't test it, but it should work   :P

dedndave

sorry - i should have tested it, first
try this one...
        include \masm32\include\masm32rt.inc

        .code

start:  print   input("Enter a String: ")
        print   chr$(13,10)
        inkey
        exit

        end     start

DanWebb314

OK,  I jumped to quick.  esp is the the stack pointer.  so 'print esp'  sends the character you pushed with 'push eax'.  I guess that is all I need to know about 'print exp'


DanWebb314

About this code

Quote from: dedndave on April 18, 2012, 04:17:00 AM
sorry - i should have tested it, first
try this one...
        include \masm32\include\masm32rt.inc

        .code

start:  print   input("Enter a String: ")
        print   chr$(13,10)
        inkey
        exit

        end     start


I guess I will have to try it out to see where the string goes.

dedndave

 :bg

the "input" macro sets up the "Enter a String: " string in the .DATA section
the input buffer (where your typed characters go) may be on the stack, or it may be in the .DATA? section
you can look at the macro and see how it works
it is in the \masm32\macros\macros.asm file
search for "input macro"

dedndave

        getkey
        push  eax
        print esp
        pop   eax


in this code of Jochen's, ESP is the stack pointer register
the character is PUSH'ed onto the stack (followed by a 0), then it's displayed, then it's POP'ed back off the stack

DanWebb314

This code show me where the inputted string goes::

    mov ebx, input(" Input number: ")
    mov eax, [ebx]


Now 'eax' has the first 4 charters of the input string.

dedndave

well - i tried that in EDX
maybe i am not using the right register   :lol