Instruction Set
4-144
LODS
LODS
This example counts the number of carriage returns in a string of characters in memory.
The microcontroller copies the bytes and compares them with the carriage-return character,
one by one, from first to last.
Tips
Before using LODS, always be sure to: set up SI with the offset of the string, set up CX
with the length of the string, and then use CLD (forward) or STD (backward) to establish
the direction for string processing.
To inspect each component in a string, use LODS within a loop.
To perform a custom operation on each component in a string, use LODS and STOS within
a loop. Within the loop, use the following sequence of instructions: use LODS to copy a
component from memory, use other instructions to perform the custom operation, and then
use STOS to copy the component back to memory. To overwrite the original string with the
results, set up DI with the same offset as SI before beginning the loop.
The string instructions always advance SI and/or DI, regardless of the use of the REP prefix.
Be sure to set or clear DF before any string instruction.
STRING
DB
512 DUP (?)
CR
DB
0Dh
; carriage return
; count number of carriage returns in string
; initialize and use string
...
; set up registers and flags
LEA
SI,STRING
; load offset (segment = DS)
MOV
CX,LENGTH STRING
; set up counter
; LENGTH is an assembler directive
CLD
; process string low to high
MOV
DX,0
; set up total
LOAD:
; load character and compare
LODSB
; copy character to AL
CMP
AL,CR
; is it a carriage return?
; if not, then load next character
JNE
NEXT
; else, add 1 to number of carriage returns
INC
DX
NEXT:
LOOP
LOAD
; while CX is not zero,
; jump to top of loop
Содержание Am186 Series
Страница 1: ...Am186 and Am188 Family Instruction Set Manual February 1997...
Страница 10: ...Table of Contents x...
Страница 18: ...Programming 1 8...
Страница 40: ...Instruction Set Listing 3 14...