# `cmpsb`

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

**Compare string bytes**

Category: Block and string

Compares the byte at [rsi] with the byte at [rdi] and sets the flags accordingly.

8086 and later

Used in: [Arrays, strings and the string instructions](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arrays-strings-and-the-string-instructions.md) (x86-64 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| CF | depends on the result |
| PF | depends on the result |
| AF | depends on the result |
| ZF | depends on the result |
| SF | depends on the result |
| DF | not changed |
| OF | depends on the result |

Reads: DF

## Forms

- `cmpsb`

## Description

The b suffix selects an element of 1 byte. After each element, the instruction increases `rsi` and `rdi` by 1 when DF = 0, or decreases `rsi` and `rdi` by 1 when DF = 1. In 64-bit mode the default addresses use `rsi` and `rdi`. The `a32` address-size prefix instead uses `esi` and `edi`; 16-bit string addressing is unavailable.

`repe` / `repz` repeats while ZF = 1, and `repne` / `repnz` repeats while ZF = 0. Each stops when the counter reaches zero; a nonzero initial counter permits the first comparison before testing its result. The counter is `rcx` by default or `ecx` with `a32`. If it starts at zero, a repeated instruction accesses no element. The data width is independent of this address and counter width.

## Example

```x86
global _start

section .data
source: db 1, 2, 3, 4, 5, 6, 7, 8
destination: times 8 db 0

section .text
_start:
        cld                 ; count upwards
        mov rsi, source
        mov rdi, destination
        mov rax, 7
        cmpsb               ; compares [rsi] with [rdi] and sets the flags

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