Author Topic: Inconsistent assigning of negative constants  (Read 888 times)

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Inconsistent assigning of negative constants
« on: May 03, 2016, 09:00:20 PM »
Code: [Select]
include \masm32\include\masm32rt.inc ; plain Masm32

@signed$ MACRO arg ; returns signed immediate as string (borrowed from MasmBasic)
  if arg lt 0
EXITM @CatStr(<->, %(-arg))
  else
EXITM %arg
  endif
endm

  negnum=80000000h
  echo using 80000000h:
  if negnum lt 0
echo number is negative
  else
echo number is positive
  endif
  tmp$ CATSTR <  TheNumber=>, %negnum
  % echo tmp$
  tmp$ CATSTR <  TheNumber=>, @signed$(negnum)
  % echo tmp$

  echo

  negnum=-2147483648

  echo using -2147483648:
  if negnum lt 0
echo number is negative
  else
echo number is positive
  endif
  tmp$ CATSTR <  TheNumber=>, %negnum
  % echo tmp$
  tmp$ CATSTR <  TheNumber=>, @signed$(negnum)
  % echo tmp$

.code
start:
  exit
  .err <--- ok -->
end start

Output:

Code: [Select]
using 80000000h:
number is positive
TheNumber=2147483648
TheNumber=2147483648

using -2147483648:
number is negative
TheNumber=2147483648
TheNumber=-2147483648

IMHO 80000000h and -2147483648 should be, ehm, roughly the same ::)

qWord

  • Member
  • *****
  • Posts: 1454
  • The base type of a type is the type itself
    • SmplMath macros
Re: Inconsistent assigning of negative constants
« Reply #1 on: May 03, 2016, 10:00:39 PM »
MASM constants range from -4294967295 to 4294967295.

See https://sourceforge.net/p/jwasm/bugs/47/
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Re: Inconsistent assigning of negative constants
« Reply #2 on: May 03, 2016, 10:28:03 PM »
MASM constants range from -4294967295 to 4294967295.

Good to know, thanks :t
Quote
symbolic constants ( EQU and '=' ) do have 32+1 bits

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Inconsistent assigning of negative constants
« Reply #3 on: May 03, 2016, 10:58:41 PM »
There seems to be an inconsistence there:
Code: [Select]
cmp rax,0FFFFFFFFh ; imm32 - ok
        cmp rax,4294967295 ; imm32 - ok
cmp rax,4294967296 ; imm64 - "ok"
cmp rax,4294967297 ; imm64 - "ok"
cmp rax,4294967295 + 1 ; error A2084: constant value too large
cmp rax,100000000h    ; error A2084: constant value too large
cmp rax,0FFFFFFFFh + 1 ; error A2084: constant value too large

I notice this from the regress test of the switch, so it appears to be correct with hex numbers but not decimal.

EDIT: it actually works in JWASM
Code: [Select]
cmp rax,0FFFFFFFFh ; imm32 - ok
cmp rax,4294967295 ; imm32 - ok
cmp rax,4294967296 ; Error A2235: Constant value too large: 100000000h
cmp rax,4294967297 ; Error A2235: Constant value too large: 100000001h
cmp rax,4294967295 + 1 ; Error A2235: Constant value too large: 100000000h
cmp rax,100000000h    ; Error A2235: Constant value too large: 100000000h
cmp rax,0FFFFFFFFh + 1 ; Error A2235: Constant value too large: 100000000h

So this is a bug in ASMC  :P

jj2007

  • Member
  • *****
  • Posts: 7559
  • Assembler is fun ;-)
    • MasmBasic
Re: Inconsistent assigning of negative constants
« Reply #4 on: May 03, 2016, 11:45:10 PM »
> cmp   rax,4294967297      ; Error A2235: Constant value too large: 100000001h

I wonder, though, why the comparison of a "33-bit" number to a 64-bit register should fail ::)

nidud

  • Member
  • *****
  • Posts: 1371
    • https://github.com/nidud/asmc
Re: Inconsistent assigning of negative constants
« Reply #5 on: May 04, 2016, 12:40:17 AM »
It’s possible to compare r64 to r/m64 but not imm64. This a bit inconvenient for the switch. It is however possible to move imm64 to a register, and also other things, but not compare.

Well, I found the bug:
Code: [Select]
cmp edi,10
jae radix_10_QWORD ; ja --> jae

ALIGN 4
radix_10_DWORD: ; FFFFFFFF - 4294967295 = 10