The LODSB and LODSW instructions
Load from memory into the AL or AX register.
Format of the LODSB/LODSW instructions
| Instruction | Description |
|---|---|
| LODSB | Load the byte at address DS:SI into the AL register. |
| LODSW | Load the word at address DS:SI into the AX register. |
Loads a byte or a word from the source operand into the al or ax register. The source operand is a memory location whose address is read from the registers ds:si. The ds segment can be overridden by a segment override prefix.
After a byte or a word is transferred from the memory location into the al or ax register, the si register is automatically incremented or decremented according to the setting of the DF flag in the FLAGS register. (If the DF flag is 0, the si register is incremented; if the DF flag is 1, the si register is decremented.) The si register is incremented or decremented by 1 for byte operations or by 2 for word operations.
Example of use: searching for an element in an array
title .model small .stack 100h data segment public array db 1, 2, 3, 4, 5, 6, 8 arrlen equ ($-array) 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 cx, arrlen ; Set the counter equal to the length of the array array lea si, ds:[array] ; Load the address of the variable array into the ds:si register FindAndQuitLoop: lodsb ; Load the byte at address ds:si into the al register cmp al, 5 ; Compare the loaded value in the al register with the number 5 loopnz FindAndQuitLoop ; Loop while CX≠0 or ZF≠1 sub cx, ax neg cx inc cx ; Increment the value in cx by 1 cmp cx, ax ja NoElementInArray ; If the number in cx is greater than in ax, jump to NoElementInArray xchg cx, ax ; Exchange the values in cx and ax jmp Done ; Jump unconditionally to the Done label NoElementInArray: mov ax, 0FFFFh ; Copy the value -1 into the ax register Done: 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)".
Processor status flags
Note: the DF flag is not changed by the LODSB/LODSW instructions, but it affects the direction of processing (increment/decrement of the index).