串的复制——串传送指令MOVSB和方向标志位DF和CLD和REP

  • 复制字符串,没学串移动指令的操作
assume cs:codes,ds:datas

datas segment
 buf db 'Welcome to masm!'
 count equ $-buf
 copy db 16 dup(?),0AH,0DH,'$'
datas ends

codes segment
start:
       mov ax,datas
       mov ds,ax
       lea si,buf
       lea di,copy
       mov cx,count/2;字传输
    
  next:
       mov ax,[si]
       mov [di],ax
       add si,2
       add di,2
       loop next
lea dx,copy
mov ah,09h
int 21h
MOV AH,4CH
INT 21H
codes ends
end start

  • DF 方向标志位 DIRECTION FLAG
  • 在串处理指令中,控制每次操作后SI,DI的增减
  • DF = 0每次操作后SI,DI递增 UP
  • DF = 1每次操作后SI,DI递减 DN/Down

MOVSB(move string byte)

  • movsb 以字节为单位传送(move string byte)
((ES) * 16 +  (DI)) = ((DS) * 16 + (SI))
;DF = 0
(SI) = (SI) + 1
(DI) = (DI) + 1
;DF = 1
(SI) = (SI) - 1
(DI) = (DI) - 1
  • movsw 以字为单位传送
((ES) * 16 +  (DI)) = ((DS) * 16 + (SI))
;DF = 0
(SI) = (SI) + 2
(DI) = (DI) + 2
;DF = 1
(SI) = (SI) - 2
(DI) = (DI) - 2

CLD

  • 对DF位进行设置的指令
  • CLD指令:将标志寄存器的DF位设为0(CLEAR)
  • SI和DI递增
  • STD指令:将标志寄存器的DF位设为1(SET UP)
  • SI和DI递减

想用MOVSB记得设置ES

MOV AX,DATASEG

MOV ES,AX

assume cs:codes,ds:datas

datas segment
 buf db 'Welcome to masm!'
 count equ $-buf
 copy db 16 dup(?),0AH,0DH,'$'
datas ends

codes segment
start:
       mov ax,datas
       mov ds,ax
       ;没有设置ES
       mov es,ax
       lea si,buf
       lea di,copy
       cld;清除标志寄存器
       mov cx,count
  next:
       movsb
       loop next
lea dx,copy
mov ah,09h
int 21h
MOV AH,4CH
INT 21H
codes ends
end start

rep

  • 通常与串传送指令搭配使用
  • 根据CX的值,重复执行后面的命令
rep movsb
;s:movsb
;loop s

想使用REP记得MOV CX = COUNT

assume cs:codes,ds:datas

datas segment
 buf db 'Welcome to masm!'
 count equ $-buf
 copy db 16 dup(?),0AH,0DH,'$'
datas ends

codes segment
start:
       mov ax,datas
       mov ds,ax
       ;没有设置ES
       mov es,ax
       lea si,buf
       lea di,copy
       cld;清除标志寄存器
       mov cx,count
       REP MOVSB
lea dx,copy
mov ah,09h
int 21h
MOV AH,4CH
INT 21H
codes ends
end start
发布了251 篇原创文章 · 获赞 28 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xiong_xin/article/details/103550472