您如何区分“ mov r/m64,imm32” VS“ MOV R/M32,IMM32”在MASM64中?

发布于 2025-02-04 18:51:18 字数 193 浏览 2 评论 0原文

英特尔手册说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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能否归途做我良人 2025-02-11 18:51:19

像往常一样覆盖内存操作数的大小。

mov   qword ptr [rdx], 123
add   byte ptr [rcx], 1
movzx eax,  word ptr [r8]
shl   dword ptr [r9], cl

通常,考虑设置内存操作数的大小,而不是即时,因为即使使用movzxshl dword ptr [ mem],4,其中操作数不必是相同的大小(换档计数为字节,可以是cl)。

您不会对即时的任何事情覆盖;您仍然需要添加QWORD PTR [MEM],4以使用sign_extdendended_imm8编码,而编码将编码放在汇编程序中。 缺少来自原始8086的Alu指令的签名扩展字节表单。


(只有MOVtest 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.

mov   qword ptr [rdx], 123
add   byte ptr [rcx], 1
movzx eax,  word ptr [r8]
shl   dword ptr [r9], cl

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 or shl 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. (Only mov and test 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. Using dword on an immediate without strict 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, or add dword [rdi], byte 1 to avoid the imm32 encoding. (With nasm -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 for add qword [rdi], strict qword 123, not strict dword, matching the operand-size instead of the immediate width. IMO it's a bug that it rejects add 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.)

怀念你的温柔 2025-02-11 18:51:18

@ECM上面的评论给出了答案:

mov qword ptr [rsp+08h], 0   ; selects MOV r/m64, imm32
mov dword ptr [rsp+08h], 0   ; selects MOV r/m32, imm32

@ecm's comment above gives the answer:

mov qword ptr [rsp+08h], 0   ; selects MOV r/m64, imm32
mov dword ptr [rsp+08h], 0   ; selects MOV r/m32, imm32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文