Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does xchg work in Intel Assembly Language

Tags:

x86

assembly

can someone explain how does the xchg work in this code? Given that arrayD is an DWORD array of 1,2,3.

mov eax, arrayD ; eax=1
xchg eax, [arrayD+4]; eax=2 arrayD=2,1,3

Why isn't the array 1,1,3 after the xchg?

like image 381
Alloysius Goh Avatar asked Apr 30 '18 14:04

Alloysius Goh


People also ask

What does Xchg do in assembly?

Microprocessor 8085 In 8085 Instruction set, there is one mnemonic XCHG, which stands for eXCHanGe. This is an instruction to exchange contents of HL register pair with DE register pair.

What is Xchg instruction in assembly language?

The XCH instruction loads the accumulator with the byte value of the specified operand while simultaneously storing the previous contents of the accumulator in the specified operand.

What does this instruction mean Xchg AX BX?

xchg ax, bx ; Put AX in BX and BX in AX xchg memory, ax ; Put "memory" in AX and AX in "memory" xchg mem1, mem2 ; Illegal, can't exchange memory locations! The rules for operands in the XCHG instruction are the same as those for the MOV instruction... ... except that XCHG does not accept immediate operands.

What does Xchg ax ax do?

Actually, xchg ax,ax is just how MS disassembles "66 90". 66 is the operand size override, so it supposedly operates on ax instead of eax . However, the CPU still executes it as a nop. The 66 prefix is used here to make the instruction two bytes in size, usually for alignment purposes.

What is XCHG instruction in microprocessor?

The XCHG (exchange data) instruction exchanges the contents of two operands. There are three variants: XCHG reg, reg XCHG reg, mem XCHG mem, reg.

What is the output of XCHG Ah CL instruction?

Then XCHG AH, CL exchanges the most significant bits of AH with lower bits of CL. The final output becomes: Just like MOV instruction, the XCHG instruction does not modify the contents of flag register.

Is it possible to swap XCHG and EAX in one instruction?

xchg only stores one element, and it can't magically look back in time to know where the value in eax came from and swap two memory locations with one xchg instruction. The only way to swap 1,2 to 2,1 in one instruction would be a 64-bit rotate, like rol qword ptr [arrayD], 32 (x86-64 only).

Where does XCHG CL 25 Bx exchange bytes?

XCHG CL, 25 [BX] exchanges bytes of CL with bytes stored in memory location DS:25+BX. XCHG CS, AX is illegal.


1 Answers

xchg works like Intel's documentation says.

I think the comment on the 2nd line is wrong. It should be eax=2, arrayD = 1,1,3. So you're correct, and you should email your instructor to say you think you've found a mistake, unless you missed something in your notes.

xchg only stores one element, and it can't magically look back in time to know where the value in eax came from and swap two memory locations with one xchg instruction.

The only way to swap 1,2 to 2,1 in one instruction would be a 64-bit rotate, like rol qword ptr [arrayD], 32 (x86-64 only).


BTW, don't use xchg with a memory operand if you care about performance. It has an implicit lock prefix, so it's a full memory barrier and takes about 20 CPU cycles on Haswell/Skylake (http://agner.org/optimize/). Of course, multiple instructions can be in flight at once, but xchg mem,reg is 8 uops, vs. 2 total for separate load + store. xchg doesn't stall the pipeline, but the memory barrier hurts a lot, as well as it just being a lot of work for the CPU to do to make it atomic.

Related:

  • swapping 2 registers in 8086 assembly language(16 bits) (how to efficiently swap a register with memory). xchg is only useful for this case if you need atomicity, or if you care about code-size but not speed.
  • Can num++ be atomic for 'int num'?
  • Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? (for the reg,reg version)
like image 179
Peter Cordes Avatar answered Nov 15 '22 09:11

Peter Cordes