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

BlackVortex

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

Neil

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.

goo5257

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?

Neil

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

goo5257

                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?

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

goo5257

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?

Rainstorm

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

goo5257

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.

Neil

If you are just using numbers < 10, to get the decimal equivalent of ascii  just do:-


                               sub eax,48

goo5257

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?

goo5257

                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?

Rainstorm

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

goo5257

THANKS Rainstorm...
Ur code is working...
This is much more easier... :thumbu