目录:
空山新雨后:汇编语言学习笔记(目录)zhuanlan.zhihu.com![8a3c5b91bbf105499425e40fc51653ee.png](https://i-blog.csdnimg.cn/blog_migrate/5aec7389988b3f0183d583e1c07636c2.jpeg)
题目:
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干净,不然就会产生奇奇怪怪的错误。
当然这只是一个
运行效果图:
![7083d2701e2153ec57c25ce7a8f61b89.png](https://i-blog.csdnimg.cn/blog_migrate/31acf0d02121e4b4744894bcb4120668.png)