代码之家  ›  专栏  ›  技术社区  ›  Ilan Aizelman WS

MASM:检查4个字符是否为回文

  •  -1
  • Ilan Aizelman WS  · 技术社区  · 8 年前

    我输入了四个ASCII字符: ch1, ch2, ch3, ch4 .

    我想检查4个字母的单词是否构成回文。

    算法是正确的,但无法编译。你知道为什么吗?

    ;
    ; isPalindrom.asm 
    ;
        .MODEL SMALL
        .STACK 100h
        .DATA
        ch1 DB ? ;var dec
        ch2 DB ? ;var dec
        ch3 DB ? ;var dec
        ch4 DB ? ;var dec
    PrintUSER DB 13,10
             DB 'Pls enter 4 chars:',13,10,'$' 
             ;Print 4 chars string
    
    PaliExists DB 13,10
             DB 'Palindrome permutation exists',13,10,'$'
             ;Print msg that pali exists
    
    PaliNOTExists DB 13,10
             DB 'There is no possible solution',13,10,'$'
             ;Print msg that pali does not exist
    
         .CODE
    ProgStart:
         MOV AX,@DATA             ; DS can be written to only through a register
         MOV DS,AX                ; Set DS to point to data segment
    
         MOV AH,9
         MOV DX, Offset PrintUSER
         INT 21h 
    
         MOV AH,1 
         INT 21h                  ; Input to ch1
         MOV ch1,AL
    
         MOV AH,1
         INT 21h                  ; Input to ch2
         MOV ch2,AL
    
         MOV AH,1
         INT 21h                  ; Input to ch3
         MOV ch3,AL
    
         MOV AH,1
         INT 21h   
         MOV ch4,AL      ; Input to ch4
         ;////////////////////////////////
    
         CMP ch1, ch2 ; Compare ch1,ch2
         JE checkThreeFour ; jump to label checkTwoThree
         JMP Skip1 ;Skip compare ch3,ch4
    
         checkThreeFour: ;label
         CMP ch3, ch4 ; Compare ch3,ch4
         JE Exists ; Jump to Exists
         JMP notExists
    
         Skip1:
         CMP ch1, ch3 ;Compare ch1,ch3
         JE checkTwoFour ;Jump
         JMP Skip2:
    
         checkTwoFour:
         CMP ch2, ch4 ;Compare ch2,ch4
         JE Exists ; Jump to Exists
         JMP notExists
    
         Skip2:
         CMP ch1, ch4 ;Compare ch1,ch4
         JE checkTwoThree ;Jump
         JMP notExists 
    
        checkTwoThree: 
         CMP ch2, ch3 ;Compare ch2,ch3
         JMP Exists ; Jump to Exists
         JMP notExists
    
    Exists: ;print message for Existing pali
        MOV AH,9 
        MOV DX, Offset PaliExists 
        INT 21h
        JMP DisplayGreeting
    
    notExists: ; print messasge palindrom does not exist
        MOV AH,9
        MOV DX, Offset PaliNOTExists
        INT 21h
    
    
    DisplayGreeting:
         MOV AH,9                         ; Set print option for int 21h
         INT 21h                          ; Print  chosen message
         MOV AH,4Ch               ; Set terminate option for int 21h
         INT 21h                  ; Return to DOS (terminate program)
        END ProgStart
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   zx485 potemkin    8 年前

    您正在比较

    CMP chX, chY ;Compare ch2,ch3
    

    说明书在英特尔ISA上,您无法做到这一点。一个参数必须始终是寄存器,请参见 CMP here .

    因此,解决方案是 MOV 先将一个参数输入寄存器,然后应用 CMP .

    P.S.:检查4字符字符串是否为回文的简单解决方案是:

    mov eax, dword ptr [string]
    mov edx, eax
    bswap edx
    cmp eax, edx
    je palindrome
    no_palindrome:
    ...
    jmp end
    palindrome:
    ...
    end: