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 the fact that the first operand is an immediate constant. I've thought about conditional jumping to a mov but I'm unsure if there are other opcodes that might be better. How can I conditionally zero this register without eating up another register?
Because the
cmovgqinstruction does not accept an immediate value operand, an approach that would not use another register could be to add the zero value to the stack, use its corresponding address instead of using an immediate value, and then restore the stack pointer.Alternatively, rather than pushing zero to the stack and then restoring the stack pointer, the zero value could be stored elsewhere in memory.
Rather than using
cmovgq, a conditional jump is an alternative approach that would not use another register, and would result in the same behavior (i.e.,rcxconditionally set to 0 ifr8b > r9b).