如何从汇编中的地址加载单个字节

发布于 2025-02-08 16:16:43 字数 78 浏览 1 评论 0原文

如何从地址加载单个字节?我认为这是这样的:

mov      rax, byte[rdi]

How can I load a single byte from address? I thought it would be something like this:

mov      rax, byte[rdi]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吾家有女初长成 2025-02-15 16:16:43
mov al, [rdi]

将一个字节合并到RAX的低字节中。


或更好的是,通过将零扩展到32位寄存器(和隐含于64位), movzx

movzx  eax, byte [rdi]       ; most efficient way to load one byte on modern x86

或者如果要签名 - extension到更宽的寄存器中,请使用 movsx

movsx  eax, byte [rdi]    ; sign extend to 32-bit, zero-extend to 64
movsx  rax, byte [rdi]    ; sign extend to 64-bit

(在某些CPU上,MOVSX与Movzx一样有效,即使在负载端口中处理,甚至不需要Alu Uop。 uops.info


。代码>字节带有byte ptr

A MOV加载不需要大小指示符(al目标>目标>代码>字节 operand-size)。 movzx始终为内存源做,因为32位目标不会在8个与16位内存源之间消除歧义。

movzbl(%rdi),%eax(用movzb指定我们零扩展一个字节,l指定32位目标大小。)

mov al, [rdi]

Merge a byte into the low byte of RAX.


Or better, avoid a false dependency on the old value of RAX by zero-extending into a 32-bit register (and thus implicitly to 64 bits) with MOVZX:

movzx  eax, byte [rdi]       ; most efficient way to load one byte on modern x86

Or if you want sign-extension into a wider register, use MOVSX.

movsx  eax, byte [rdi]    ; sign extend to 32-bit, zero-extend to 64
movsx  rax, byte [rdi]    ; sign extend to 64-bit

(On some CPUs, MOVSX is just as efficient as MOVZX, handled right in a load port without even needing an ALU uop. https://uops.info. But there are some where MOVZX loads are cheaper than MOVSX, so prefer MOVZX if you don't care about the upper bytes and really just want to avoid partial-register shenanigans.)


The MASM equivalent replaces byte with byte ptr.

A mov load doesn't need a size specifier (al destination implies byte operand-size). movzx always does for a memory source because a 32-bit destination doesn't disambiguate between 8 vs. 16-bit memory sources.

The AT&T equivalent is movzbl (%rdi), %eax (with movzb specifying that we zero-extend a byte, the l specifying 32-bit destination size.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文