# `cqo`

[x86-64 Documentation](https://asm-editor.specy.app/documentation/x86.md) › [Instructions](https://asm-editor.specy.app/documentation/x86/instruction.md)

Web page: https://asm-editor.specy.app/documentation/x86/instruction/cqo

**Sign-extend rax into rdx:rax**

Category: Arithmetic

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](https://intel.github.io/SDM/instruction/CWD-CDQ-CQO.html)

Used in: [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arithmetic-logic-and-bits.md) (x86-64 assembly), [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/x86/examples/multiply-and-divide.md) (x86-64 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| CF | not changed |
| PF | not changed |
| AF | not changed |
| ZF | not changed |
| SF | not changed |
| DF | not changed |
| OF | not changed |

## Forms

- `cqo`

## Description

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.

## Example

```x86
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
```
