cqo

Sign-extend rax into rdx:rax

Sign-extends rax into rdx:rax, filling rdx with copies of the sign bit of rax.

x86-64 and later

Intel's reference: CWD/CDQ/CQO

CFPFAFZFSFDFOF –––––––
  • Changes no flag

Forms

  • cqo

Use cqo before idiv rbx when the intended dividend is the signed 64-bit value in rax. idiv actually divides the signed 128-bit pair, so an unintended high half can produce a wrong quotient or a divide error. A deliberately prepared 128-bit dividend does not need this extension.

global _start

section .text
_start:
        mov rax, 12
        mov rbx, 5
        mov rcx, 3
        mov rsi, 9

        cqo

        mov rax, 60         ; syscall 60: exit
        xor rdi, rdi
        syscall