Author Topic: atol  (Read 12180 times)

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
atol
« on: December 14, 2012, 10:31:07 PM »
deleted
« Last Edit: February 25, 2022, 06:00:47 AM by nidud »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10026
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: atol
« Reply #1 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: atol
« Reply #2 on: December 15, 2012, 12:45:15 AM »
deleted
« Last Edit: February 25, 2022, 06:00:10 AM by nidud »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10026
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: atol
« Reply #3 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 13335
  • Assembly is fun ;-)
    • MasmBasic
Re: atol
« Reply #4 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
        Init
        Inkey Str$("Result=%i", Val("01:11:34"))        ; returns -127 in eax and edx, ERROR
        Exit
end start

C++ documentation:

atol:
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

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1196
Re: atol
« Reply #5 on: December 15, 2012, 01:33:02 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.
Well Microsoft, here’s another nice mess you’ve gotten us into.

Adamanteus

  • Member
  • **
  • Posts: 249
    • LLC "AMS"
Re: atol
« Reply #6 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.

jj2007

  • Member
  • *****
  • Posts: 13335
  • Assembly is fun ;-)
    • MasmBasic
Re: atol
« Reply #7 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
        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


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?  ::) )

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: atol
« Reply #8 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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10026
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: atol
« Reply #9 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

nidud

  • Member
  • *****
  • Posts: 2388
    • https://github.com/nidud/asmc
Re: atol
« Reply #10 on: December 19, 2012, 12:41:47 AM »
deleted
« Last Edit: February 25, 2022, 05:56:51 AM by nidud »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 10026
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: atol
« Reply #11 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Adamanteus

  • Member
  • **
  • Posts: 249
    • LLC "AMS"
Re: atol
« Reply #12 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.