dos字符串查找比较(汇编)

第三题:字符串查找比较

实验目的:掌握串指令的基本功能及应用

实验内容:利用DOS功能调用从键盘输入一个字符串,然后输入单个字符,利用串扫描指令查找该字符串中是否有该字符。具体要求如下:

  1. 如果找到,则在屏幕上显示:

Success!!! Location: X

其中,X为该字符在字符串中第一次出现的位置

  1. 如果没找到,则在屏幕上显示:

Fail!!!

  1. 输入一个字符串后,可以反复输入希望查询的字符,直到按ESC键退出程序返回DOS命令行

  2. 程序结束时,在屏幕上显示本人的姓名全拼和学号

;Q3.ASM
;locusxt
;cnjs.zhuting[at]gmail.com

data segment
	myname	db 0dh, 0ah, 'NAME: locusxt', 0dh, 0ah, '$'
	myid	db 'ID:locusxt', 0dh, 0ah, '$'
	buf		db 1024 dup (?)    ;缓冲区
	suc		db 0dh, 0ah, 'Success!!! Location: ', '$'
	fai		db 0dh, 0ah, 'Fail!!!', 0dh, 0ah, '$'
	endl	db 0dh, 0ah, '$'    ;换行加回车
	num		db '000', '$'   ;用于暂时存个数字
data ends

stack segment stack
	sta db 50 dup(?)
	top equ length sta
stack ends

code segment
	assume cs:code, ds:data, ss:stack
	begin:
		mov ax, data
		mov ds, ax
		mov es, ax    
		;因为repne scasb默认以es为段基址,需要将es与ds置成一样
		mov ax, stack
		mov ss, ax
		mov ax, top
		mov sp, ax
	
	inputstr:    ;输入一个字符串
		mov bh, 00h
		mov bx, offset buf
		mov ds:[bx], 1000    
		;调用时的参数,缓冲区大小,注意这里只用了一个字节来存缓冲区大小,
		;所以如果大于255只会取低8位作为大小
		mov dx, bx    ;从键盘读到缓冲区
		mov ah, 0ah
		int 21h
		mov bx, dx
		mov dl, ds:[bx + 1]    
		;将实际字符数读入到dl,实际字符数是不会超过255的,
		;同样因为只有一个字节存了实际字符大小
		mov bx, dx
		mov dx, offset endl    ;输出一个回车和换行,否则光标不会移动
		mov ah, 09h
		int 21h

	inputch:    
		;带回显的读入一个字符
		;(但这会导致一个问题,当按了一个无法显示的键时,会显示出奇怪的字符)
		mov ah, 01h
		int 21h
	
	judgeesc:    ;判断是不是esc
		cmp al, 1bh
		jz dealesc

	search:    ;串操作
		cld
		mov di, offset buf
		add di, 02h
		mov cx, bx
		repne scasb
		jz success
		jmp fail

	success:
		mov dx, offset suc
		mov ah, 09h
		int 21h

		mov si, bx
		sub si, cx
		mov ax, si
		
		;以下用于位置标号的输出,位置从1开始
		;需要考虑超过9的情况,13,100这样的
		;大致是不断模10,将余数存在num中
		mov cl, 10d
		mov si, 03d    ;记录位数
	modten:
		div cl
		dec si
		add ah, 30h
		mov ds:[num + si], ah
		mov ah, 00h
		cmp al, 0
		jnz modten
		
		mov dx, offset num
		add dx, si
		mov ah, 09h
		int 21h

		;add dx, 30h    ;加0x30,使输出数字
		;mov ah, 02h
		;int 21h
		
		mov dx, offset endl
		mov ah, 09h
		int 21h
		jmp inputch
	
	fail:
		mov dx, offset fai
		mov ah, 09h
		int 21h
		jmp inputch
	
	dealesc:    ;输入esc退出
		mov dx, offset myname
		mov ah, 09h
		int 21h
		mov dx, offset myid
		mov ah, 09h
		int 21h

		mov ax, 4c00h
		int 21h

code ends
	end begin



=====================================
很麻烦,细节很多
比如最后数字的输出
感谢pr的帮助



转载于:https://my.oschina.net/locusxt/blog/206669

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值