已知一任意长度字符串str(该字符串是一个变量,需要在运行时手动输入),以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小…
- User
已知一任意长度字符串str(该字符串是一个变量,需要在运行时手动输入),以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ffh,并将匹配位置(匹配位置以字符串str首地址为0参考点)存放在pos单元中;否则,将found单元置为00H。 要求:1. 编写完整程序,并上机通过调试能运行。(禁止使用串操作指令)
- Assistant
.data str db 200h dup(?) substr db 80 dup(?) found db ? pos dw ?
.code start: ; 读入字符串 str mov si, offset str read_str: mov ah, 01h int 21h mov [si], al cmp al, 00h je search inc si cmp si, offset str + 200h jb read_str jmp end_prog
; 读入子串 substr search: mov si, offset substr read_substr: mov ah, 01h int 21h mov [si], al cmp al, 00h je start_search inc si cmp si, offset substr + 80 jb read_substr jmp end_prog ; 开始搜索 start_search: mov di, offset str mov cx, 200h mov dx, 80h sub cx, dx next_char: cmp byte ptr [di], 00h je not_found mov si, offset substr cmp byte ptr [di], [si] jne next_char mov bx, di mov al, 1 cmp cx, dx jb not_found mov cx, dx compare: cmp byte ptr [di], 00h je found cmp byte ptr [di], [si] jne next_char inc si inc di loop compare ; 找到了子串 found: mov al, 0ffh mov [found], al sub di, bx mov [pos], di jmp end_prog ; 没有找到子串 not_found: mov al, 00h mov [found], al ; 结束程序 end_prog: mov ah, 4ch int 21hend start
- User
已知一任意长度字符串str,以00h结束,长度小于200h,编写汇编程序实现在该字符串中搜索匹配子串substr(以00h结束,长度小于80),若找到,则将found单元置为ffh,并将匹配位置(匹配位置以字符串str首地址为0参考点)存放在pos单元中;否则,将found单元置为00H。 要求:1. 编写完整程序,并上机通过调试能运行。(禁止使用串操作指令),任意长度字符串str和匹配子串substr都是需要自己输入的变量
- Assistant
.data str db 200h dup(?) substr db 80 dup(?) found db ? pos dw ?
.code start: ; 读入字符串 str mov si, offset str read_str: mov ah, 01h int 21h mov [si], al cmp al, 00h je search inc si cmp si, offset str + 200h jb read_str jmp end_prog
; 读入子串 substr search: mov si, offset substr read_substr: mov ah, 01h int 21h mov [si], al cmp al, 00h je start_search inc si cmp si, offset substr + 80 jb read_substr jmp end_prog ; 开始搜索 start_search: mov di, offset str mov cx, 200h mov dx, 80h sub cx, dx next_char: cmp byte ptr [di], 00h je not_found mov si, offset substr cmp byte ptr [di], [si] jne next_char mov bx, di mov al, 1 cmp cx, dx jb not_found mov cx, dx compare: cmp byte ptr [di], 00h je found cmp byte ptr [di], [si] jne next_char inc si inc di loop compare ; 找到了子串 found: mov al, 0ffh mov [found], al sub di, bx mov [pos], di jmp end_prog ; 没有找到子串 not_found: mov al, 00h mov [found], al ; 结束程序 end_prog: mov ah, 4ch int 21hend start
- User
(63) duplicate declaration of: FOUND
- Assistant
这个错误说明在程序中重复定义了一个变量
FOUND。在汇编程序中,每个变量的名称必须是唯一的,不能重复定义。解决方法是在程序中找到所有定义
FOUND变量的地方,将其改成其他名称,保证每个变量的名称都不重复即可。 - User
上面的代码中,出现了(63) duplicate declaration of: FOUND的问题
- Assistant
这个问题是因为在示例代码中,变量
FOUND被定义了多次,导致汇编器无法确定到底是哪个定义是正确的。解决方法有两种:
-
删除其中一个定义:找到并删除多余的
FOUND定义即可。 -
另取一个变量名:将其中一个
FOUND改名为其他的变量名,保证每个变量名都是唯一的。
下面是修改后的代码示例:
.data str db 200h dup(?) substr db 80 dup(?) found db ? pos dw ? .code start: ; 读入字符串 str mov si, offset str read_str: mov ah, 01h int 21h mov [si], al cmp al, 00h je search inc si cmp si, offset str + 200h jb read_str jmp end_prog ; 读入子串 substr search: mov si, offset substr read_substr: mov ah, 01h int 21h mov [si], al cmp al, 00h je start_search inc si cmp si, offset substr + 80 jb read_substr jmp end_prog ; 开始搜索 start_search: mov di, offset str mov cx, 200h mov dx, 80h sub cx, dx next_char: cmp byte ptr [di], 00h je not_found mov si, offset substr cmp byte ptr [di], [si] jne next_char mov bx, di mov al, 1 cmp cx, dx jb not_found mov cx, dx compare: cmp byte ptr [di], 00h je found_substring cmp byte ptr [di], [si] jne next_char inc si inc di loop compare ; 找到了子串 found_substring: mov al, 0ffh mov [found_sub], al sub di, bx mov [pos], di jmp end_prog ; 没有找到子串 not_found: mov al, 00h mov [found_sub], al ; 结束程序 end_prog: mov ah, 4ch int 21h end start .data found_sub db ?以上修改后的代码中,将第一个
FOUND改名为FOUND_SUB,并在第二个数据段中添加了定义FOUND_SUB的代码。 -
- User
(46) wrong parameters: CMP byte ptr [di], [si]
- Assistant
这个错误是由于在比较两个字节时使用了错误的语法。
在示例代码中,使用的语法是
CMP byte ptr [di], [si],这个语法是错误的。正确的语法应该是CMP byte ptr [di], [si]或者CMP [di], [si]。修改后的代码示例如下:
; 比较两个字节是否相等 cmp byte ptr [di], [si] ; 或者 cmp [di], [si]在汇编代码中,指令的参数必须是正确的语法,否则编译器会报告错误。