flat assembler
Message board for the users of flat assembler.

Index > Main > 64 bit immediate values

Author
Thread Post new topic Reply to topic
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom
Hi, why does this code assemble:

Code:
format ELF64

section '.text' executable

add rax, 0x7fffffff
ret
    


But this code doesn't:

Code:
format ELF64

section '.text' executable

add rax, 0x80000000
ret
    


It seems it won't allow any immediate value which has the sign bit set? But why? I read this here http://www.x86-64.org/documentation/assembly.html

Quote:

Immediates

Immediate values inside instructions remain 32 bits and their value is sign extended to 64 bits before calculation. This means that:

addq $1, %rax # Valid instruction
addq $0x7fffffff, %rax # As this
addq $0xffffffffffffffff, %rax # as this one
addq $0xffffffff, %rax # Invalid instruction
addl $0xffffffff, %eax # Valid instruction


But I still don't understand why? Could someone explain please. Thanks in advance.
Post 11 Jul 2011, 03:30
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Immediate values are stored in a signed 32 bit format within the instruction encoding. You can't store 0x0000000080000000 because that would need 33 bits minimum.

You can encode values from 0xffffffff80000000 to 0xffffffffffffffff, and 0x0000000000000000 to 0x000000007fffffff

The sign bit of the 32 bit immediate is copied into the high order 32 bits of the 64 bit number.
Post 11 Jul 2011, 03:39
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom
Thanks revolution, I think I get it now. Smile
Post 11 Jul 2011, 04:23
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
When I didn't understand it, I made a graph on paper to visualize it.
It looked something like:
xxxx...............................yyyy
where xxxx are positive (32-bit) integers and yyyy are negative.
You cannot encode values that are too large or too small. So about -4 billion to +4 billion are okay, but others values should be MOVed to register first or read from memory.
Post 11 Jul 2011, 07:48
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger 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.