flat assembler
Message board for the users of flat assembler.

Index > Windows > [x64] Question about sign-extension

Author
Thread Post new topic Reply to topic
yoshimitsu



Joined: 07 Jul 2011
Posts: 96
yoshimitsu 07 Jan 2012, 10:17
Hello,
I don't quite get x64 operation encodings and how x86 and x64 operands interact.
Quote:
REX.W + C7 /0 | MOV r/m64,imm32 | Move imm32 sign extended to 64-bits to r/m64.
for example, how do I achieve such an instruction?
"mov rax,12345678h" gives "mov rax,0000000012345678h" and something like "mov rax,dword 12345678h" is invalid.
"mov qword [var],12345678h" writes a zero-extended value into var, as expected.
But "mov qword [var],87654321h" fails to assemble because of the value being out of range, although I was expecting sign-extension.
Am I misunderstanding this at all?
And which instructions do actually sign-extend?

Additionally, I've got another question in reference to LEA:
Is LEA encoded RIP-relative only if the ModR/M byte is 05, means just a disp32-value was provided?
And if e.g. a register is added, then the disp32-value is interpreted as an absolute value?
Thanks
Post 07 Jan 2012, 10:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
Location: Kraków, Poland
Tomasz Grysztar 07 Jan 2012, 12:08
yoshimitsu wrote:
"mov rax,12345678h" gives "mov rax,0000000012345678h" and something like "mov rax,dword 12345678h" is invalid.
"mov rax,12345678h" assembles for me to form with sign-extended 32-bit immediate (because it's shorter). What version of fasm have you used?

yoshimitsu wrote:
But "mov qword [var],87654321h" fails to assemble because of the value being out of range, although I was expecting sign-extension.
Am I misunderstanding this at all?

When you write assembly language instruction, it focuses on what the instruction has to do, not how is it going to be encoded. So if you write what value you want to put into qword variable, you should really specify the value that you want to put there and not the value that you expect to get in the encoding. So the correct form would be:
Code:
mov qword [var],0FFFFFFFF87654321h    
or
Code:
mov qword [var],-789ABCDFh    


yoshimitsu wrote:
Additionally, I've got another question in reference to LEA:
Is LEA encoded RIP-relative only if the ModR/M byte is 05, means just a disp32-value was provided?
And if e.g. a register is added, then the disp32-value is interpreted as an absolute value?
Yes.
Post 07 Jan 2012, 12:08
View user's profile Send private message Visit poster's website Reply with quote
yoshimitsu



Joined: 07 Jul 2011
Posts: 96
yoshimitsu 07 Jan 2012, 18:44
Sorry, I did not use "mov rax,12345678h", but "mov rax,87654321h" in which case you can't take the sign-extended 32-bit immediate, because it'd have a false result which wouldn't match the actual instruction.
Tomasz Grysztar wrote:
[...] you should really specify the value that you want to put there and not the value that you expect to get in the encoding [...]
That's a point, I was wondering, though, because the intel docs is talking about a 32-bit immediate and 0FFFFFFFF87654321h wouldn't fit this.
So FASM throws "value out of range" because "87654321h" isn't explicitly negative?
Post 07 Jan 2012, 18:44
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.