简体   繁体   中英

How to compare two strings assembly

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

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.

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

You might also want to look at this article relating to regular expressions (regex) in assembly language: Regular Expressions and Assembly

j is jump l is less e is equel if bl=a & bh=k here cmp is work like ak

if here 0 or less-equal value then jle work. then n will work. if positive value jle is not work.

cmp bl,bh jle n

mov ah,2 mov dl,bh int 21h

n: mov ah,2 mov dl,bl int 21h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM