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

时间:2016-03-17 16:36:36

标签: assembly masm

我输入了四个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 个答案:

答案 0 :(得分:3)

您正在比较

中的两个内存操作数
yes '' | awk -F '|'  'NF != 35 {print NR, $0} ' test.txt

的指令。在Intel ISA上,你不能这样做。一个参数必须始终是一个寄存器,请参阅CMP here

所以解决方法是首先将CMP chX, chY ;Compare ch2,ch3 一个参数放入寄存器,然后应用MOV

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

CMP