字符串查找_汇编语言实验(二)——字符串查找

本文通过一个实验介绍了如何使用汇编语言进行字符串查找。实验要求输入两个字符串A和B,判断A是否在B中出现。提供的代码中强调了查找成功后清理堆栈的重要性,以防出现错误。程序运行后展示出查找结果。
摘要由CSDN通过智能技术生成

目录:

空山新雨后:汇编语言学习笔记(目录)​zhuanlan.zhihu.com
8a3c5b91bbf105499425e40fc51653ee.png

题目:

1、从键盘输入两个字符串:A串,例如’software’;B串,例如’school of software engineering’

2、判断A在B中是否存在,如果存在,输出“found”,反之,输出“not found”


代码:

datas segment
	string_a db 128                        	;首先创建两个字符串的缓冲区,最长大小都定义为128
	lenA     db 0
	stringA  db 128 dup('$')
	string_b db 128
	lenB     db 0
	stringB  db 128 dup('$')

	inputA   db 'Please enter StringA:', '$'
	inputB   db 'Please enter StringB:', '$'
	find     db 'found!', 0ah, 0dh, '$'
	notfind  db 'not found!', 0ah, 0dh, '$'
	new_Line db 0ah, 0dh, '$'
datas ends

stacks segment
	       db 128 dup(?)
stacks ends

codes segment
	          assume cs:codes, ds:datas, ss:stacks
	start:    
	          mov    ax, datas
	          mov    ds, ax
	;输入字符串A
	          mov    dx, offset inputA
	          mov    ah, 09h
	          int    21h
	          mov    dx, offset string_a
	          mov    ah, 0ah
	          int    21h
	          call   newLine
	;输入字符串B
	          mov    dx, offset inputB
	          mov    ah, 09h
	          int    21h
	          mov    dx, offset string_b
	          mov    ah, 0ah
	          int    21h
	          call   newLine
	;挨个比较
	          call   compare

	          cmp    al, 1                        	;返回值为1代表找到了,反之为没有找到
	          je     result1
	          call   notfound
	          jmp    next
	result1:  
	          call   found
	next:     
	          mov    ax, 4c00h
	          int    21h
	;===========================================
	compare:  
	;要在B串里面找A串
	          mov    ax, ds                       	;让附加段和数据段相等,不明白的回去看串操作的定义
	          mov    es, ax

	          mov    bx, offset stringA
	          mov    si, bx
	          mov    bx, offset stringB
	          mov    di, bx
	          mov    ah, lenB
	          mov    al, lenA
	          sub    ah, al
	          js     fail                         	;如果A串比B转长,那肯定找不到

	          xor    cx, cx
	          mov    cl, ah                       	;总共要重复lenB-lenA+1次
	          inc    cx
	com_loop1:
	          push   cx
	          push   si
	          push   di
             
	          xor    cx, cx
	          mov    cl, lenA                     	;从di开始的lenA个字符找,是否相等
	          repe   cmpsb
	          jz     success
             
	          pop    di
	          pop    si
	          pop    cx
	          inc    di
	          loop   com_loop1
	fail:     
	          xor    al, al
	          ret
	success:  
	          pop    di
	          pop    si
	          pop    cx
	          mov    al, 1
	          ret
	;===========================================
	found:    
	          mov    dx, offset find
	          mov    ah, 09h
	          int    21h
	          ret
	;===========================================
	notfound: 
	          mov    dx, offset notfind
	          mov    ah, 09h
	          int    21h
	          ret
	;===========================================
	newLine:  
	          mov    dx, offset new_Line
	          mov    ah, 09h
	          int    21h
	          ret
codes ends
   end start

这个程序,值得注意的地方就是查找成功的路线,一定要注意pop干净,不然就会产生奇奇怪怪的错误。

当然这只是一个

的算法,如果有兴趣也不妨写一下KMP算法。

运行效果图:

7083d2701e2153ec57c25ce7a8f61b89.png
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值