LC3装配回文错误

时间:2015-05-21 23:48:10

标签: assembly lc3

我使用LC3汇编语言来构建回文程序,但是当它运行时会打印出许多奇怪的字符。这是LC3装配中的回文:

enter image description here

.ORIG x3000
            LEA     R0, Message     ; display a message
            PUTS
            LEA     R1, FirstChar   ; R1 points to the first
                                    ; character which will be entered

; the loop for echoing user's input and deciding whether the string is
; a palindrome
Next        LD  R2, LF_ASCII
            GETC                    ; read in one character
            OUT                     ; write character entered
            ADD R3, R1, 1           ; R3 points to the next character
            ADD R4, R0, R2          ; check whether the input is LF
            BRnp    Next            ; if input is LF check whether it's,
                                    ; a palindrome, otherwise go
                                    ; back to NEXT
            ADD R3, R3, -2

Check       LD  R3, Negate          ; negate the value of the Last char
            ADD R5, R1, R3          ; check whether first and last chars
                                    ; are equal
            BRz NextChar            ; if they are, check the next characters,
                                    ; otherwise the string isn't a palindrome
            LEA R6, NotPalindrome
            PUTS
            BRnzp Done

Negate      NOT R3, R3              ; negate R3
            ADD R3, R3, 1           ; 2's complement
            RET                     ; return


NextChar    ADD R1, R1, 1           ; increment the first pointer
            ADD R3, R3, -1          ; decrement the second pointer
            BRp Check               ; check whether the string is done
            LEA R6, IsPalindrome    ; the string is a palindrome
            PUTS
            BRnzp   Done

Done        HALT

Message         .STRINGZ    "Please enter a string: "
LF_ASCII        .FILL       -10
FirstChar       .BLKW       10
IsPalindrome    .STRINGZ    "The string is a palindrome."
NotPalindrome   .STRINGZ    "The string is not a palindrome."
.END

1 个答案:

答案 0 :(得分:0)

查看您的第一个代码块:

it = two.find(pairs[i] );

除非用户按ENTER键,否则您将从用户获取一个字符,然后不对其执行任何操作。

我不确定你用这行代码完成了什么:

Next        LD  R2, LF_ASCII
        GETC                    ; read in one character
        OUT                     ; write character entered
        ADD R3, R1, 1           ; R3 points to the next character
        ADD R4, R0, R2          ; check whether the input is LF
        BRnp    Next            ; if input is LF check whether it's,
                                ; a palindrome, otherwise go
                                ; back to NEXT

当Negate是位置标签时,您正在加载" NOT R3,R3"这是x96FF到R3,没有多大意义。

我建议在对字符进行检查之前存储用户的输入。