The XLAT instruction

Perform a table lookup using the AL and BX registers.

The XLAT instruction format

InstructionDescription
XLAT / XLATBSet AL to the memory byte DS:[BX + unsigned AL].

Locates a byte entry in a table in memory, using the contents of the al register as a table index, then copies the contents of the table entry back into the al register. The index in the al register is treated as an unsigned integer. The xlat instruction obtains the base address of the table in memory from the ds:bx register pair. The ds segment can be overridden by a segment override prefix.

Example 1: A simple table lookup

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    table db "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
data ends

code segment public             ; Start of the code segment
    assume ds:data, cs:code

    main proc                   ; Start of the main procedure
        mov     ax, seg data    ; Get the data segment address and store it in the ax register
        mov     ds, ax          ; Pass the data segment address from ax to ds

        mov     al, 3           ; Copy the value 3 into the al register; AL = 03 (a 0-based index, so the 4th character 'D')
        lea     bx, ds:[table]  ; Load the address of the variable table into ds:bx
        xlat                    ; Look up the value in the al register in the table named table; AL = 'D' (44h)

        mov     ax, 4C00h       ; Copy the process termination function number into ah and the value into al
        int     21h             ; Call the interrupt
    main endp                   ; End of the main procedure

code ends                       ; End of the code segment
end main                        ; End of file. The main procedure is the entry point

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".

Example 2: A custom encoding table

This example demonstrates the conversion of an array of input bytes using a translation table.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    encoding db 10h, 20h, 30h, 40h, 50h, 60h, 70h, 80h
    input    db 2, 4, 6
    output   db 3 dup(0)
data ends

code segment public                ; Start of the code segment
    assume ds:data, cs:code

    main proc                      ; Start of the main procedure
        mov     ax, seg data       ; Get the data segment address and store it in ax
        mov     ds, ax             ; Pass the data segment address from ax to ds
        mov     es, ax             ; Pass the data segment address from ax to es

        lea     bx, ds:[encoding]  ; Load the address of the variable encoding into ds:bx
        lea     si, ds:[input]     ; Load the address of the variable input into ds:si
        lea     di, es:[output]    ; Load the address of the variable output into es:di
        mov     cx, 3              ; Set the loop counter

    EncodeLoop:
        lodsb                      ; Load the byte at the address ds:si into the al register and increment si
        xlat                       ; Convert the byte using the encoding table
        stosb                      ; Copy from al to es:di and increment di
        loop    EncodeLoop         ; Loop while CX≠0

        mov     ax, 4C00h          ; Copy the process termination function number into ah and the value into al
        int     21h                ; Call the interrupt
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main                           ; End of file. The main procedure is the entry point

The values in the output buffer (at the address ES:DI) will be: 30h, 50h, 70h.

Processor status flags

CF
No
PF
No
AF
No
ZF
No
SF
No
TF
No
IF
No
DF
No
OF
No