0

Program Description

I used .BLKW to allocate 20 locations for each character that the user inputs and for now, I just want to display the string the user typed at the first prompt. (This will be a pig latin translator, hence the second prompt; but right now I just want to see if I can print out the user input)

The Problem

The problem is that when I run it, I get extra characters at the end.

For example:

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

My Program

.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

Attempted Solution

I was thinking that the problem was that R4 holds the string of the first user input throughout the whole program. So for a solution, I thought about clearing R4 after it is displayed so that it's ready to take the next user input. Does anyone know how I would do that?

4

2 回答 2

0

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”值,这样当调用 PUT 时它将停止在零值处。

于 2015-04-22T22:43:41.440 回答
0

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

第一次运行它时,内存将包含 ['A','P','P','L','E'],如果您在加载程序时没有随机化内存内容,则后跟零。这意味着 PUTS 调用将返回“APPLE”。当你输入新单词时,它并没有清除记忆,所以输入“at”将导致 ['A','T','P','L','E'] 和你的打印例程将打印“ATPLE”。

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

于 2015-04-16T00:23:45.110 回答