程序集x86 - 与REPE CMPSB有麻烦

时间:2017-02-15 17:08:38

标签: assembly emu8086

我在程序集中创建了一个字符串搜索程序,接受两个字符串, STRING和FIND,以及相应的大小(开头为N),我想创建一个MEMCMP PROC。这个MEMCMP在SI和DI中检索两个指向字符串的指针,并将AX指向比较的大小。 由于“The”在“快速棕色......”的开头,我期望CALL MEMCMP打开ZF,因为它只用两个字符串复制CMP A,B。但它没有,并且它遍历所有字符串并且无法在“STRING”中找到“FIND”的索引。谁能告诉我为什么? 问题在于MEMCMP,其余代码用于显示MEMCMP的使用。 :

DSEG SEGMENT
    STRING  DB "The quick brown fox jumped over the lazy dog", 0
    NSTRING DW OFFSET NSTRING - OFFSET STRING
    FIND    DB "The"      
    NFIND   DW OFFSET NFIND - OFFSET FIND
DSEG ENDS

SSEG SEGMENT STACK
    DW 100H DUP(?)
SSEG ENDS

CSEG SEGMENT
    ASSUME CS:CSEG, DS:DSEG, SS:SSEG ; Assume

    ; Gets 3 parameters from the registers:
    ; (SI = &str1, DI = &str2, AX= size)
    ; And compares them according to size.
    MEMCMP PROC NEAR 
        ; Saving register data.
        PUSHA

        CLD         ; Scan in the forward direction.        
        MOV CX, AX  ; Counter from AX.
        REPE CMPSB  ; Repeating comparison.

        ; Restoring register data.
        POPA
        RET
    MEMCMP ENDP

    MAIN PROC FAR                 ; Creating the main process.
        PUSH DS
        PUSH ES
        MOV AX, 0
        PUSH AX
        MOV AX, DSEG
        MOV DS, AX
        MOV ES, AX
        MOV AX, NFIND             ; Saving NFIND for MEMCMP
        MOV CX, NSTRING           ; Going NSTRING times. 

    FIND_LOOP:                    ; Finding the index of "FIND".
        MOV BX, NSTRING           ; Getting NSTRING.
        SUB BX, CX                ; Getting the current index.
        MOV DX, BX                ; Saving current index.
        MOV SI, OFFSET STRING[BX] ; Setting the SI to the string's current index.
        MOV DI, OFFSET FIND       ; Setting the DI to the start of find.
        CALL MEMCMP               ; Comparing memory.
        JE   END_FIND_LOOP        ; We found a comparison. 
        JNE  CONTINUE_FIND_LOOP   ; We haven't found a comparison.
    CONTINUE_FIND_LOOP:
        CMP  CX, NFIND            ; Comparing CX and NFIND.
        JL   END_FIND_LOOP        ; Found an error, if less than NFIND bytes are left!
        LOOP FIND_LOOP            ; Looping again, finding through another character.

    END_FIND_LOOP:                ; DX now holds the index of the item.
        ; Done! ;
        RET
    MAIN ENDP
CSEG ENDS
END MAIN

0 个答案:

没有答案