Search code examples
lc3

I can't find out why the loop isn't stopping-LC3


I'm just a super newbie like I just learn how to do this just for 12 hrs I was wondering why my loop is not stopping. Can you help me find what is wrong. I know this code is garbage, please bear with me.

So our task is to ask the user to ask the user input a string with max 80 characters and should end with period since it is our basis to know if it is the end of the string. The program will count the characters and words and display it, but in my case the program doesn't stop. Please help.

.ORIG X3000

LEA R0, PROMPT_ENTER ;Message for entering number.
PUTS
LEA R2, SENTENCE ;allocated memory
AND R3, R3, #0 ;setting R3 to zero for word counter.
ADD R3, R3, #1
AND R1, R1, #0 ;setting R4 to zero for char counter.

;---------ASKING USER TO INPUT A SENTENCE------
GET_USER_INPUT: ;loop for getting characters.               
GETC                                     
OUT                                  
STR R0, R2, #0   ;r0 -> ( memory address stored in r2 + 0 )
PUT                             
ADD R2, R2, #1    ;increments the memory pointer                  
ADD R0, R0, #-10    ;decrements loop to proceed when pressed enter.
BRz COUNT_LENGTH
BRnp GET_USER_INPUT 



;--------Element counter----
COUNT_LENGTH:
AND R0, R0, #0
LEA R4, SENTENCE
LDR R0, R4, #0
ADD R0, R0, #-10
BRz EMPTY
BRnp COUNT_ELEMENTS

EMPTY:
AND R0, R0, #0
LEA R0, PROMPT_NULL
PUTS
HALT


COUNT_ELEMENTS:
AND R0, R0, #0
LEA R4, SENTENCE
LDR R0, R4, #0
LD R6, TMNT
ADD R0, R0, R6
BRz END_OF_SENTENCE
LDR R0, R4, #0
LD R6, SPACE
ADD R0, R0, R6
BRz WORD_COUNT
ADD R4, R4, #1
ADD R1, R1, #1
BRnp COUNT_ELEMENTS

WORD_COUNT:
ADD R4, R4, #1
ADD R3, R3, #1
JSR COUNT_ELEMENTS

END_OF_SENTENCE:
AND R0, R0, #0
LDR R3, R3, #0
LD R5, ASCII
ADD R0, R0, R5
OUT
AND R0, R0, #0
LDR R1, R1, #0
ADD R0, R0, R1
OUT

HALT

SENTENCE .BLKW #80 ;initialize the array named sentence with length 80
TMNT .fill #-89
SPACE .fill #-32
ASCII .fill #48
;----MESSAGES------
PROMPT_ENTER .stringz "Enter the word(maximum 80 characters): \n"
PROMPT_AGAIN .stringz "Do you want to try again? Y/N: \n"
PROMPT_NULL .stringz "Error: Please enter a sentence!"
PROMPT_NOTMNT .stringz "Error: No terminating symbol (.) is expected at the end!"
PROMPT_DSPACE .stringz "Error: Multiple white space is not allowed!"
.END

Solution

  • I've only skimmed though this code.

    it is an infinite loop because you reset R4 to point to the start of SENTENCE in each iteration of COUNT_ELEMENTS.

    I can see in your code where you are incrementing R4 before going back to COUNT_ELEMENTS (btw JSR is only used to call a subroutine if you want to Branch unconditionally use BR).

    You'd want to set R4 to point to SENTENCE only once. I do believe you can simply remove the LEA R4, SENTENCE within COUNT_ELEMENTS since it was set previously as part of COUNT_LENGTH.

    In the future I would recommend pulling your code up in a lc3 simulator and stepping through it examining the values of the registers as you step though.