# `idiv`

[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/idiv

**Signed divide**

Category: Arithmetic

Divides the implicit signed dividend selected by the operand width, producing a quotient and remainder.

8086 and later

[Intel's reference: IDIV](https://intel.github.io/SDM/instruction/IDIV.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 | undefined (do not rely on it) |
| PF | undefined (do not rely on it) |
| AF | undefined (do not rely on it) |
| ZF | undefined (do not rely on it) |
| SF | undefined (do not rely on it) |
| DF | not changed |
| OF | undefined (do not rely on it) |

## Forms

- `idiv r/m` (8, 16, 32, 64)
  - `idiv r/m8`
  - `idiv r/m16`
  - `idiv r/m32`: 386 and later
  - `idiv r/m64`

## Operands

- `r/m`: A general-purpose register or memory location matching the operation’s size, for example `rax` or `qword [rbp-8]`.

## Description

The divisor width selects the implicit dividend and outputs: 8 bits divides `ax` into quotient `al` and remainder `ah`; 16 bits divides `dx:ax` into `ax` and `dx`; 32 bits divides `edx:eax` into `eax` and `edx`; 64 bits divides `rdx:rax` into `rax` and `rdx`.

The high half is part of the signed dividend. To divide a signed value held only in the low half, extend it first with `cbw`, `cwd`, `cdq` or `cqo` respectively. For example, `cqo` then `idiv rcx` divides signed `rax`. The quotient rounds toward zero; a nonzero remainder has the dividend's sign. A zero divisor or a quotient outside the destination's signed range causes a divide error. Flags are undefined.

## Example

```x86
global _start

section .text
_start:
        mov rax, -100       ; the dividend is rdx:rax
        cqo                 ; sign extends rax into rdx
        mov rbx, 7
        idiv rbx            ; quotient in rax, remainder in rdx

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