string 如何比较两个字符串组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14099832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two strings assembly
提问by HymanRobinson
Could anyone tell me how to compare two strings in assembly language, I`ve written the followign, but it does not seem to work.
谁能告诉我如何用汇编语言比较两个字符串,我写了以下内容,但似乎不起作用。
assume cs:code, ds:data
data segment
sirlung db "abcdjjj"
lungimelung equ $-sirlung
sirscurt db "aby"
lungimescurt equ $-sirscurt
exista db "Exista!$"
nuexista db "NU exista!$"
iesire db "Apasa enter pentru iesire!$"
data ends
code segment
start:
mov ax,data
mov ds,ax
mov bx,offset sirlung
mov di,offset sirscurt
dec bx
push bx
push di
mov dx,lungimelung
mov si,lungimescurt
bucla1:
pop di
pop bx
inc bx
mov al,sirlung[bx]
mov cl,sirscurt[di]
cmp al,cl
jne bucla1
push bx
push di
je bucla2
cmp bx,dx
ja sfarsit_nu_exista
bucla2:
inc bx
inc di
mov al,sirlung[bx]
mov cl,sirscurt[di]
cmp al,cl
jne bucla1
cmp di,si
jl sfarsit_exista
jae bucla2
sfarsit_exista:
mov dx,offset exista
mov ah,09h
int 21h
mov ah, 0ah
mov dx,offset iesire
int 21h
mov ax,4c00h
int 21h
sfarsit_nu_exista:
mov dx,offset nuexista
mov ah,09h
int 21h
mov ah, 0ah
mov dx,offset iesire
int 21h
mov ax,4c00h
int 21h
code ends
end start
回答by Matthew Layton
I had the same problem when I was writing asm in school, years ago. The problem I encountered was that I wanted to compare the word "exit" with a user entry. If they typed "exit", the application quit. If they typed anything else, then a message appeared telling them there was an erroneous entry.
几年前,我在学校编写 asm 时遇到了同样的问题。我遇到的问题是我想将“退出”一词与用户条目进行比较。如果他们输入“exit”,应用程序就会退出。如果他们输入任何其他内容,则会出现一条消息,告诉他们输入错误。
The way I solved the issue was to compare the strings character-by-character, until the characters no longer matched, or vice-versa.
我解决这个问题的方法是逐个字符地比较字符串,直到字符不再匹配,反之亦然。
This also might be of some use. Here are some code examples relating to string comparison in assembly language: http://www.daniweb.com/software-development/assembly/threads/58667/assembly-language-comparing-strings
这也可能有一些用处。以下是一些与汇编语言中的字符串比较相关的代码示例:http: //www.daniweb.com/software-development/assembly/threads/58667/assembly-language-comparing-strings
You might also want to look at this article relating to regular expressions (regex) in assembly language: Regular Expressions and Assembly
您可能还想查看有关汇编语言中的正则表达式 (regex) 的这篇文章:Regular Expressions and Assembly
回答by Mehrab Patwary Munna
j is jump l is less e is equel if bl=a & bh=k here cmp is work like a-k
j is jump l is less e is equel if bl=a & bh=k 这里 cmp 和 ak 一样工作
if here 0 or less-equal value then jle work. then n will work. if positive value jle is not work.
如果此处为 0 或更少的相等值,则 jle 工作。那么 n 会起作用。如果正值 jle 不起作用。
cmp bl,bh jle n
cmp bl,bh jle n
mov ah,2 mov dl,bh int 21h
mov ah,2 mov dl,bh int 21h
n: mov ah,2 mov dl,bl int 21h
n: mov ah,2 mov dl,bl int 21h