TOPIC: How to input character and store it into a variable?

Started by goo5257, September 09, 2008, 01:18:44 PM

Previous topic - Next topic

goo5257

Hi...I m newbie for masm32.
So, can anyone please help me to counter my problem?
Here is my code...

.data

      control      dd      1
      intro      db      "intro.wav",0   ;wave file name
      choice      dd      ?   
      
    .code
   
    main:
   
start:
      call ClearScreen               ;clear screen

      invoke  PlaySound,addr intro,NULL,SND_FILENAME+SND_ASYNC

      invoke locate,22,7
      invoke Sleep, 500               ;delay 0.5 second
      print chr$("====================================")           
      invoke locate,27,8
      print chr$("ASSIGNMENT 3 : RECITATION")                        ; show title
      invoke locate,22,9
      print chr$("====================================")
   
   
      invoke locate,7,12
      invoke Sleep ,5000               ;delay 5 second
      print chr$("( 1 ) English")
      invoke locate,33,12
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 2 ) 华语")
      invoke locate,56,12
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 3 ) Bahasa Melayu")
      invoke locate,0,14
      invoke Sleep ,3000               ;delay 3 second
      print chr$("Your choice is ")
      invoke StdIn,addr choice,6

   
      .if choice==1
      print chr$("Welcome to www.recitation.com")
      jmp begin

      .elseif choice==2
      print chr$("欢迎来到 www.recitation.com")
      jmp begin

      .elseif choice==3
      print chr$("Selamat datang ke www.recitation.com")
      jmp begin

      .else
      print chr$("Please enter again...")
      invoke Sleep ,2000               ;delay 2 second
      jmp start
      .endif


begin:
      invoke locate,0,24
      mov     control, sval(input()) ; receive signed input

      exit
      
   end main

Is it my code correct for the input?
It has some problem..
I m not sure of the function StdIn
can anyone please post some tutorial on how to make the code for input and output?
Thanks..

Neil

Instead of using StdIn use getkey Macro :-


keypress: getkey
              and eax,255

SWITCH eax

            case "1"
            do something
            case "2"
            do something
            case "3"
            do something
            case "4"
           do something

DEFAULT
           jmp keypress

ENDSW

goo5257

Em...is it still got other method?
I had try to use the macro. But, it seem to be got error...
can you show it clearly how to use the getkey macro?
as this macro doesn't have any argument right?
I not really understand what you mean that "and eax,255"?
can you explain more deeply?
thankx you very much...


Neil

The getkey macro returns a key in the AL register, by anding eax with 255  (FFh) this clears to zero the high 24 bits of the register, this is just a precaution and may not be needed.

case "1" is the same as .if al=="1"

so in your code it would be  case "1"
                                        print chr$("Welcome to www.recitation.com")
                                         jmp begin
                                         case "2"
                                         print chr$("欢迎来到 www.recitation.com")
                                         jmp begin
                                         etc.
                                         etc.


goo5257

I ade follow what u had teach me...But it seems stil got problems..

      invoke locate,7,12
      invoke Sleep ,1000               ;delay 1 second
      print chr$("( 1 ) English")
      invoke locate,33,12
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 2 ) 华语")
      invoke locate,56,12
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 3 ) Bahasa Melayu")
      invoke locate,0,14
      invoke Sleep ,3000               ;delay 3 second
      print chr$("Your choice is ")
      ;invoke StdIn,addr choice,6
keypress:
      getkey
      ;and eax,255

      SWITCH eax

case 1
      call ClearScreen               ;clear screen
      invoke Sleep ,500               ;delay 0.5 second
      invoke  PlaySound,addr english,NULL,SND_FILENAME+SND_ASYNC
      invoke locate,22,6
      print chr$("*****                                ")
      invoke locate,22,7
      print chr$("***                                 *")
      invoke locate,22,8
      print chr$("**  Welcome to www.recitation.com  **")
      invoke locate,22,9
      print chr$("*                                 ***")
      invoke locate,22,10
      print chr$("                                *****")
      invoke locate,15,13
      invoke Sleep ,5000               ;delay 5 second
      print chr$("( 1 ) Quadratic Equation")
      invoke locate,53,13
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 2 ) Delay")


      ;invoke locate,30,20
      ;invoke Sleep ,1000               ;delay 1 second
      ;print chr$("Please enter again...")
      ;invoke  PlaySound,addr englishend,NULL,SND_FILENAME+SND_ASYNC
      ;invoke Sleep ,5000               ;delay 5 second

      jmp begin

case 2
      call ClearScreen               ;clear screen
      invoke Sleep ,500               ;delay 0.5 second
      invoke  PlaySound,addr chinese,NULL,SND_FILENAME+SND_ASYNC
      invoke locate,21,6
      invoke Sleep ,500               ;delay 0.5 second
      print chr$("*****                                ")
      invoke locate,21,7
      print chr$("***                                 *")
      invoke locate,21,8
      print chr$("**   欢迎来到 www.recitation.com   **")
      invoke locate,21,9
      print chr$("*                                 ***")
      invoke locate,21,10
      print chr$("                                *****")
      invoke locate,18,13
      invoke Sleep ,5000               ;delay 5 second
      print chr$("( 1 ) 数学方程式")
      invoke locate,50,13
      invoke Sleep ,3000               ;delay 3 second
      print chr$(" ( 2 ) 拖延")

      ;invoke locate,30,20
      ;invoke Sleep ,1000               ;delay 1 second
      ;print chr$("Please enter again...")
      ;invoke  PlaySound,addr chineseend,NULL,SND_FILENAME+SND_ASYNC
      ;invoke Sleep ,5000               ;delay 5 second

      jmp begin

case 3
      call ClearScreen               ;clear screen
      invoke  PlaySound,addr malay,NULL,SND_FILENAME+SND_ASYNC
      invoke locate,18,6
      invoke Sleep ,500               ;delay 0.5 second
      print chr$("*****                                       ")
      invoke locate,18,7
      print chr$("***                                        *")
      invoke locate,18,8
      print chr$("**  Selamat datang ke www.recitation.com  **")
      invoke locate,18,9
      print chr$("*                                        ***")
      invoke locate,18,10
      print chr$("                                       *****")
      invoke locate,15,13
      invoke Sleep ,4500               ;delay 4.5 second
      print chr$("( 1 ) Masalah quadratik")
      invoke locate,53,13
      invoke Sleep ,3000               ;delay 3 second
      print chr$("( 2 ) Tunda")

      ;invoke locate,30,20
      ;invoke Sleep ,1000               ;delay 1 second
      ;print chr$("Please enter again...")
      ;invoke  PlaySound,addr malayend,NULL,SND_FILENAME+SND_ASYNC
      ;invoke Sleep ,5000               ;delay 5 second

      jmp begin

DEFAULT
      invoke locate,30,20
      invoke Sleep ,1000               ;delay 1 second
      print chr$("Please enter again...")
      invoke  PlaySound,addr englishend,NULL,SND_FILENAME+SND_ASYNC
      invoke Sleep ,6000               ;delay 5 second
      jmp keypress
      
ENDSW


After i assemble,
it say got unresolved external symbol_imp_getch referenced in the function _ret_key@0
Got 1 unresolved externals

Is it the getkey macro got error?
Or i had done wrong at some where else?
Please correct me...

Neil

AL returns an Ascii character, so you have to put inverted commas around the character being tested.

                         case "1"

                         case "2"

goo5257

After i assemble,
it say got unresolved external symbol_imp_getch referenced in the function _ret_key@0
Got 1 unresolved externals

I think it is not the problem u had thought as i ade change the code following to your lesson.
Here is retkey.asm..
Is it got any problem here?
As i cant even use the getkey macro...Inside the macro,it call this retkey function...
So,when i use getkey macro, it prompts those errors such as above...got unresolved externals.
How to solve this?

; ?

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

comment * ----------------------------------
    This algorithm was designed by Greg Lyon
    -------------------------------------- *

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

    .code

; ?

ret_key proc

    invoke FlushConsoleInputBuffer,rv(GetStdHandle,STD_INPUT_HANDLE)

    call crt__getch
    xor ecx, ecx        ; zero ecx for non extended key return value
    test eax, eax
    jz @F
    cmp eax, 0E0h
    jnz quit
  @@:
    call crt__getch
    mov ecx, 1          ; return 1 in ECX if an extended key is pressed
  quit:
    ret

ret_key endp

; ?

end

GregL

goo5257,

You need to add 'INCLUDELIB \masm32\lib\msvcrt.lib' to your code, or include msvcrt.lib on the linker command-line.


Neil

goo5257,

As Greg says you must have the correct includes on your command line. Another way of doing this is to use queditor, click on 'code' & from the drop down menu click on 'Bare Console Template', you can then insert your own code & data.

hutch--

You don't need to prepare EAX before or after the getkey macro.


    mov eax, 66666666h

    getkey

    print hex$(eax),13,10

    ret


Press the space bar and you get 20h, the rest of the register in filled with zero.


00000020
Press any key to continue ...
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Neil

Thanks Hutch,
I wasn't sure what the hi bits of eax contained that's why I cleared them as a precaution, I can now get rid of that statement in my code  :thumbu

BlackVortex

Just wanna take this opportunity to say that the commandline macros are really useful.

Offtopic question not worthy of a new thread : What is the best way to make the print macro do a newline at the end ?
Now I'm using :

   print "PEB patching skipped"
   invoke StdOut, offset newline

with newline in the data as db 13,10,0

hutch--


print "Howdy",13,10


the terminating zero is automatically appended.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

BlackVortex

Quote from: hutch-- on September 11, 2008, 08:57:46 AM

print "Howdy",13,10


the terminating zero is automatically appended.
Oh, nice !

* BlackVortex runs off to clear up his sources

goo5257

THANX A LOT everyone...
i had solve my problem...
But i not understand the code below:

mov eax, 66666666h

    getkey

    print hex$(eax),13,10

    ret

why when press space bar then it will print out 20h?

If i want to print out the value inside the register,
what can i do?