Questions tagged [conditional-move]
8 questions
23
votes
4 answers
Purpose of cmove instruction in x86 assembly?
When disassembling an executable I encountered the cmove instruction. I've already searched on the Internet but I've only found that it's a conditional move, and if the source and destination are equal a mov occurs. What I don't understand yet is…

maluz
- 265
- 1
- 3
- 10
7
votes
2 answers
Hard to debug SEGV due to skipped cmov from out-of-bounds memory
I'm trying to code a few high-performance assembly functions as an exercise, and have encountered a weird segfault that happens when running the program, but not in valgrind or nemiver.
Basically a cmov that shouldn't be run, with an out-of-bound…

Lou Garczynski
- 624
- 3
- 10
6
votes
2 answers
Conditional move (cmov) for AVX vector registers based on scalar integer condition?
For 64-bit registers, there is the CMOVcc A, B instruction, that only writes B to A if condition cc is satisfied:
; Do rax <- rdx iff rcx == 0
test rcx, rcx
cmove rax, rdx
However, I wasn't able to find anything equivalent for AVX. I still want to…

janw
- 8,758
- 11
- 40
- 62
5
votes
1 answer
In assembly, should branchless code use complementary CMOVs?
It's well known that we can use the CMOV instruction to write branchless code, but I was wondering if I'm writing the equivalent of x = cond ? 1 : 2, should I prefer
CMOVE rax, 1 #1a
CMOVNE rax, 2 #1b
or
MOV rax, 1 #2a
CMOVNE rax, 2 …

Daniel
- 2,869
- 4
- 26
- 28
5
votes
1 answer
Why does x86 only have 1 form of conditional move, not immediate or 8-bit?
I've noticed that the Conditional Move instruction is less extensible than the normal mov. For example, it doesn't support immediates and doesn't support the low-byte of a register.
Out of curiosity, why is the Cmov command much more restrictive…

David542
- 104,438
- 178
- 489
- 842
4
votes
1 answer
RISCV branchless coding
On Intel AVX, there is a possibility of branchless code.
Instead of branching for case0 or case1, you can compute both cases, and blend the results based on a condition.
AVX does this 8 way for float using the vblendps instruction.
You can also do…

Bram
- 7,440
- 3
- 52
- 94
3
votes
1 answer
Conditional move zero into register?
Is there a way to conditional move a zero into a register in assembly? I'm trying to do
cmpb %r9b, %r8b #compare r9 and r8
cmovgq $0, %rcx #If r8>r9, move zero to rcx
But the compiler is complaining about "operand type mismatch for cmovg" due to…

Tony S
- 35
- 4
-1
votes
1 answer
How to force compiler to generate conditional move by using inline assembly
I've spent several hours trying to convert the following code to inline assembly (GCC) but in vain:
int new_low = mid + 1;
int new_high = mid - 1;
if (middle < key) {
low = new_low;
}
if (!(middle < key)) {
high = new_high;
}
I want the…

Bogi
- 2,274
- 5
- 26
- 34