News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

need help to manipulate 64-bit range numbers in masm32

Started by shaikkareem, March 27, 2014, 10:23:16 PM

Previous topic - Next topic

qWord

Even if the FPU internally can represent 64 bit unsigned integers (QWORD), the load and store instructions support only singed integers.
.data
    qw QWORD 7fffffffffffffffh
.code
finit
fld1
fild qw
fscale  ; st = st*2
fistp qw ; invalid operation (masked) -> qw = 8000000000000000h = -9223372036854775808
fstp st
MREAL macros - when you need floating point arithmetic while assembling!

MichaelW

Quote from: qWord on March 28, 2014, 06:20:32 PM
Even if the FPU internally can represent 64 bit unsigned integers (QWORD), the load and store instructions support only singed integers.

Yes, I had forgotten that, tested by unmasking the interrupt. I think the problem could be corrected by prefixing the operand with SQWORD PTR, but unfortunately ML 6.15 apparently does not recognize SQWORD.

;==============================================================================
include \masm32\include\masm32rt.inc
.686
;==============================================================================

;-----------------------------------------
; Values for the FPU interrupt mask bits.
;-----------------------------------------

FIM_INVALID      equ 1
FIM_DENORMALIZED equ 2
FIM_ZERODIVIDE   equ 4
FIM_OVERFLOW     equ 8
FIM_UNDERFLOW    equ 16
FIM_PRECISION    equ 32
FIM_ANY          equ 63

;--------------------------------------------------------
; This macro selectively clears the FPU interrupt masks.
; Example usage: FCLEARIM FIM_ZERODIVIDE or FIM_OVERFLOW
;--------------------------------------------------------

FCLEARIM MACRO maskbits
    push eax
    fstcw [esp]
    pop eax
    and ax, NOT (maskbits)
    push eax
    fldcw [esp]
    pop eax
ENDM

;==============================================================================
.data
    qw QWORD 7fffffffffffffffh
.code
;==============================================================================
start:
;==============================================================================

    finit
    FCLEARIM FIM_INVALID
    inkey
    fld1
    fild qw
    fscale  ; st = st*2
    fistp qw ; invalid operation (masked) -> qw = 8000000000000000h = -9223372036854775808
    fstp st

    inkey
    exit
;==============================================================================
end start

#define STATUS_FLOAT_DENORMAL_OPERAND  ((NTSTATUS)0xC000008DL)
#define STATUS_FLOAT_DIVIDE_BY_ZERO    ((NTSTATUS)0xC000008EL)
#define STATUS_FLOAT_INEXACT_RESULT    ((NTSTATUS)0xC000008FL)
#define STATUS_FLOAT_INVALID_OPERATION ((NTSTATUS)0xC0000090L)
#define STATUS_FLOAT_OVERFLOW          ((NTSTATUS)0xC0000091L)
#define STATUS_FLOAT_STACK_CHECK       ((NTSTATUS)0xC0000092L)
#define STATUS_FLOAT_UNDERFLOW         ((NTSTATUS)0xC0000093L)

Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Quote from: MichaelW on March 29, 2014, 12:34:02 AMI think the problem could be corrected by prefixing the operand with SQWORD PTR, but unfortunately ML 6.15 apparently does not recognize SQWORD.

Jwasm and higher Masm versions do recognise SQWORD, but that doesn't change the encoding.

Str$() and printf() can handle 8000000000000000h, the latter can even print it signed. Another question is how useful that is - positive qwords are useful for file sizes etc, but who needs a negative QWORD?

KeepingRealBusy


jj2007

Quote from: KeepingRealBusy on March 29, 2014, 02:13:11 AM
Bignum functions need unsigned.

Dave,

I had a suspicion that somebody would claim it's important ;-)

It was easier than I thought:

include \masm32\MasmBasic\MasmBasic.inc      ; download
.data
qwPos      dq 7fffffffffffffffh      ; highest positive number
qwNeg      dq 8000000000000000h      ; lowest negative number
  Init
  deb 4, "MasmBasic deb macro - see also Str$():", x:qwPos, s:qwPos, u:qwPos, x:qwNeg, s:qwNeg, u:qwNeg
  Print CrLf$, "CRT printf():"
  printf("\nx:qwPos\t\t%I64X\ns:qwPos\t\t%I64i\nu:qwPos\t\t%I64u", qwPos, qwPos, qwPos)
  printf("\nx:qwNeg\t\t%I64X\ns:qwNeg\t\t%I64i\nu:qwNeg\t\t%I64u", qwNeg, qwNeg, qwNeg)
  Exit
end start


Output (identical for MB + CRT):
x:qwPos         7FFFFFFF FFFFFFFF
s:qwPos         9223372036854775807
u:qwPos         9223372036854775807
x:qwNeg         80000000 00000000
s:qwNeg         -9223372036854775808
u:qwNeg         9223372036854775808


SetupMasmBasic27March14q.zip is needed 8)

dedndave

Quote from: KeepingRealBusy on March 29, 2014, 02:13:11 AM
Bignum functions need unsigned.

wouldn't that depend on the bignum library being used ?
it seems to me that unsigned functions are also available
sure - for crypto stuff, unsigned seems logical, but not for math