一、字符串处理指令
(1) lodsb、lodsw:把DS:SI指向的存储单元中的数据装入AL或AX,然后根据DF标志增减SI
(2) stosb、stosw:把AL或AX中的数据装入ES:DI指向的存储单元,然后根据DF标志增减DI
(3) movsb、movsw:把DS:SI指向的存储单元中的数据装入ES:DI指向的存储单元中,然后根据DF标志分别增减SI和DI
(4) scasb、scasw:把AL或AX中的数据与ES:DI指向的存储单元中的数据相减,影响标志位,然后根据DF标志分别增减SI和DI
(5) cmpsb、cmpsw:把DS:SI指向的存储单元中的数据与ES:DI指向的存储单元中的数据相减,影响标志位,然后根据DF标志分别增减SI和DI
(6) rep:重复其后的串操作指令。重复前先判断CX是否为0,为0就结束重复,否则CX减1,重复其后的串操作指令。主要用在MOVS和STOS前。一般不用在LODS前。
上述指令涉及的寄存器:段寄存器DS和ES、变址寄存器SI和DI、累加器AX、计数器CX
涉及的标志位:DF、AF、CF、OF、PF、SF、ZF
二、使用举例
(1) 把当前数据段中偏移1000H开始的100个字节的数据传送到从偏移2000H开始的单元中
CLD
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 100
NEXT: LODSB
STOSB
DEC CX
JNZ NEXT
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 100
NEXT: LODSB
STOSB
DEC CX
JNZ NEXT
CLD
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 100
NEXT: MOVSB
DEC CX
JNZ NEXT
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 100
NEXT: MOVSB
DEC CX
JNZ NEXT
CLD
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 3
NEXT: MOVSB
LOOP NEXT
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 3
NEXT: MOVSB
LOOP NEXT
CLD
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 3
REP MOVSB
PUSH DS
POP ES
MOV SI,1000H
MOV DI,2000H
MOV CX, 3
REP MOVSB
(2) 判断AL中的数字是否为16进制数
STRING DB
'
0123456789ABCDEFabcdef
'
STRLEN EQU $-STRING
...
CLD
MOV DX,SEG STRING
MOV ES,DX
MOV CX,STRLEN
MOV DI,OFFSET STRING
NEXT: SCASB
LOOPNZ NEXT
JNZ NOT_FOUND
FOUND:
...
NOT_FOUND:
...
STRLEN EQU $-STRING
...
CLD
MOV DX,SEG STRING
MOV ES,DX
MOV CX,STRLEN
MOV DI,OFFSET STRING
NEXT: SCASB
LOOPNZ NEXT
JNZ NOT_FOUND
FOUND:
...
NOT_FOUND:
...
(3) 其它实例
start:
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,# 256
sub si,si
sub di,di
rep
movw
mov ax,#BOOTSEG
mov ds,ax
mov ax,#INITSEG
mov es,ax
mov cx,# 256
sub si,si
sub di,di
rep
movw
movl $
1024
*
5
,%ecx
xorl %eax,%eax
xorl %edi,%edi
cld ; rep;stosl
movl $pg0+ 7 ,_pg_dir
movl $pg1+ 7 ,_pg_dir+ 4
movl $pg2+ 7 ,_pg_dir+ 8
movl $pg3+ 7 ,_pg_dir+ 12
movl $pg3+ 4092 ,%edi
movl $0xfff007,%eax
std
1 : stosl
subl $0x1000,%eax
jge 1b
xorl %eax,%eax
xorl %edi,%edi
cld ; rep;stosl
movl $pg0+ 7 ,_pg_dir
movl $pg1+ 7 ,_pg_dir+ 4
movl $pg2+ 7 ,_pg_dir+ 8
movl $pg3+ 7 ,_pg_dir+ 12
movl $pg3+ 4092 ,%edi
movl $0xfff007,%eax
std
1 : stosl
subl $0x1000,%eax
jge 1b