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

.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发布于 2015-05-23 08:02:11
看看你的第一段代码:
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你从用户那里得到一个字符,然后什么也不做,除非用户按回车键。
我不确定您想要用这行代码实现什么:
LD R3, Negate当Negate是一个位置标签时,将x96FF的"NOT R3,R3“值加载到R3中没有多大意义。
我建议在对字符进行检查之前存储用户的输入。
https://stackoverflow.com/questions/30386227
复制相似问题