检查字符串是否为palindrom

时间:2016-04-12 17:31:53

标签: assembly x86 dos

我无法理解此代码在这些特定行中的作用:

DISPLAY MSG2之后,如何知道用DISPLAY P11打印消息?

DISPLAY MSG3之后如何知道这4行打印数组的长度? L1应该是" ?"它是如何获得长度的?

 MOV DL,L1
 ADD DL,30H
 MOV AH,2
 INT 21H

我认为我对MACROLEA缺乏了解。但我知道LEAMOV blabla, OFFSET blabla2

得到了高度赞赏。

  .MODEL SMALL
    .STACK 100h
    .DATA

        MSG1 DB 10,13,'ENTER ANY STRING :- $'
        MSG2 DB 10,13,'ENTERED STRING IS :- $'
        MSG3 DB 10,13,'LENGTH OF STRING IS :- $'
        MSG4 DB 10,13,'NO, GIVEN STRING IS NOT A PALINDROME $' 
        MSG5 DB 10,13,'THE GIVEN STRING IS A PALINDROME $'
        MSG6 DB 10,13,'REVERSE OF ENTERED STRING IS :- $'   

        P1 LABEL BYTE   ;start of label byte
        M1 DB 0FFH      ;assign maximum length of array
        L1 DB ?     ;length of string 

        P11 DB 0FFH DUP ('$')  ;init array (max 256)
        P22 DB 0FFH DUP ('$')  ;init array (max 256)

    DISPLAY MACRO MSG   ;Called like that: <Display argument>
        MOV AH,9
        LEA DX,MSG
        INT 21H
    ENDM   

    .CODE

    START:
            MOV AX,@DATA  ;Assign data to AX
            MOV DS,AX     ;Assign data to Data Segment           

            DISPLAY MSG1  ; Enter string

            LEA DX,P1 ;DX points to P1's offset
            MOV AH,0AH ; buffer filled with user input   
            INT 21H

            DISPLAY MSG2  ;The entered string is:

            DISPLAY P11   ;This will display the string

            DISPLAY MSG3  ;Display length

            ;Ap

ply string's length to DL,
        ; covert from ASCII to DEC, and output the length value.
        MOV DL,L1
        ADD DL,30H
        MOV AH,2
        INT 21H

        DISPLAY MSG6

        ;initialize P11 to SI register and P22 to DI register       
        LEA SI,P11
        LEA DI,P22

        ;Jump SI to the end of string
        MOV DL,L1
        DEC DL
        MOV DH,0
        ADD SI,DX                  

        ;Move length of actual string to CL, and apply CH = 0
        MOV CL,L1
        MOV CH,0    
        ;CX = string's length.

REVERSE:    
        ;Put P11's REVERESED string into P22 string
        MOV AL,[SI]
        MOV [DI],AL
        INC DI
        DEC SI
        LOOP REVERSE

        DISPLAY P22  ; Display the reversed string

        ;Re-assign P11,P22 to SI,DI registers              
        LEA SI,P11
        LEA DI,P22   

        ;Move length of actual string to CL, and apply CH = 0
        MOV CL,L1
        MOV CH,0    
        ;CX = string's length.

CHECK:
        MOV AL,[SI]
        CMP [DI],AL
        JNE NOTPALIN
        INC DI
        INC SI
        LOOP CHECK


        DISPLAY MSG5
        JMP EXIT
NOTPALIN:
        DISPLAY MSG4

EXIT:   MOV AH,4CH
        INT 21H
CODE ENDS
END START

1 个答案:

答案 0 :(得分:1)

当你拨打function 0Ah in int 21h时,你会给它一个特定格式的缓冲区。第一个字节(此处为L1)表示缓冲区有多少字节,第二个字节将具有读取的字节数(此处为P11),之后将读取实际字符的字节数({ {1}}这里)。

因此在调用读取函数L1之后,初始化读取的字符数。