The MASM Forum

Projects => MASM32 => Topic started by: nidud on December 14, 2012, 10:31:07 PM

Title: atol
Post by: nidud on December 14, 2012, 10:31:07 PM
deleted
Title: Re: atol
Post by: hutch-- on December 14, 2012, 11:52:42 PM
The "atol" algorithm does what it is documented to do and that does not include the additional test you have used. If you have additional criteria, write a new algo. The example string you have cited contains other characters than ascii numbers and the plus and minus signs, thats why it does not perform the task you have designed.
Title: Re: atol
Post by: nidud on December 15, 2012, 12:45:15 AM
deleted
Title: Re: atol
Post by: hutch-- on December 15, 2012, 01:03:27 AM
Same problem, wrong reference, in the masm32 library atol is documented in the help file and it does what it is documented to do. Noting that no-one own a particular name, in the masm32 library the atol algorithm is provided both in source code and with its own reference material. If you want to write or use ANSI C standards, use ANSI C.

If alternately you want to use the C runtime library in masm, you can call either the MSVCRT version or the LIBC version, the latter will bloat your code.

You must have added the LIBC library to your masm installation as it is not part of the masm32 distribution. What is provided in masm32 is an import library for the original MSVCRT so you can use the C runtime functions. Due to name clashes the MSVCRT include file prepend crt_ to the function names.
Title: Re: atol
Post by: jj2007 on December 15, 2012, 01:16:14 AM
Same "problem" for Val(). Apart from "linguistic" disputes, there is an advantage in atol or Val() returning an error for exotic formats - the potential damage from the occasional typo is higher than the extra effort to care for a known format like hh:mm:ss.

include \masm32\MasmBasic\MasmBasic.inc        ; download (http://masm32.com/board/index.php?topic=94.0)
        Init
        Inkey Str$("Result=%i", Val("01:11:34"))        ; returns -127 in eax and edx, ERROR
        Exit
end start

C++ documentation:

atol: (http://en.cppreference.com/w/c/string/byte/atoi)
Function discards any whitespace characters until first non-whitespace character is found.
Then it takes as many characters as possible to form a valid integer number representation
and converts them to integer value. The valid integer value consists of the following parts:
    (optional) plus or minus sign
    numeric digits
Title: Re: atol
Post by: MichaelW on December 15, 2012, 01:33:02 AM
Quote from: jj2007 on December 15, 2012, 01:16:14 AM
Same "problem" for Val(). Apart from "linguistic" disputes, there is an advantage in atol or Val() returning an error for exotic formats - the potential damage from the occasional typo is higher than the extra effort to care for a known format like hh:mm:ss.

I agree, the designers did put enough effort into this detail. The special case should require extra code, not the typical case.
Title: Re: atol
Post by: Adamanteus on December 15, 2012, 02:03:59 AM
This extra code is in strtol, strtoul by standard. I myself solved problem with leading zeros and non digits signs in past by num2str function, that is in K-Lib demo now available on my site.
Title: Re: atol
Post by: jj2007 on December 15, 2012, 02:30:26 AM
Workarounds are usually so simple that a tailored function is kind of an overkill...

include \masm32\MasmBasic\MasmBasic.inc                ; download (http://masm32.com/board/index.php?topic=94.0)
        Init
        Let esi="12:34:56"                             ; hh:mm:ss
        Print Str$("Result=%i\n", Val(esi))            ; returns -127 in eax and edx, ERROR
        Inkey Str$("Result=%i", Val(Left$(esi, 2)))    ; returns 12 in eax and 2 digits used in edx
        Exit
end start


Quote from: hutch-- on December 15, 2012, 01:03:27 AM
Noting that no-one own a particular name, ...

You don't know German judges, Hutch. They decided today that BMW owns the letter M.
(I know asm32 looks ugly, but can you afford a bunch of lawyers?  ::) )
Title: Re: atol
Post by: dedndave on December 15, 2012, 02:54:21 AM
an issue that noone has mentioned...
if you modify your LIB version of atol, then your masm32 library is no longer compatible with other forum members
that means that examples posted by others may not behave properly on your machine
and it means that code you post may not behave properly on others

from time to time, we do modify the library functions
but that is to fix bugs, not to alter the intended purpose of a function
Title: Re: atol
Post by: hutch-- on December 18, 2012, 09:20:39 AM
This is the bit I fail to understand, most languages overlap others somewhere, C runtime has some function names that clash with assembler mnemonics and to use them in assembler you must change the name. When it comes to function names, the masm32 library named functions with indifference to how other languages name functions and importantly, it DOCUMENTS what each function does. I am like most people, I hate writing documentation and I did not do it for fun, I wrote the documentation for exactly the reason that a function needs to be predictable.

Various computer programming languages make assumptions about their primacy over other languages yet for the life of MASM, 1981 onwards it has been a language in its own right and has never needed the assumptions of other languages. A common process when designing libraries is consistency, once you have written a function and documented it, you NEVER change it or you break someone elses code and introduce a very hard to fix bug.

It has always been the case that if you need a similar but different function, you write it, get it reliable then name it uniquely so it is not confused with other functions.
Title: Re: atol
Post by: nidud on December 19, 2012, 12:41:47 AM
deleted
Title: Re: atol
Post by: hutch-- on December 19, 2012, 01:12:00 AM
All you are arguing here is to NOT use the documentation for a function but to assume that it conforms to some arbitrary standard that you have in mind. Try using C runtime functions that have naming clashes with assembler directives and mnemonics and you will get the idea very quickly that you assumptions about the primacy of C over other languages is nonsense.
Title: Re: atol
Post by: Adamanteus on December 19, 2012, 09:41:22 AM
I'm thinking that C runtime solving such problem by underlines before x-systems functions : underliner for system runtime functions, if it's and system's function one more underline and deepness is mesured only by maximum name size. So, if you need to call assembler mnemonic function simple add underline to it's name - but runtime of course could be prepared for such use by global labels before function names.