News:

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

Get input and convert to string without macros?

Started by rob1218, November 23, 2011, 01:11:43 AM

Previous topic - Next topic

rob1218

I have been working on this for a few weeks and cant figure it out...   

I am trying to get a number as input from the user and convert it to a integer without using any macros or shortcuts..

I already have the working version with "sval" and have tried digging into the libraries and looking up what atol and sval does and using that code.. but have been unsuccessful..

Any help with this would be GREATLY appreciated..    As I feel i've hit a dead end in ASM and it's way too soon for that.  :/

Thanks for your time and help. 

qWord

As long as not writing your own OS, you must use Window's function for input and output: console functions
(or do you want to use a GUI for input?)

here a simple example how to read and convert a decimal number (example only!):
main proc
LOCAL hStdIn:HANDLE
LOCAL sz[128]:CHAR
LOCAL nCharRead:DWORD

invoke GetStdHandle,STD_INPUT_HANDLE
mov hStdIn,eax
invoke SetConsoleMode,hStdIn,ENABLE_LINE_INPUT or ENABLE_ECHO_INPUT
invoke ReadConsole,hStdIn,ADDR sz,LENGTHOF sz,ADDR nCharRead,0
xor ecx,ecx
xor edi,edi
xor esi,esi
.while ecx < LENGTHOF sz && ( sz[ecx] == 20h || sz[ecx] == 9 )
lea ecx,[ecx+1]
.endw

.if ecx < LENGTHOF sz && ( sz[ecx] == '+' || sz[ecx] == '-' )
.if sz[ecx] == '-'
mov esi,1
.endif
lea ecx,[ecx+1]
.endif
.while ecx < LENGTHOF sz &&  sz[ecx] >= '0' && sz[ecx] <= '9'
imul edi,edi,10
movzx eax,sz[ecx]
lea eax,[eax-'0']
lea edi,[edi+eax]
lea ecx,[ecx+1]
.endw
.if esi
neg edi
.endif
; edi = number
...

main endp

A simpler alternative would be the CRT functions (scanf,printf,...)
FPU in a trice: SmplMath
It's that simple!

rob1218

Thanks for the response! :)

Hmm.. whichever method is more simple.. I dont mind a gui but i assume that would be more difficult??  Am I right to assume that?

I am currently running all of my programs out of the cmd prompt...

I am just trying to accomplish this task as simply as possible without the use of any macros..

From what I understand user input is stored as a string correct?  So in order to perform any mathematical functions on it must be converted to an integer..

and than to print any results we have to convert it back to a string?


MichaelW

This is a simplified version of the Microsoft atol source from the PSDK, done in straightforward C with no macros or function calls, along with some test code.

//=============================================================================
#include <windows.h>
#include <conio.h>
#include <stdio.h>
//=============================================================================

long _atol(char *nptr)
{
    int c;              // current char
    long total;         // current total
    int sign;           // if '-' then negative, otherwise positive

    // skip leading whitespace

    while( (int)*nptr == 32 || (int)*nptr == 9 )
        ++nptr;

    c = (int)*nptr++;
    sign = c;                   // save sign
    if( c == '-' || c == '+' )
        c = (int)*nptr++;       // if char was sign, skip past it

    total = 0;

    while( c >= '0' && c <= '9' )
    {
        total = 10 * total + (c-0x30);  // accumulate digit values
        c = (int)*nptr++;               // get next char
    }

    // return result, negated if necessary

    if( sign == '-' )
        return -total;
    else
        return total;
}

int main(void)
{
    printf( "%d\n", _atol( "" ) );
    printf( "%d\n", _atol( " \t" ) );
    printf( "%d\n", _atol( "x" ) );
    printf( "%d\n", _atol( "1" ) );
    printf( "%d\n", _atol( "   \t\t1" ) );
    printf( "%d\n", _atol( "-1" ) );
    printf( "%d\n", _atol( "   \t\t-1" ) );
    printf( "%d\n", _atol( "123456789" ) );
    printf( "%d\n", _atol( "+123456789" ) );
    printf( "%d\n", _atol( " -123456789 123 " ) );

    getch();
    return 0;
}


0
0
0
1
1
-1
-1
123456789
123456789
-123456789

eschew obfuscation

rob1218

can you use c in masm like that?? are there certain libraries you have to include??

Here is an example of some NASM code that does something similar to what im looking for.. is there a name for writing code in this particular manner??  sorry for my ignorance in this area... :/

I am trying to write something similar to this but in MASM.. Thanks for your time


stiint   mov   [inAddr], eax   ; str. addr: eax
   mov   esi, [inAddr]   ;
   sub   eax, eax
   mov   cx, [decBase]
stiintNxt:
   sub   ebx, ebx
   mov   bl, [esi]   ; get next char
   cmp   bl, [newLine]   ; last char.?
   je   stoiDone   ; exit
   sub   bl, [asc0]   ; convert to digit
   mul   cx
   add   eax, ebx
   inc   esi
   jmp   stiintNxt

stiintDone:
   ret

MichaelW

Rob,

The C code that I posted does the conversion using the same basic algorithm as qWord's MASM code, but expressed in a language that should be much easier for you to read and understand. The expectation is that you will understand the algorithm well enough to implement it in assembly.
eschew obfuscation

rob1218

Ok thanks so much! im going to mess with that and see what i can come up with.