News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Maybe I'll Make It Bask To ASM This Time!

Started by Fred Harris, November 16, 2015, 03:49:08 AM

Previous topic - Next topic

Fred Harris

How does one add the asm equivalent of getchar() in C/C++ or Waitkey$ in PowerBASIC to keep a console window open?  I apologize for asking such a basic question.  I used to do DOS asm years back, but haven't touched it since. 

I have a several years old copy of Hutsch's masm32 on my xp box, and I found this program in the ...

\masm32\tutorial\console/demo1

...directory...


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    print chr$("Hey, this actually works.",13,10)
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends


It works, but only flashes by as it closes.

dedndave

more recent versions have an "inkey" macro

    ;; ------------------------------------------------------
    ;; display user defined text, default text or none if
    ;; NULL is specified and wait for a keystroke to continue
    ;; ------------------------------------------------------
    inkey MACRO user_text:VARARG
      IFDIF <user_text>,<NULL>                  ;; if user text not "NULL"
        IFNB <user_text>                        ;; if user text not blank
          print user_text                       ;; print user defined text
        ELSE                                    ;; else
          print "Press any key to continue ..." ;; print default text
        ENDIF
      ENDIF
      call wait_key
      print chr$(13,10)
    ENDM


the macro uses the wait_key function (also part of the masm32 library)

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\msvcrt.inc
    include \masm32\macros\macros.asm

    .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

wait_key proc

    invoke FlushConsoleInputBuffer, rv(GetStdHandle,STD_INPUT_HANDLE)

  @@:
    invoke Sleep, 1
    call crt__kbhit
    test eax, eax
    jz @B

    call crt__getch     ; recover the character in the keyboard
                        ; buffer and return it in EAX
    ret

wait_key endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end


wait_key uses crt__kbhit and crt__getch, both from the MSVCRT runtime library

your version may or may not have those - i suspect it does

Vortex

Hi Fred,

Welcome to the forum.

You need to run the console application in the command prompt :

D:\>cd \masm32\tutorial\console\demo1

D:\masm32\tutorial\console\demo1>hello.exe
Hey, this actually works.

D:\masm32\tutorial\console\demo1>



getchar() is a member of the C run-time library. You can call it from an assembly application :

\masm32\include\msvcrt.inc


Vortex

Hi Fred,

Here is a quick example :

.386
.model flat,stdcall
option casemap:none

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

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

.data

msg         db 'Press any key and hit RETURN',13,10,0
msg2        db 'You pressed %c before hitting RETURN',13,10,0

.code

start:

    invoke  crt_printf,ADDR msg

    invoke  crt_getchar

    invoke  crt_printf,ADDR msg2,eax

    invoke  ExitProcess,0

END start

hutch--

Hi Fred,

Be careful, we may lead you astray.  :biggrin:

Fred Harris

Thanks for the help Dedndave and Vortex.  That's exactly what I needed.  Believe I'll work on the msvcrt and win32 console functions a bit to try to get up to speed with things as they are now.

Wow, I'm really on a roll!  Look at the 'advanced' stuff I can do now thanks to your help...   :biggrin:


.386
.model flat,stdcall
option casemap:none

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

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

.data
msg1         db 'eax = %d',0

.code
start:
  mov eax, 12345
  invoke  crt_printf, ADDR msg1, eax
  invoke  crt_getchar
  invoke  ExitProcess, 0
END start


Don't know if I can be radicalized any worse than I already am Hutch.  This might be my last refuge!  :biggrin:

The date on my qeditor.exe is 10/29/2011.  I guess there's been updates since then?  If so, I'll install this week.  I do recall in the version I have Iczillion's GUI tutorials were a part of the installation, and I really liked them.

jj2007

Quote from: Fred Harris on November 16, 2015, 01:17:12 PMThe date on my qeditor.exe is 10/29/2011.  I guess there's been updates since then?

Looks like you've got the current version :t

hutch--

Fred,

There is a later version here, mainly minor mods and a bug fix or two. Get the second one.

http://masm32.com/board/index.php?topic=3514.0

Just overwrite the old one with this one.

dedndave

to simplify the typiing, many of us use masm32rt.inc...

        INCLUDE     \masm32\include\masm32rt.inc
        .686p

        .DATA

szMessage db 'Hello World!',0

        .CODE

main    PROC

    print   offset szMessage,13,10

    inkey
    exit

main    ENDP

        END     main


masm32rt.inc adds most of the commonly used inc's and lib's (open it and check it out)
it sets the processor to .486
to use a few newer instructions (like rdtsc or cpuid), use .586
to use later SSE versions, use .686p, .MMX, .XMM

we sometimes have need of additional inc's and lib's
an example might be advapi32.inc and lib (registry functions)
another is winmm (multi-media)

        INCLUDE    \masm32\include\masm32rt.inc
        .686p
        .MMX
        .XMM
        INCLUDE     \masm32\include\advapi32.inc
        INCLUDE     \masm32\include\winmm.inc
        INCLUDELIB  \masm32\lib\advapi32.lib
        INCLUDELIB  \masm32\lib\winmm.lib