简体   繁体   中英

Checking if two strings are equal in assembly

The program is to check if the user entered password matches with the one specified directly in the program. Not able to understand why i always happen to get 'PASSWORD INCORRECT' when i try to input directly from the keyboard. When specifying the 'SRC' directly in the program the output seems to be perfect though.

.MODEL SMALL
.STACK 1000H

DISP MACRO MSG ;macro to display a string of characters
LEA DX,MSG
MOV AH,09H
INT 21H
ENDM

INPUT MACRO ;macro to input character by character
MOV AH,01H
INT 21H
ENDM

DATA SEGMENT 
CR EQU 0DH
LF EQU 0AH
MSG DB 'ENTER YOUR PASSWORD PLEASE : ',CR,LF,'$'
TRU DB 'PASSWORD CORRECT$'
FAL DB 'PASSWORD INCORRECT$'
SRC DB 10 DUP('$')
DEST DB 'YO$' 
LEN EQU ($-DEST)
DATA ENDS 


CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:DATA
START:  MOV AX,DATA
    MOV DS,AX
    MOV ES,AX
    MOV SI,OFFSET SRC
    MOV DI,OFFSET DEST  
    CLD
    MOV CX,LEN
    XOR BX,BX
    DISP MSG
RE: INPUT
    MOV [SI],AL
    INC SI
    INC BX
    CMP AL,CR
    JNE RE

    CMP BX,CX ;if string lengths dont match then the strings are unequal
    JNE L1

    MOV SI,OFFSET SRC
    REPE CMPSB
    JNZ L1
L2: DISP TRU
    JMP EXIT
L1: DISP FAL
EXIT:   MOV AH,4CH
    INT 21H
CODE    ENDS
    END START

Your check for whether the read character is a carriage return is placed after the character has been written to the SRC buffer. So when you compare the two strings later on, SRC will contain a CR character that DEST doesn't contain.

That is, if you entered YO you'll have DEST = 'YO$' , SRC = 'YO\\r', and LEN = 3 .

Here's a modified version of the input loop that works (new code is in lowercase):

RE: INPUT
    cmp al,CR
    je got_input  ; exit the loop if we read a CR character
    MOV [SI],AL
    INC SI
    INC BX
    jmp RE
got_input:
    inc bx     ; LEN includes the '$' character after 'YO', so increase bx by one to match that
    CMP BX,CX  ; if string lengths dont match then the strings are unequal
DATA SEGMENT
    STR1 DB "ENTER FIRST STRING HERE ->$"
    STR2 DB "ENTER SECOND STRING HERE ->$"
    STR11 DB "FIRST STRING : ->$"
    STR22 DB "SECOND STRING: ->$"

    INSTR1 DB 20 DUP("$")
    INSTR2 DB 20 DUP("$")
    NEWLINE DB 10,13,"$"
    N DB ?
    S DB ?
    MSG1 DB "BOTH STRING ARE SAME$"
    MSG2 DB "BOTH STRING ARE DIFFERENT$"

DATA ENDS

CODE SEGMENT

    ASSUME DS:DATA,CS:CODE
START:MOV AX,DATA
    MOV DS,AX

    LEA SI,INSTR1
    LEA DI,INSTR2

GET STRING
    MOV AH,09H
    LEA DX,STR1
    INT 21H

    MOV AH,0AH
    MOV DX,SI
    INT 21H


    MOV AH,09H
    LEA DX,NEWLINE
    INT 21H

    MOV AH,09H
    LEA DX,STR2
    INT 21H

    MOV AH,0AH
    MOV DX,DI
    INT 21H


    MOV AH,09H
    LEA DX,NEWLINE
    INT 21H


PRINT THE STRING

    MOV AH,09H
    LEA DX,STR11
    INT 21H

    MOV AH,09H
    LEA DX,INSTR1+2
    INT 21H

    MOV AH,09H
    LEA DX,NEWLINE
    INT 21H

    MOV AH,09H
    LEA DX,STR22
    INT 21H

    MOV AH,09H
    LEA DX,INSTR2+2
    INT 21H

    MOV AH,09H
    LEA DX,NEWLINE
    INT 21H

STRING COMPARISION
    MOV BX,00

    MOV BL,INSTR1+1
    MOV BH,INSTR2+1

    CMP BL,BH
    JNE L1

    ADD SI,2
    ADD DI,2

  L2:MOV BL,BYTE PTR[SI]
    CMP BYTE PTR[DI],BL
    JNE L1
    INC SI
    INC DI
    CMP BYTE PTR[DI],"$"
    JNE L2

    MOV AH,09H
    LEA DX,MSG1
    INT 21H

    JMP L5

  L1:MOV AH,09H
    LEA DX,MSG2
    INT 21H



 L5:
    MOV AH,09H
    LEA DX,NEWLINE
    INT 21H

    MOV AH,4CH
    INT 21H


CODE ENDS
END START


OUTPUT:
Z:\SEM3\SS\21-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->PATEL
FIRST STRING : ->ARPIT
SECOND STRING: ->PATEL
BOTH STRING ARE DIFFERENT

Z:\SEM3\SS\21-30>P29
ENTER FIRST STRING HERE ->ARPIT
ENTER SECOND STRING HERE ->ARPIT
FIRST STRING : ->ARPIT
SECOND STRING: ->ARPIT

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