News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

64 bit values

Started by Gunther, March 10, 2022, 09:00:00 AM

Previous topic - Next topic

Gunther

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? 
You have to know the facts before you can distort them.

jj2007

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).
Your number: -1234567890123456789
The number:  -1234567890123456789 (a QWORD)


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

fearless

utoa_ex: https://masm32.com/board/index.php?topic=4174.msg60108#msg60108

TimoVJL

May the source be with you

Gunther

You have to know the facts before you can distort them.

jj2007

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

HSE

    invoke vc__strtoui64, addr szNumbers, addr pEnd, 10
    mov lli1, rax


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

Gunther

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.
You have to know the facts before you can distort them.

Gunther

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

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

Gunther

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

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


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


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

Gunther

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.
You have to know the facts before you can distort them.

deeR44

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.


jj2007

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

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.
Assembly language programming should be fun. That's why I do it.