当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

汇编语言:编写程序在字符串STRING1中查找子串STR2且

更新:01-06 整理:39baobao.com
字体:

;在MASM 6.15下编译通过 data segment msg1 db 'Input String1:$' msg2 db 'Input String2:$' msg3 db 'Found str2 in str1 at:$' msg4 db 'str2 not in str1.',0dh,0ah,'str1+str2=$' str1 db 255;输入缓冲区大小 db ?;实际输入的字符数 db 255 dup(?);存贮STRING1 str2 db 255 db ? db 255 dup(?);存贮STRING2 Pos db 3 DUP('$');用来存位置对应的数字字符串(16进制形式),第一个字符位置为00H data ends sta segment stack db 1024 dup(0) sta ends CRLF macro;输出回车换行符 mov ah,02h mov dl,10 int 21h mov ah,02h mov dl,13 int 21h endm code segment assume cs:code,ds:data,es:data,ss:sta FindInStr proc far ;入口参数 ;[bp+8]=string2的开始地址 ;[bp+6]=string1的开始地址 ;[bp-2]=string2的长度,字节 ;[bp-4]=string1的长度,字节 ;[bp-6]=string1位置变量,字 ;[bp-8]=最大查找次数,字节 ;出口:AL=如果STR2在STR1中,则为第一次匹配时的位置,如果没找到,则为0FFH push bp mov bp,sp add sp,-8 cld ;局部变量初始化 mov si,[bp+8] mov di,[bp+6] mov [bp-6],di mov al,[si-1] mov [bp-2],al mov al,[di-1] mov [bp-4],al sub al,[bp-2] inc al mov [bp-8],al mov cl,[bp-8] xor ch,ch LP_CMP: push cx mov si,[bp+8] mov di,[bp-6] mov cl,[bp-2] xor ch,ch repe cmpsb jne Next mov ax,[bp-6] sub ax,[bp+6] jmp RetHere Next: inc byte ptr [bp-6] pop cx loop LP_CMP mov al,0FFH RetHere: mov sp,bp pop bp ret 4 FindInStr endp TRANBUFF proc far;转化16进制数字串,入口参数AL=位置 CLD XOR AH,AH MOV BL,16 DIV BL LEA DI,Pos CMP AL,0AH JB L1 ADD AL,37H JMP L2 L1: ADD AL,30H L2: STOSB MOV AL,AH CMP AL,0AH JB L3 ADD AL,37H JMP L4 L3: ADD AL,30H L4: STOSB ret TRANBUFF endp start: mov ax,data mov ds,ax mov es,ax mov ah,09h lea dx,msg1 int 21h;显示Input String1: mov ah,0ah lea dx,str1 int 21h;用户输入STRING1 CRLF mov ah,09h lea dx,msg2 int 21h;显示Input String2: mov ah,0ah lea dx,str2 int 21h;用户输入STRING2 CRLF lea ax,str2+2 push ax lea ax,str1+2 push ax call FindInStr cmp al,0FFH jz NoMatch call TRANBUFF mov ah,09h lea dx,msg3 int 21h mov ah,09h lea dx,Pos int 21h;显示找到的位置 jmp DONE NoMatch: cld lea si,str2+2 lea di,str1 add di,2 xor ax,ax mov al,str1+1 add di,ax xor ax,ax mov al,str2+1 mov cx,ax rep movsb mov byte ptr [di],'$' mov ah,09h lea dx,msg4 int 21h mov ah,09h lea dx,str1+2 int 21h;显示str2连接到str1后合并的字符串 DONE: mov ax,4c00h int 21h code ends end start

本文地址:https://www.39baobao.com/show/29_24840.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.