首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LC3程序集回文错误

LC3程序集回文错误
EN

Stack Overflow用户
提问于 2015-05-22 07:48:10
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
.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
EN

回答 1

Stack Overflow用户

发布于 2015-05-23 08:02:11

看看你的第一段代码:

代码语言:javascript
复制
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

你从用户那里得到一个字符,然后什么也不做,除非用户按回车键。

我不确定您想要用这行代码实现什么:

代码语言:javascript
复制
LD  R3, Negate

当Negate是一个位置标签时,将x96FF的"NOT R3,R3“值加载到R3中没有多大意义。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30386227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档