News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to input an integer from keyboard ???

Started by m7xtuf, February 10, 2010, 07:20:18 PM

Previous topic - Next topic

dedndave

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)

jj2007

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.

Magicle


hutch--

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

m7xtuf

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

dedndave

#20
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

Magicle

Damnit Dave, that link was quite useful! Thanks.  :U

dedndave

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

hutch--

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