[Assembly] String comparison

Comparison of strings in assembly language:

1. Send the first address of the two strings into the SI and DI registers respectively

2. Send the length of the template string to the CX register

3. Clear the direction flag bit, use the CLD instruction, and set the string pointer to auto increment

4. Use the automatic comparison instruction REPZ CMPSB to compare two strings

The following sample program specifies a template string, and the comparison string is input through the system function call:

The settings for the string buffer are:


; You may customize this and other start-up templates; 
; The locaweixin_46918378.//32tion of this template is c:\emu8086\inc\0_com_template.txt

DATA SEGMENT
    STR1 DB 80 ;定义
               ;接收字符串str1的缓冲区
         DB 0
         DB 80 DUP(0) 
    STR2 DB 'XCOPY.EXE' ;STR2 中的内容
    NUM EQU $-STR2  ;获取STR2的长度
    DATA ENDS
SSEG SEGMENT STACK
    STACK DB 20 DUP(0)
    SSEG ENDS
CSEG SEGMENT 'CODE'                
    ASSUME CS:CSEG,DS:DATA,SS:SSEG,ES:DATA
    START : MOV AX,DATA
            MOV DS,AX
            MOV ES,AX ;建立附加数据段
            
            LEA DX,STR1
            MOV AH,10   ;输入字符串到STR1
            INT 21H
            XOR AH,AH
            MOV AL,STR1+1  ;取实际长度值
            CMP AX,NUM
            JNE EXIT   ;长度不等,转EXIT
            LEA SI,STR1+2
            LEA DI,STR2
            MOV CX,NUM  ;串长度送到CX
            CLD
            REPZ CMPSB
            JNE EXIT
            MOV BX,0
            JMP END1
    EXIT:   MOV BX,0FFFFH  ; 不相等
            MOV AH,4CH
            INT 21H 
    END1:   MOV BX,0H  ; 相等
            MOV AH,4CH
            INT 21H   
CSEG ENDS
END START




 

Guess you like

Origin blog.csdn.net/mid_Faker/article/details/112388923