Hello World

汇编查找子字符串出现次数

Shanicky posted @ 2013年12月17日 14:54 in 未分类 with tags assembly 8086 微机原理 , 780 阅读
assume cs:code,ds:data,ss:stack
data segment
   str db 100 dup (0)
   subs db 100 dup (0)
   subslen db 0
   strlen db 0   
   count db 0
   str1 db 'string',13,10,'$'
   str2 db 'substring',13,10,'$'
   str3 db 'count=$'   
   crlf db 13,10,'$' 
data ends
stack segment
    dw   128  dup(0)
stack ends
code segment
start:
    mov ax, data
    mov ds, ax   
    mov ax,stack
    mov ss,ax
    mov sp,80h
    
    lea dx,str1
    mov ah,9
    int 21h
       
    mov bx,0
s1:
    mov ah,1
    int 21h
    cmp al,13
    je cr1
    
    mov str[bx],al
    inc bx
    inc byte ptr strlen
    jmp s1
    
cr1:
    
    lea dx,crlf
    mov ah,9
    int 21h
    
    lea dx,str2
    mov ah,9
    int 21h
    
    
    mov bx,0
s2:
    mov ah,1
    int 21h
    cmp al,13
    je cr2
    
    mov subs[bx],al
    inc bx
    inc byte ptr subslen
    jmp s2
    
cr2:
    
    mov cl,strlen
    mov ch,0
    mov bx,0
s3:
    mov al,str[bx]
    cmp al,'a'
    jb s3_next
    cmp al,'z'
    ja s3_next
    
    push cx
    mov cl,subslen
    mov ch,0
    mov di,bx
    mov si,0
s4:
    mov al,str[di]
    mov dl,subs[si]
    cmp al,dl
    
    jne not_eq
    
    inc si
    inc di
    loop s4 

    inc byte ptr count    
not_eq:
     
     pop cx
s3_next:
     inc bx
     loop s3       
   
    lea dx,crlf
    mov ah,9
    int 21h
    
    lea dx,str3
    mov ah,9
    int 21h
    
    mov dl,count
    add dl,30h
    mov ah,2
    int 21h
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter