您如何区分“ mov r/m64,imm32” VS“ MOV R/M32,IMM32”在MASM64中?
英特尔手册说mov
具有两个涉及内存和32位立即操作数的变体:
MOV r/m32, imm32
MOV r/m64, imm32
第一个副本复制了四个字节,第二个副本8,将给定的32位立即进行,并将其扩展到64位。
在为MASM64编写汇编代码时,您如何指定哪个?
The Intel manual says mov
has two variants involving both memory and 32-bit immediate operands:
MOV r/m32, imm32
MOV r/m64, imm32
The first one copies four bytes, the second copies eight, taking the given 32-bit immediate and sign-extending it to 64 bits.
How do you specify which one you want when writing assembly code for MASM64?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像往常一样覆盖内存操作数的大小。
通常,考虑设置内存操作数的大小,而不是即时,因为即使使用
movzx
或shl dword ptr [ mem],4
,其中操作数不必是相同的大小(换档计数为字节,可以是cl)。您不会对即时的任何事情覆盖;您仍然需要
添加QWORD PTR [MEM],4
以使用sign_extdendended_imm8编码,而编码将编码放在汇编程序中。 缺少来自原始8086的Alu指令的签名扩展字节表单。(只有
MOV
和test rcx],word 0x0001
,我永远不会那样写。特别是在NASM中,您可以强迫使用
添加QWORD [RDI]立即使用4字节,严格的QWORD 123
将在机器代码中使用4字节即可进行其他可能会重写。在没有严格
的情况下,使用dword
似乎是错误的思考方式。尤其是启用NASM的默认优化,因此您不需要
mov rax,dword -1
之类的内容来避免IMM64编码,或添加dword [rdi],byte 1
to to避免使用IMM32编码。 (使用NASM -O0
,如果您不建议使用非图片提示进行较小的编码,则这些 do 选择更长的编码,因此可以将大小放在即时上请与设置操作数 size。 >不是
严格的DWORD
,与直接宽度匹配操作数大小语法是在存在x86-64之前设计的,并且在如何将其扩展到非字节以外的非宽宽的情况下,犯了/或设计错误。Override the size of the memory operand, like always.
It makes sense in general to think about setting the size of the memory operand, not the immediate, because that always works, even with
movzx
orshl dword ptr [mem], 4
where the operands don't have to be the same size (the shift count is a byte and could be CL).You're not overriding anything about the immediate; you still want
add qword ptr [mem], 4
to use a sign_extended_imm8 encoding, leaving the encoding up to the assembler. (Onlymov
andtest
lack sign-extended byte forms, out of ALU instructions that come from original 8086.)Even for assemblers like NASM that do accept
add [rcx], word 0x0001
, I'd never write it that way.Especially in NASM where you can force it to use a 4-byte immediate with
add qword [rdi], strict qword 123
which would use a 4-byte immediate in the machine code that something else could rewrite. Usingdword
on an immediate withoutstrict
seems like the wrong way of thinking about it.Especially with NASM's default optimization enabled so you don't need stuff like
mov rax, dword -1
to avoid the imm64 encoding, oradd dword [rdi], byte 1
to avoid the imm32 encoding. (Withnasm -O0
, those do pick longer encodings if you didn't suggest the smaller encoding with a non-strict hint, so putting sizes on immediates is something you can do separately from setting the operand-size.)(NASM's a bit messy in requiring
strict qword 123
foradd qword [rdi], strict qword 123
, notstrict dword
, matching the operand-size instead of the immediate width. IMO it's a bug that it rejectsadd qword [rdi], strict dword 123
. Probably the syntax was designed before x86-64 existed, and/or design mistakes were made in how to extend it to cases where there are non-full-width immediates other than bytes.)@ECM上面的评论给出了答案:
@ecm's comment above gives the answer: