Author Topic: 64 bit values  (Read 1991 times)

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
64 bit values
« 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? 
« Last Edit: March 10, 2022, 09:09:07 PM by Gunther »
You have to know the facts before you can distort them.

jj2007

  • Member
  • *****
  • Posts: 13633
  • Assembly is fun ;-)
    • MasmBasic
Re: 64 bit values
« Reply #1 on: March 10, 2022, 09:11:36 AM »
Pure Assembly, no CRT involved:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  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).
Code: [Select]
Your number: -1234567890123456789
The number:  -1234567890123456789 (a QWORD)

Source & exe attached, the *.asc opens in RichMasm, WordPad, MS Word etc.

fearless

  • Member
  • ****
  • Posts: 572
    • Github
Re: 64 bit values
« Reply #2 on: March 10, 2022, 03:20:58 PM »
fearless

ASUS Crosshair 8 Hero, AMD 5950X, 32GB, MSI 5700XT, NZXT Kraken Z73, Seasonic 1000W PSU

Github Twitter Mastodon Gitbook

TimoVJL

  • Member
  • *****
  • Posts: 1254
Re: 64 bit values
« Reply #3 on: March 10, 2022, 08:48:59 PM »
_strtoui64 ?
May the source be with you

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
Re: 64 bit values
« Reply #4 on: March 10, 2022, 09:29:32 PM »
_strtoui64 ?

Yes, you mean this one. But it's not inside msvcrt.inc.
You have to know the facts before you can distort them.

jj2007

  • Member
  • *****
  • Posts: 13633
  • Assembly is fun ;-)
    • MasmBasic
Re: 64 bit values
« Reply #5 on: March 10, 2022, 10:08:28 PM »
But it's not inside msvcrt.inc.

Code: [Select]
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

HSE

  • Member
  • *****
  • Posts: 2358
  • AMD 7-32 / i3 10-64
Re: 64 bit values
« Reply #6 on: March 10, 2022, 10:13:08 PM »
Code: [Select]
    invoke vc__strtoui64, addr szNumbers, addr pEnd, 10
    mov lli1, rax

Translation to Masm64 SDK from C++ strtoll example
Equations in Assembly: SmplMath

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
Re: 64 bit values
« Reply #7 on: March 10, 2022, 10:47:57 PM »
Code: [Select]
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.
You have to know the facts before you can distort them.

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
Re: 64 bit values
« Reply #8 on: March 10, 2022, 10:57:51 PM »
HSE,

Code: [Select]
    invoke vc__strtoui64, addr szNumbers, addr pEnd, 10
    mov lli1, rax

Translation to Masm64 SDK from C++ strtoll example

thank you. :thumbsup: That's a great help.
You have to know the facts before you can distort them.

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
Re: 64 bit values
« Reply #9 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.
You have to know the facts before you can distort them.

jj2007

  • Member
  • *****
  • Posts: 13633
  • Assembly is fun ;-)
    • MasmBasic
Re: 64 bit values
« Reply #10 on: March 11, 2022, 12:37:04 AM »
In 32-bit land:

include \masm32\MasmBasic\MasmBasic.inc         ; download
  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


Code: [Select]
Your number: 1234567890123456789
CRT: The number:  1234567890123456789 (a QWORD)
MB:  The number:  1234567890123456789 (a QWORD)

Use %llu (Str$(): %u) for unsigned QWORDs.

Gunther

  • Member
  • *****
  • Posts: 4145
  • Forgive your enemies, but never forget their names
Re: 64 bit values
« Reply #11 on: March 11, 2022, 02:20:47 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.
You have to know the facts before you can distort them.

deeR44

  • Member
  • **
  • Posts: 101
Re: 64 bit values
« Reply #12 on: July 22, 2022, 05:20:41 AM »
Quote
include \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.


jj2007

  • Member
  • *****
  • Posts: 13633
  • Assembly is fun ;-)
    • MasmBasic
Re: 64 bit values
« Reply #13 on: July 22, 2022, 07:25:16 AM »
Code: [Select]
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?

NoCforMe

  • Member
  • *****
  • Posts: 1043
Re: 64 bit values
« Reply #14 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.