串的复制——串传送指令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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值