String operation instruction 2

String operation instruction 2

1. movs

; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$"      
    str1 db "howbin"  
    str2 db 6 dup(0)
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

    ;1 设置源地址寄存器
    
    lea si,str1;
    ;2 设置目的寄存器
    
    lea di,str2;
    
    ;3.设置方向
    cld ;std 
    
    ;4.设置循环次数
    mov cx, 6;      
    
    ;5.重复头
    
    rep movsb;
         
    lea dx,  str2 
    mov ah, 9
    int 21h        ; output string at ds:dx
    
    ; wait for any key....    
    mov ah, 1
    int 21h
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

2 stack

; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$" 
    str1 db 6 dup(0)
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

    ; add your code here
    
    ;1.选型 stos  stosb
    
    ;2.设置AL/AL填充物  al  源
    
    mov al'h';
    
    ;3. 设置目的
    lea di, str1
    ;4.设置方向
    cld
    ;5.设置次数
    mov cx,0
    mov cl,6
    ;6.设置重复头
    rep stosb;
     
    
            
    lea dx, pkey
    mov ah, 9
    int 21h        ; output string at ds:dx
    
    ; wait for any key....    
    mov ah, 1
    int 21h
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

3 lods

; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$"   
    block db -1,-2,-3,0,1,2,3,4
    count equ 8
    dplus db count dup(0)
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax

    ; add your code here    
    ;从字符串block中取正数存入字节串dplus中    
    
    
    ;1. 确定取出 lodsb  确定放入 stos
    
    ;2. 设置源头 si     目的 di
    
    lea si,block
    lea di,dplus
    
    ;3. 设置方向 
    
    cld
    
    ;4. 设置次数
    mov cx,0;
    mov cl,count;
    
again:
    lodsb; 
    cmp al,0
    jle next;
    stosb
next: 
    loop again; 
           
    lea dx, pkey
    mov ah, 9
    int 21h        ; output string at ds:dx
    
    ; wait for any key....    
    mov ah, 1
    int 21h
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

4 cmps string comparison instruction

;比较等长俩个字符串,判断他们是否相等
    ; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$"       
    str1 db "abc"
    str2 db "abc"
    result db dub(0);
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
; set segment registers:
    mov ax, data
    mov ds, ax
    mov es, ax
     
     mov result,0
    ;1.选择cmps 
    
    ;2.设置源与目的  
    lea si,str1;
    lea di,str2
    
    ;3.设置方向
    cld
    
    ;4.设置次数
    
    mov cx,0;
    mov cl,3
    
    ;5.执重复头
    
    repe cmpsb;
   
   ;6.判断
    jnz next;
    mov result,1
next:
    
            
    lea dx, pkey
    mov ah, 9
    int 21h        ; output string at ds:dx
    
    ; wait for any key....    
    mov ah, 1
    int 21h
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

5 scas

;从一个字符串中查找一个指定字符
        lea di,string
        mov al,	''      ;20h 空格
        mov cl, sizeof string
        cld             ;设置方向
        repnz scasb
        jz        found
        .....
        
found:....
enter code here

Summary 6

 1. 串操作,确定指令
 2. 根据串的位置准备ds,es(ds可超越,但是es不可以)
 3. 利用了lea,或者 设置offset si,di
 4. 利用 sizeof lenghof 设置cx
 5. 选择合适的rep 
 6. 设置不同分支程序(针对repz或者repnz)

Guess you like

Origin www.cnblogs.com/Howbin/p/11112084.html
Recommended