lc3 - Why am I getting extra characters? -
program description
i used .blkw allocate 20 locations each character user inputs , now, want display string user typed @ first prompt. (this pig latin translator, hence second prompt; right want see if can print out user input)
the problem
the problem when run it, characters @ end.
for example:
english word: apple pig-latin word: apple english word: @ 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 starting address of prompt puts ; displays prompt on screen lea r4,englword ; sets aside memory locations typed characters input getc ; user has typed, read char r0 add r6,r5,r0 ; adds negative value of ascii enter key code input character brz pigprompt ; if sum of ascii codes step before 0, means user pressed enter go pigprompt out ; write char in r0 console str r0,r4,#0 ; store typed character memory location add r4,r4,#1 ; increment memory location write next character next location brnzp input ; break no matter input step 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 original value ld r2,saver2 ; restore r2 original value ld r3,saver3 ; restore r3 original value halt saver1 .blkw 1 ; allocates 1 memory location saver1 saver2 .blkw 1 ; allocates 1 memory location saver2 saver3 .blkw 1 ; allocates 1 memory location saver3 englword .blkw #20 enter .fill xfff6 ; negative value of ascii code enter key newline .fill x000a prompt .stringz "\nenglish word: " ; initializes sequence of stringlength+1 memory locations hold string pig .stringz "\npig-latin word: " dsr .fill xfe04 ddr .fill xfe06 kbsr .fill xfe00 kbdr .fill xfe02 .end
attempted solution
i thinking problem r4 holds string of first user input throughout whole program. solution, thought clearing r4 after displayed it's ready take next user input. know how that?
the key here how puts works -- prints characters starting @ address in r0 until reaches 0 ('\0' not '0').
the first time run it, memory contain ['a','p','p','l','e'], followed zeroes if didn't randomize memory contents when loaded program. means puts call return "apple". when enter new word, doesn't clear out memory, entering "at" result in ['a','t','p','l','e'], , print routine print "atple".
in order finish word, need add '\0' (a.k.a. 0) element after last character print. in other words, if memory contains ['a','t','\0','l','e'], print routine print "at".
Comments
Post a Comment