x86 Assembly Language Reference Manual

# Decimal Arithmetic Instructions

## Decimal Adjust AL after Addition (daa)

```daa
```
##### Operation

decimal-adjust AL -> AL

##### Description

Use daa only after executing the form of an add instruction that stores a two-BCD-digit byte result in the AL register. daa then adjusts AL to a two-digit packed decimal result.

##### Example

Decimal adjust the two-BCD-digit in the AL register:

`daa`

## Decimal Adjust AL after Subtraction (das)

```das
```

##### Operation

decimal-adjust AL -> AL

##### Description

Use das only after executing the form of a sub instruction that stores a two-BCD-digit byte result in the AL register. das then adjusts AL to a two-digit packed decimal result.

##### Example

Decimal adjust the two-BCD-digit in the AL register:

`das`

## ASCII Adjust after Addition (aaa)

```aaa
```

##### Operation

ASCII-adjust AL -> AL

##### Description

You use aaa only after executing the form of an add instruction that stores a two-BCD-digit byte result in the AL register. aaa then adjusts AL to contain the correct decimal result. The top nibble of AL is set to 0. To convert AL to an ASCII result, follow the aaa instruction with:

`or %al, 0x30`

The following table shows how aaa handles a carry.

Table 2-8 aaa Handling a Carry

Carry

Action

decimal carry

AH + 1; CF and AF set to 1

no decimal carry

AH unchanged; CF and AF cleared to 0

##### Example

Adjust the AL register to contain the correct decimal result after an add instruction that stores a two-BCD-digit byte.

`aaa`

## ASCII Adjust after Subtraction (aas)

```aas
```

##### Operation

ASCII-adjust AL -> AL

##### Description

Use aas only after executing the form of an add instruction that stores a two-BCD-digit byte result in the AL register. aas then adjusts AL to contain the correct decimal result. The top nibble of AL is set to 0. To convert AL to an ASCII result, follow the aas instruction with:

`or %al, 0x30`

Table 2-9 shows how aas handles a carry.

Table 2-9 How aas Handles a Carry

Carry

Action

decimal carry

AH - 1; CF and AF set to 1

no decimal carry

AH unchanged; CF and AF cleared to 0

##### Example

Adjust the AL register to contain the correct decimal result after a sub instruction that stores a two-BCD-digit byte

`aas`

## ASCII Adjust AX after Multiply (aam)

```aam
```

##### Operation

AL 10 -> AH mod 10 AL -> AL

##### Description

You use aam only after executing a mul instruction between two BCD digits (unpacked). mul stores the result in the AX register. The result is less than 100 so it can be contained in the AL register (the low byte of the AX register). aam unpacks the AL result by dividing AL by 10, stores the quotient (most-significant digit) in AH, and stores the remainder (least-significant digit) in AL.

##### Example

Adjust the AL register to contain the correct decimal result after a mul instruction between two BCD digits:

`aam`

## ASCII Adjust AX before Division (aad)

```aad
```

##### Operation

AL + (AH x 10) -> AL 0 -> AH

##### Description

aad prepares two unpacked BCD digits for a division operation that yields an unpacked result. The least-significant digit is in AL; the most-significant in AH.

aad prepares the AL and AH registers:

```AL + (AH x 10) -> AL
0 -> AH```

AX is then equal to the binary equivalent of the original unpacked two-digit BCD number.

##### Example

Adjust the AL and AH registers for a division operation by setting the AX register equal to the original unpacked two-digit number:

`aad`