flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > "Error: value out of range"

Author
Thread Post new topic Reply to topic
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla
When I enter this code in FASM, I get a "value out of range" error:

Code:
USE64
POP    R10                ;Pop the Alloc length off the stack.
AND    R10,0x7FFFFFFFF    ;Error:value out of range    


This is a code snippet from the file system of my 64-bit operating system. Strangely enough, 0x7FFFFFFFF is less than 2^35, which is obviously less than 2^64 and 2^63. I am using FASM v1.67.21
Post 26 May 2007, 10:39
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
64bit mode allows only signed 32bit immediates.

Only instruction that can take 64bit immediate is "mov rax, imm64"
Post 26 May 2007, 11:13
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Adam Kachwalla



Joined: 01 Apr 2006
Posts: 150
Adam Kachwalla
So I guess this should work right?

Code:
USE64
POP R10
MOV R9,0x7FFFFFFFF
AND R10,R9    
Post 26 May 2007, 11:24
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
no, it won't work with other register than "rax".

this will work:
Code:
use64
pop r10
mov rax, 0x7FFFFFFFF
and r10, rax    
Post 26 May 2007, 12:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
Xorpd!
Quote:

no, it won't work with other register than "rax".

I think you are confusing two separate issues. The "mov rax, moffs64" syntax only works with (absolute addressing), but mov r10, imm64 is a syntax that works with any of the 16 integer registers.
One of the big conveniences of x86, and by extension x86-64, is the ability to load an integer register with an immediate in a single instruction. Try doing that in the mips ISA for example and see how many instructions in takes!
Post 27 May 2007, 03:04
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Yes, its pretty hard. Here my try
Code:
; r0: Always contains 0
; r1: Reserved for assembler use (obviously fasmMIPS doesn't reserve it because it
;     doesn't have built-in macroinstruction such as LI)

macro li reg, imm
{
  if ~reg eqtype r0
    display "I said a register, BITCH!", 13, 10
    err
  else if reg eq r1
    display "Destination register is reserved for LI", 13, 10
    err
  else if imm >= 1 shl 32
    display "Immediate out of range", 13, 10
    err
  end if

  if (imm >= -1 shl 15) & (imm < 1 shl 15)
    addiu   reg, r0, imm
  else
    lui    reg, imm shr 16 and $FFFF
    ori    reg, imm and $FFFF
  end if
}

macro li64 reg, imm
{
  if ~reg eqtype r0
    display "I said a register, BITCH!", 13, 10
    err
  else if reg eq r1
    display "Destination register is reserved for LI", 13, 10
    err
  end if

  if imm >= 0
    if imm < 1 shl 15
      addiu   reg, r0, imm
    else if imm < 1 shl 31
      lui    reg, imm shr 16 and $FFFF
      ori    reg, imm and $FFFF
    end if
  else
    lui    r1, imm shr 48 and $FFFF
    lui    reg, imm shr 16 and $FFFF

    ori    r1, r1, imm shr 32 and $FFFF
    ori    reg, reg, imm and $FFFF

  ; WARNING: The processor must be properly initialized to prevent Reserved Instruction exception.
  ;          Obviously it must be a MIPS64 aswell...
    dsll32 reg, reg, 0
    dsll32 r1, r1, 0
    dsrl32 reg, reg, 0

    or     reg, r1, reg
  end if
}    

Note that I'm not a MIPS assembly programmer so possibly this is not the best way.

[EDIT] I corrected a sign extention related bug and improved the macros a little. I found the li64 ridiculously complex, any suggestion is welcomed.[/EDIT]
Post 27 May 2007, 04:14
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
xorpd: you are right

sorry for confusion
Post 27 May 2007, 07:09
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat
Since ANDing with 7FFFFFFFF only affects the high dword, you could also do it using a dword operation like this:
Code:
and   dword [rsp+4],7
pop  r10    
Post 27 May 2007, 17:26
View user's profile Send private message Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
Xorpd!
Quote:

Code:
and     dword [rsp+4],7 
pop     r10
    


While it's true that this approach has the effect you want, it's nightmare code that is thinking 32-bit in a 64-bit world. I have seen gcc generate code like this which is bad because it violates store forwarding.
Post 28 May 2007, 06:52
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.