The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: goo5257 on September 09, 2008, 01:18:44 PM

Title: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 09, 2008, 01:18:44 PM
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..
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 09, 2008, 02:04:20 PM
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
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 10, 2008, 01:40:08 PM
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...

Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 10, 2008, 04:18:44 PM
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.

Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 10, 2008, 04:53:16 PM
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...
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 10, 2008, 07:12:44 PM
AL returns an Ascii character, so you have to put inverted commas around the character being tested.

                         case "1"

                         case "2"
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 11, 2008, 01:37:50 AM
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
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: GregL on September 11, 2008, 03:05:24 AM
goo5257,

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

Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 11, 2008, 07:51:24 AM
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.
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: hutch-- on September 11, 2008, 08:05:55 AM
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 ...
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 11, 2008, 08:12:33 AM
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
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: BlackVortex on September 11, 2008, 08:47:40 AM
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
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: hutch-- on September 11, 2008, 08:57:46 AM

print "Howdy",13,10


the terminating zero is automatically appended.
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: BlackVortex on September 11, 2008, 09:08:05 AM
Quote from: hutch-- on September 11, 2008, 08:57:46 AM

print "Howdy",13,10


the terminating zero is automatically appended.
Oh, nice !

/me runs off to clear up his sources
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 11, 2008, 01:03:45 PM
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?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: BlackVortex on September 11, 2008, 01:40:58 PM
Quote from: goo5257 on September 11, 2008, 01:03:45 PM
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?
That was just hutch's example to show that you don't need to use the "and" operand.
The value is in eax, after getkey
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 11, 2008, 02:17:38 PM
why when press space bar then it will print out 20h?

20h (32 decimal) is the code returned in AL when the spacebar is pressed, if you press any other key it will return the code for that key. Look up the Ascii codes for the keyboard.
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 11, 2008, 02:26:19 PM
oh...ok.
I understand d...
Then, what can i do if i want to print out the value stored inside the register?
As when i use getkey, the register can store the value i had entered into eax register..
But, the problem is i cannot c the value i had typed.
So, how?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 11, 2008, 02:39:03 PM
Use Hutch's example:-

    getkey

    print hex$(eax),13,10    ;prints out the value in eax in hexadecimal

here you need to insert inkey or another getkey else your program will exit immediately

    ret
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 11, 2008, 03:06:02 PM
                getkey
      mov choice,eax
      print hex$(eax)
      ret

If i do like this, the program stuck there...
It wont continue run again?
why?
how i wan to display the number in decimal?
not hexadecimal...
To make sure that after display, the program is stil running...
What m i going to do?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: hutch-- on September 11, 2008, 03:37:37 PM
use any of "str$(), ustr$() or sstr$, the latter two give you the choice between signed and unsigned values. You should append the "13,10" at the ned like the previous examples so the cursor ends up in the right place for the next line. If not the next print will follow on the same line.
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 12, 2008, 01:03:54 AM
Now the problem is when i press number 2 for example,
it wil print out ASCII code and not the true decimal number...
Is it possible for me to print out the decimal number...
1 more problem is after i press d number,
my program cannot run d..
It stuck there...
Why is this happens?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Rainstorm on September 12, 2008, 02:57:59 AM
QuoteNow the problem is when i press number 2 for example,
it wil print out ASCII code and not the true decimal number...
ustr$(dword_value_here) should do that
it converts the dword value into a string

like
   

mov ebx, 2
print ustr$(ebx),13,10      ;  will display 2


QuoteTo make sure that after display, the program is stil running...
What m i going to do?

put
; your display code here
inkey
exit

that might help, since.. inkey will make it pause after the display.. till you press another key
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 12, 2008, 06:12:07 AM
Ya...
If straight move the value into the eax.
print ustr$(eax) will display the number.
But, now my condition is i get the number from user input which means that using getkey macro ,when i press button 2 ...
it will store d value into eax.
But what it store is ASCII code of the button 2.
So, when i use print ustr$(eax) to display, it will print out the ASCII code oni which is 50.
But not the decimal number 2 that user had input.
So, what do i need to do to make it can print out the exactly decimal number that had been input by the user.
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Neil on September 12, 2008, 06:50:29 AM
If you are just using numbers < 10, to get the decimal equivalent of ascii  just do:-


                               sub eax,48
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 12, 2008, 07:08:15 AM
What can i do if i want to print out everythg that i had input?
I means when i press "2" it will show "2", while i press "a" it will show "a".
I think getkey macro is not suitable for this purpose rite?
As getkey oni can read the ASCII code from the keyboard then store it into eax.
But it cannot print out exactly what i had press.
Is there other way to input?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 12, 2008, 07:22:39 AM
                invoke locate,15,14
      getkey
      mov choice,eax
      sub eax,48
      print ustr$(eax)
      mov     control, sval(input()) ; receive signed input
      jmp tocontinue
      ret

I had solve part of my problem d.
I had to jump out then oni ret..
o else my program will stuck there .
Even i sub eax with 48, it only can print out the number  from 1-10...
How about if i press "a"?
It will print out number but wot print out alphabet "a".
How i solve this?
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: Rainstorm on September 12, 2008, 08:00:22 AM
goo5257, maybe you could try this then (with console assemble & link)
will display the number or letter you typed

    .data

     temp  dd  0
; -----------------------------------------------------------------------
    .code

start:

    print "type any letter...",13,10
   
    getkey                 ;  get the key you type

    mov temp, eax          ;  store it in a temp location
    print "this letter was typed : "   
    print addr temp,13,10  ;  display the letter you typed earlier

    inkey                  ;  pause till you press a key then exit

    exit

    end start
Title: Re: TOPIC: How to input character and store it into a variable?
Post by: goo5257 on September 12, 2008, 09:07:40 AM
THANKS Rainstorm...
Ur code is working...
This is much more easier... :thumbu