Conditional move zero into register?

714 Views Asked by At

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?

1

There are 1 best solutions below

1
dannyadam On

Because the cmovgq instruction 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.

pushq   $0            # push 0 onto the stack
cmpb    %r9b,   %r8b  # compare r9b and r8b
cmovgq  (%rsp), %rcx  # if r8b > r9b, move zero to rcx
addq    $8,     %rsp  # restore 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.

  cmpb    %r9b, %r8b  # compare r9b and r8b
  cmovgq  zero, %rcx  # if r8b > r9b, move zero to rcx
  ...

  .section .rodata
zero:
  .quad 0

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., rcx conditionally set to 0 if r8b > r9b).

  cmpb    %r9b, %r8b   # compare r9 and r8
  jle     destination  # if r8b <= r9b, skip the next line
  movq    $0, %rcx     # if r8b > r9b, move zero to rcx
destination:
  ...