The MASM Forum

General => The Campus => Topic started by: Gunther on March 10, 2022, 09:00:00 AM

Title: 64 bit values
Post by: Gunther on March 10, 2022, 09:00:00 AM
I want to read in a number from the keyboard and then assign it to an unsigned 64 bit integer. For this I used vc_scanf to write the ASCII characters into a buffer.
To convert that to an unsigned integer, vc_strtoll would have to be used, but it does not exist in msvcrt.inc; there is only vc_strtol. The same is with integers; there
is vc_atol, but not vc_atoll.

Has anyone ever done it this way? Are there other solutions for this? 
Title: Re: 64 bit values
Post by: jj2007 on March 10, 2022, 09:11:36 AM
Pure Assembly, no CRT involved:

include \masm32\MasmBasic\MasmBasic.inc         ; download (http://masm32.com/board/index.php?topic=94.0)
  SetGlobals MyNumber:QWORD
  Init
  Let esi="123456789012345678"
  .While 1
        Let esi=Input$("Your number: ", esi)
        .Break .if !Len(esi)
        MovVal MyNumber, esi
        Print Str$("The number:  %i (a QWORD)\n", MyNumber)
  .Endw
EndOfCode


One minor flaw: it prints a signed QWORD (you may try %u instead of %i).
Your number: -1234567890123456789
The number:  -1234567890123456789 (a QWORD)


Source & exe attached, the *.asc opens in RichMasm, WordPad, MS Word etc.
Title: Re: 64 bit values
Post by: fearless on March 10, 2022, 03:20:58 PM
utoa_ex: https://masm32.com/board/index.php?topic=4174.msg60108#msg60108
Title: Re: 64 bit values
Post by: TimoVJL on March 10, 2022, 08:48:59 PM
_strtoui64 ?
Title: Re: 64 bit values
Post by: Gunther on March 10, 2022, 09:29:32 PM
Quote from: TimoVJL on March 10, 2022, 08:48:59 PM
_strtoui64 ?

Yes, you mean this one (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtoui64-wcstoui64-strtoui64-l-wcstoui64-l?view=msvc-170). But it's not inside msvcrt.inc.
Title: Re: 64 bit values
Post by: jj2007 on March 10, 2022, 10:08:28 PM
Quote from: Gunther on March 10, 2022, 09:29:32 PMBut it's not inside msvcrt.inc.

include \masm32\include\masm32rt.inc
.code
start:
  .if rv(LoadLibrary, "C:\Windows\System32\msvcrt.dll")
.if rv(GetProcAddress, eax, "_strtoui64")
print hex$(eax), ": there it is"
.endif
  .endif
  inkey " - bye"
  exit
end start
Title: Re: 64 bit values
Post by: HSE on March 10, 2022, 10:13:08 PM
    invoke vc__strtoui64, addr szNumbers, addr pEnd, 10
    mov lli1, rax


Translation to Masm64 SDK from C++ strtoll example (https://www.cplusplus.com/reference/cstdlib/strtoll/)
Title: Re: 64 bit values
Post by: Gunther on March 10, 2022, 10:47:57 PM
Quote from: jj2007 on March 10, 2022, 10:08:28 PM
include \masm32\include\masm32rt.inc
.code
start:
  .if rv(LoadLibrary, "C:\Windows\System32\msvcrt.dll")
.if rv(GetProcAddress, eax, "_strtoui64")
print hex$(eax), ": there it is"
.endif
  .endif
  inkey " - bye"
  exit
end start


OK. It wasn't included in my msvcrtl.inc. So I made an installation update and now the editor finds the function. Thank you, it was my mistake.
Title: Re: 64 bit values
Post by: Gunther on March 10, 2022, 10:57:51 PM
HSE,

Quote from: HSE on March 10, 2022, 10:13:08 PM
    invoke vc__strtoui64, addr szNumbers, addr pEnd, 10
    mov lli1, rax


Translation to Masm64 SDK from C++ strtoll example (https://www.cplusplus.com/reference/cstdlib/strtoll/)

thank you. :thumbsup: That's a great help.
Title: Re: 64 bit values
Post by: Gunther on March 10, 2022, 11:56:58 PM
HSE,

vc__strtoui64 is a C++ function, but it works well. Probably the keyboard input doesn't work as I thought. This scanf never does what it is supposed to. I had problems with
it once before, years ago. Unfortunately, I can't remember how I solved it back then and I can't find the file anymore. Maybe it's in some obscure subdirectory or is on an old
computer, who knows? I'm now trying to solve this step by step.
Title: Re: 64 bit values
Post by: jj2007 on March 11, 2022, 12:37:04 AM
In 32-bit land:

include \masm32\MasmBasic\MasmBasic.inc         ; download (http://masm32.com/board/index.php?topic=94.0)
  SetGlobals MyNumber:QWORD
  Init

  Dll "Msvcrt"
  Declare _strtoui64, C:3             ; cdecl, three args expected

  Let esi="1234567890123456789"
  .While 1
        Let esi=Input$("Your number: ", esi)
        .Break .if !Len(esi)
        mov dword ptr MyNumber, _strtoui64(esi, 0, 10)  ; CRT
        mov dword ptr MyNumber[4], edx
        printf("CRT: The number:  %lli (a QWORD)\n", MyNumber)
        MovVal MyNumber, esi            ; MasmBasic
        Print Str$("MB:  The number:  %i (a QWORD)\n\n", MyNumber)
  .Endw
EndOfCode


Your number: 1234567890123456789
CRT: The number:  1234567890123456789 (a QWORD)
MB:  The number:  1234567890123456789 (a QWORD)


Use %llu (Str$(): %u) for unsigned QWORDs.
Title: Re: 64 bit values
Post by: Gunther on March 11, 2022, 02:20:47 AM
Quote from: jj2007 on March 11, 2022, 12:37:04 AM
In 32-bit land:

That's the problem. For principle reasons I want to use 64 bit unsigned integers. In the 64 bit world this corresponds exactly to the native CPU register width.
Of course you can also do this with 32 or 16 bit applications; however, that' s more cumbersome.

I've solved the problem with vc_scanf and can now focus on the algorithm again.
Title: Re: 64 bit values
Post by: deeR44 on July 22, 2022, 05:20:41 AM
Quoteinclude \masm32\MasmBasic\MasmBasic.inc         ; download

Thank you, but I have not seen nor heard anything about "Basic" since the 1970's.
How it can have anything to do with Microsoft's Macro Assembler is beyond me.

Title: Re: 64 bit values
Post by: jj2007 on July 22, 2022, 07:25:16 AM
include \masm32\include\masm32rt.inc ; pure Masm32 SDK code (© Hutch)

.code
start:
  print "Hello World", 13, 10 ; BASIC
  mov esi, chr$("Even this is BASIC") ; BASIC
  .if find$(1, esi, "BASIC") ; BASIC
lea eax, [esi+eax-1]
print eax, " is the match", 13, 10 ; BASIC
  .endif
  inkey "hit any key" ; BASIC
  exit
end start


Btw 63 posts now, and only one has traces of own code... when will you start showing us your MASM skills, deer?
Title: Re: 64 bit values
Post by: NoCforMe on July 22, 2022, 07:26:45 AM
You don't understand: JJ's MasmBasic is a set of extensions to MASM that allow you to do all sorts of wonderful things while staying in the world of assembler (it's really not BASIC). It's great stuff. However, I only know about it by reputation; still not interested in actually using it. You might try it, though.

It's his labor of love. Freely available.
Title: Re: 64 bit values
Post by: jj2007 on July 22, 2022, 07:29:45 AM
Thanks, NoCforMe. The point is that Deer44 criticises others a lot because they dare to post here snippets in MasmBasic, C/C++ and other languages but never ever has made a contribution to this forum. Check his posts in his profile, it's really ugly.
Title: Re: 64 bit values
Post by: NoCforMe on July 22, 2022, 08:24:22 AM
Maybe he's just saving himself for the right time ... (ASS-UM-ing he's a he)
Title: Re: 64 bit values
Post by: deeR44 on July 22, 2022, 12:14:36 PM
Sorry I offended anybody. I'll avoid any comments regarding "masmbasic" in the future.
I won't metion cpp or "c++" either.

I'm learning C now, but I'll keep it to myself.

Title: Re: 64 bit values
Post by: NoCforMe on July 22, 2022, 01:57:40 PM
I think you majorly misinterpreted the objections to your postings here. Nobody's suggesting you not[/] post anything referring to other languages. People do that all the time here. The objection (from JJ) was exactly the opposite of that. Maybe you ought to reread what he wrote.
Title: Re: 64 bit values
Post by: deeR44 on July 22, 2022, 02:17:38 PM
Oh, I think I'll probably be posting a lot of questions regarding C! I don't know squat about it.
Title: Re: 64 bit values
Post by: deeR44 on July 22, 2022, 02:21:52 PM
By the way, I am a 'he'.
I think that's one of the acceptable pronouns.
Title: Re: 64 bit values
Post by: NoCforMe on July 22, 2022, 02:29:32 PM
Quote from: deeR44 on July 22, 2022, 02:17:38 PM
Oh, I think I'll probably be posting a lot of questions regarding C! I don't know squat about it.

I think of C as a just a slightly more compact way of [almost] writing assembler. Some C translates almost 1-to-1 to assembly. You'll see that once you start looking at source code.

And if you use macros (I don't), you can make your MASM almost look like C. (I.e.,without all those JMPs that are the only way to transfer control in MASM.)

I don't mind those JMPs.
Title: Re: 64 bit values
Post by: hutch-- on July 22, 2022, 02:35:10 PM
Guys,

Lets keep the criticism of other people down, while this IS an assembler forum, many folks here write other languages and sometimes interface their other language with assembler.