The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: m7xtuf on February 10, 2010, 07:20:18 PM

Title: how to input an integer from keyboard ???
Post by: m7xtuf on February 10, 2010, 07:20:18 PM
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

   

   
Title: Re: how to input an integer from keyboard ???
Post by: Neil on February 10, 2010, 07:28:33 PM
Have a look at the getkey macro in macros.asm
Title: Re: how to input an integer from keyboard ???
Post by: m7xtuf on February 10, 2010, 07:34:59 PM
Where can I get that file "masm.asm" ???
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 07:37:31 PM
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\ ]
Title: Re: how to input an integer from keyboard ???
Post by: m7xtuf on February 10, 2010, 07:38:16 PM
Did I need to include any library to use those macros ???  I try to assemble and I have a link error ...

Thanx
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 07:39:23 PM
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.
Title: Re: how to input an integer from keyboard ???
Post by: Neil on February 10, 2010, 07:39:53 PM
include \masm32\macros\macros.asm
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 07:41:50 PM
Just like Neil said.
If you get additional errors, make sure you post them here and we'll do our best to help.  :U
Title: Re: how to input an integer from keyboard ???
Post by: m7xtuf on February 10, 2010, 07:43:47 PM
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




Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 07:45:59 PM
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.
Title: Re: how to input an integer from keyboard ???
Post by: 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
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 07:51:16 PM
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.
Title: Re: how to input an integer from keyboard ???
Post by: Neil on February 10, 2010, 07:53:52 PM
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
Title: Re: how to input an integer from keyboard ???
Post by: m7xtuf on February 10, 2010, 07:57:12 PM
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
Title: Re: how to input an integer from keyboard ???
Post by: oex on February 10, 2010, 08:17:09 PM
mov eax, sval(input("Enter Number: "))
print str$(eax)
Title: Re: how to input an integer from keyboard ???
Post by: dedndave on February 10, 2010, 08:32:07 PM
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)
Title: Re: how to input an integer from keyboard ???
Post by: jj2007 on February 10, 2010, 08:33:11 PM
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.
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 10, 2010, 11:16:36 PM
What's the "13,10...." after inkey for ?
Title: Re: how to input an integer from keyboard ???
Post by: hutch-- on February 10, 2010, 11:19:47 PM
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.
Title: Re: how to input an integer from keyboard ???
Post by: m7xtuf on February 11, 2010, 12:37:39 AM
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
Title: Re: how to input an integer from keyboard ???
Post by: dedndave on February 11, 2010, 01:08:38 AM
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
Title: Re: how to input an integer from keyboard ???
Post by: Magicle on February 11, 2010, 01:26:11 AM
Damnit Dave, that link was quite useful! Thanks.  :U
Title: Re: how to input an integer from keyboard ???
Post by: dedndave on February 11, 2010, 01:33:39 AM
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
Title: Re: how to input an integer from keyboard ???
Post by: hutch-- on February 11, 2010, 03:12:40 AM
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.