我可以用 OR 操作代替 MOV 操作嗎? (Can I substitute a MOV operation with an OR operation?)


問題描述

我可以用 OR 操作代替 MOV 操作嗎? (Can I substitute a MOV operation with an OR operation?)

首先,我想說我是 ASM 新手,如果這是一個愚蠢的問題,請原諒。

我在 Agner Fog 的微架構手冊 關於部分寄存器停頓(這似乎有點高級,但我很好奇為什麼 64 位中的 32 位指令位模式將寄存器的上半部分歸零)。例 6.13 給出瞭如何避免寄存器停頓的解決方案。我對此仍然有些困惑,為什麼不使用 OR 操作而不是 MOV,例如:

xor eax, eax
mov al, byte [mem8]
; or  al, byte [mem8] ; why not this?

我認為效果是一樣的。它們每秒都使用相同數量的周期嗎?一個比另一個更有效嗎?有什麼“在引擎蓋下”嗎?


參考解法

方法 1:

Partial register access in 64‑bit mode

In 64‑bit mode, the following rules apply when accessing registers with less than 64‑bit:

  • If a 32‑bit register is accessed, the upper 32 bits of the associated 64‑bit register are cleared
  • If a 16‑ or 8‑bit register is accessed, the upper 48 or 56 bits of the associated 64‑bit register remain.

  • </ul>

    If only an 8‑bit register is accessed, the old value of the associated 64‑bit register must first be obtained, the 8‑bit sub‑register changed and then the new value saved.

    Example 6.13 from Agner Fog's microarchitecture manual is not related to this, it is only an alternative to movzx, because this instruction is slow on older pentium processors.

    mov or or?

    The two lines

    31 C0                   xor eax, eax
    8A 05 ## ## ## ##       mov al, byte [mem8]
    

    (opcodes on the left) are probably faster than if you replaced the second line with

    0A 05 ## ## ## ##       or  al, byte [mem8]
    

    since there is a depency to the previous line: Only when xor eax, eax has been calculated the new value in eax can be passed on to or. In addition, just as with the variant with mov, there may be a slowdown because only a partial register is accessed. Instead, I would suggest replacing these two lines with

    0F B6 05 ## ## ## ##    movzx eax, byte [mem8]
    

    This is one byte shorter than the previous approach and also just a single instruction that accesses a full 32‑bit register. As Agner Fog said

    The easiest way to avoid partial register stalls is to always use full registers and use MOVZX or MOVSX when reading from smaller memory operands.

    (by Alex Ghfcdt)

    參考文件

    1. Can I substitute a MOV operation with an OR operation? (CC BY‑SA 2.5/3.0/4.0)

#assembly #intel-syntax #x86






相關問題

內聯彙編 - cdecl 和準備堆棧 (Inline assembly - cdecl and preparing the stack)

ASM 使用代碼查找偏移量 (ASM find offset with code)

顯示字符的 Ascii 值 (Displaying Ascii values of characters)

x86 彙編程序崩潰,可能忽略了簡單的錯誤 (x86 assembly procedure crashes, possibly overlooking simple error)

是否可以在 C++ 結構中編寫靜態程序集數據? (Is it possible to write static assembly data in C++ struct?)

將小於 %ecx 的 1 推入堆棧 (push 1 less than %ecx to stack)

emu8086 : ARR 不包含任何值 (emu8086 : ARR do not contain any value)

劊子手游戲,語法修復 x86 gcc 程序集和 C (hangman game, syntax fix x86 gcc assembly & C)

如何將內存中的 96 位加載到 XMM 寄存器中? (How to load 96 bits from memory into an XMM register?)

如何使用 Assembly 中的 printf? (How do you use printf from Assembly?)

摩托羅拉 68HC12 實時中斷寄存器 (Motorola 68HC12 Real Time Interrupt Registers)

我可以用 OR 操作代替 MOV 操作嗎? (Can I substitute a MOV operation with an OR operation?)







留言討論