为什么我会得到额外的字符?

Why am I getting extra characters?

计划说明

我使用 .BLKW 为用户输入的每个字符分配 20 个位置,现在,我只想显示用户在第一个提示时键入的字符串。 (这将是一个猪拉丁翻译器,因此是第二个提示;但现在我只想看看我是否可以打印出用户输入)

问题

问题是当我 运行 它时,我在最后得到额外的字符。

例如:

English Word: apple
Pig-Latin Word: apple
English Word: at
Pig-Latin Word: atple
English Word: set
Pig-Latin Word: setle

我的程序

.ORIG x3000
START ST R1,SAVER1
ST R2,SAVER2
ST R3,SAVER3

LD R5,ENTER

REPEAT LEA R0,PROMPT          ; loading the starting address of prompt
PUTS                   ; displays PROMPT on screen


LEA R4,ENGLWORD        ; sets aside memory locations for typed characters
INPUT GETC             ; now that user has typed, read char into R0
ADD R6,R5,R0           ; adds the negative value of the ASCII enter key code to the input character
BRz PIGPROMPT          ; if the sum of the ASCII codes from step before is 0, that means user pressed enter so go to PIGPROMPT
OUT                    ; write char in R0 to console
STR R0,R4,#0           ; store typed character into memory location
ADD R4,R4,#1           ; increment memory location so you write next character to the next location
BRnzp INPUT            ; break no matter what to the INPUT step to receive next typed character

PIGPROMPT LEA R0,PIG             ; loads starting address of pig latin prompt
PUTS                             ; displays pig latin prompt on screen
LEA R0,ENGLWORD
PUTS
BRnzp REPEAT

LD R1,SAVER1           ; restore R1 to original value
LD R2,SAVER2           ; restore R2 to original value
LD R3,SAVER3           ; restore R3 to original value

HALT

SAVER1 .BLKW 1         ; allocates 1 memory location for SAVER1
SAVER2 .BLKW 1         ; allocates 1 memory location for SAVER2
SAVER3 .BLKW 1         ; allocates 1 memory location for SAVER3
ENGLWORD .BLKW #20

ENTER .FILL xFFF6      ; the negative value of the ASCII code for the enter key
NEWLINE .FILL x000A

PROMPT .STRINGZ "\nEnglish Word: "          ; initializes a sequence of stringLength+1 memory locations to hold string
PIG .STRINGZ "\nPig-Latin Word: "
DSR .FILL xFE04                           
DDR .FILL xFE06
KBSR .FILL xFE00
KBDR .FILL xFE02
.END

尝试的解决方案

我在想,问题出在整个程序中,R4 保存着第一个用户输入的字符串。因此,对于一个解决方案,我考虑在显示后清除 R4,以便它准备好接受下一个用户输入。有谁知道我会怎么做?

这里的关键是 PUTS 的工作原理——它打印从 R0 中的地址开始的所有字符,直到它到达 0('\0' 而不是 '0')。

第一次运行它,内存将包含['A','P','P','L','E' ],如果您在加载程序时没有随机化内存内容,则后跟零。这意味着 PUTS 调用将 return "APPLE"。当您输入新单词时,它不会清除内存,因此输入 "at" 将导致 ['A'、'T'、'P'、'L' ,'E'],您的打印例程将打印 "ATPLE".

为了正确完成单词,您需要在要打印的最后一个字符之后的元素中添加一个'\0' (a.k.a. 0)。换句话说,如果您的内存包含 ['A'、'T'、'\0'、'L'、'E'],您的打印例程将打印 "AT"。

Aqua 说得对,PUTs 命令正在寻找一个零以停止将字符打印到屏幕上。我在 PIGPROMPT 之后添加了两行代码,它似乎按预期工作。

修订:

.ORIG x3000
START ST R1,SAVER1
ST R2,SAVER2
ST R3,SAVER3

LD R5,ENTER

REPEAT LEA R0,PROMPT          ; loading the starting address of prompt
PUTS                   ; displays PROMPT on screen


LEA R4,ENGLWORD        ; sets aside memory locations for typed characters
INPUT GETC             ; now that user has typed, read char into R0
ADD R6,R5,R0           ; adds the negative value of the ASCII enter keycode to the input character
BRz PIGPROMPT          ; if the sum of the ASCII codes from step before is 0, that means user pressed enter so go to PIGPROMPT
OUT                    ; write char in R0 to console
STR R0,R4,#0           ; store typed character into memory location
ADD R4,R4,#1           ; increment memory location so you write next character to the next location
BRnzp INPUT            ; break no matter what to the INPUT step to receive next typed character

PIGPROMPT AND R0, R0, #0    ; clear R0
STR R0,R4,#0                ; store typed character into memory location
LEA R0,PIG                  ; loads starting address of pig latin prompt
PUTS                             ; displays pig latin prompt on screen
LEA R0,ENGLWORD
PUTS
BRnzp REPEAT

LD R1,SAVER1           ; restore R1 to original value
LD R2,SAVER2           ; restore R2 to original value
LD R3,SAVER3           ; restore R3 to original value

HALT

SAVER1 .BLKW 1         ; allocates 1 memory location for SAVER1
SAVER2 .BLKW 1         ; allocates 1 memory location for SAVER2
SAVER3 .BLKW 1         ; allocates 1 memory location for SAVER3
ENGLWORD .BLKW #20

ENTER .FILL xFFF6      ; the negative value of the ASCII code for the enter key
NEWLINE .FILL x000A

PROMPT .STRINGZ "\nEnglish Word: "          ; initializes a sequence of stringLength+1 memory locations to hold string
PIG .STRINGZ "\nPig-Latin Word: "
DSR .FILL xFE04                           
DDR .FILL xFE06
KBSR .FILL xFE00
KBDR .FILL xFE02
.END

我所做的只是在用户字符串的末尾存储一个“0”值,这样当调用 PUTs 时它将停在零值处。