Hi there,
I am looking for a function to get input from keyboard ...
I know "invoke crt__getch" which I can read a "key" to AX ... but I want a function which get an integer to AX ... (i.e. enter a number then hit ENTER).
Thanx for all your wonderful help so far
Have a look at the getkey macro in macros.asm
Where can I get that file "masm.asm" ???
Quote from: m7xtuf on February 10, 2010, 07:34:59 PM
Where can I get that file "masm.asm" ???
Ctrl+F (Search for macros.asm) on your \masm32\ folder. :wink
[Or just go in \masm32\macros\ ]
Did I need to include any library to use those macros ??? I try to assemble and I have a link error ...
Thanx
Well, did you '#include' it?
In order to use already defined functions from other files, you have the necessity to include them in the header of the program.
include \masm32\macros\macros.asm
Just like Neil said.
If you get additional errors, make sure you post them here and we'll do our best to help. :U
Sorry I must be very stupid ... I am new to assembly programming ...
I include the file at the top "include \masm32\macros\macros.asm" but I got a lot of error when assembling ...
e.g. undefined symbol ret_key
this is what I put at the top
include \masm32\include\msvcrt.inc
includelib \masm32\lib\msvcrt.lib
include \masm32\macros\macros.asm
First of all, don't beat yourself up. Everyone has to start somewhere and even I am new to assembly, so no worries - everyone has to learn. :wink
You should check the parameters in the actual macros.asm file.
Open it up and search for 'getkey' , you will see the parameters needed for the function.
If that's not the case, please post the output you get in cmd.
I assume you are programming a console application. Use these headers :-
include \masm32\include\masm32rt.inc
include \masm32\macros\macros.asm
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
Ah, just saw this is getkey:
;; ---------------------------------------------------
;; wait for a keystroke and return its scancode in EAX
;; ---------------------------------------------------
getkey MACRO
call ret_key
ENDM
So I see no reason why that would give errors itself (Unless something is erroneous in your code).
Try what Neil wrote.
Once you get it to work you can do something like this :-
keypress:
getkey ;wait for & get keypress
SWITCH eax
case "1"
jmp somewhere or do something
case "2"
jmp somewhere or do something
case "3"
jmp somewhere or do something
case "4"
jmp somewhere or do something
case "5"
jmp somewhere or do something
case "6"
jmp somewhere or do something
case "7"
jmp somewhere or do something
DEFAULT
jmp keypress
ENDSW
Thanx for all the help ...
I can assemble now with the "getkey" macro, but I need to read a number to AX directly.
Now I can only read in ONE character at a time ... I want a integer input ...
Is there any library I can use using "invoke" ???
Thanx
mov eax, sval(input("Enter Number: "))
print str$(eax)
using the AX register would limit the range of signed integers to (-32768 to +32767)
16-bit unsigned integers can be (0 to 65535)
by using EAX, the range is (-2147483648 to +2147483647)
32-bit unsigned integers can be (0 to 4294967295)
Quote from: Neil on February 10, 2010, 07:49:43 PM
I assume you are programming a console application. Use these headers :-
include \masm32\include\masm32rt.inc
include \masm32\macros\macros.asm
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\kernel32.lib
masm32rt contains everything you need. For details, look at the file itself.
This is a complete app:
include \masm32\include\masm32rt.inc
.code
start:
mov eax, sval(input("Enter Number: "))
inkey str$(eax), 13, 10, "Press any key"
exit
end start
Make sure that you use console assembly, otherwise
input does not work.
What's the "13,10...." after inkey for ?
m7xtuf,
Keep this in mind, all input from the keyboard is in characters and/or symbols used for punctuation, you cannot directly enter a number from the keyboard. What you must do is get the character(s) and convert them t thye number they represent.
Thanx a lot a lot ...
inkey str$(eax), 13, 10, "Press any key"
With your code, instead of using 32-bit EAX. Is there a version for 16-bit register. I would like to use AX instead of EAX. With this, I can just do a shift left (shl EAX, 16) then a shift right (shr EAX, 16) to clear the upper 16-bit value, but I always want to print a 16-bit. Is there a way to type-cast to 16-bit ???
Thanx
well - you can test the value to see if the high word is non-zero
if the high word is 0, they have entered a value that will fit into 16 bits
also, there will be no need to 0 the high word because it will already be 0
test for unsigned 16-bit overflow:
cmp eax,10000h
jae unsigned_value_is_larger_than_16_bits
test for signed 16-bit overflow
this also sign-extends the value in AX into EAX
mov edx,eax
cwde
cmp eax,edx
jnz signed_value_is_larger_than_16_bits
also - if you did want to 0 the high word, there is an easier way....
movzx eax,ax
http://www1.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_6/CH06-1.html
Damnit Dave, that link was quite useful! Thanks. :U
sorry for all the edits - lol
there is a little error on that page
when you click on the 6.4.1 link, it doesn't take you to the instructions
click on 6.4 instead
m7xtuf,
As Dave mentioned, you don't need to type cast a register to get parts of it, you can get a WORD 16 bit value just by reading AX instead of EAX. Dave has shown you how to test for a number larger than 16 bit will support (65536) and as long as the nuber is smaller than that you can just read from the AX register or as Dave has explained zero extend it using MOVZX.