LC-3汇编程序无法处理用户输入,请求代码修正与完善指导
LC-3汇编程序调试与完成求助
功能需求
- 提示用户输入LC-3汇编指令(按回车结束)
- 合法指令输出对应4位opcode
- 非法指令输出错误信息
- 输出后循环重启
- 输入
QUIT则退出
当前问题
现有代码仅能打印提示信息,但输入ADD等指令无响应,输入QUIT也无法触发退出,不知道如何完成剩余功能,需要帮助。
当前代码
.ORIG x3000 ; Constants for comparison ADD_STR .STRINGZ "ADD" AND_STR .STRINGZ "AND" JMP_STR .STRINGZ "JMP" BR_STR .STRINGZ "BR" QUIT_STR .STRINGZ "QUIT" ; Memory locations MESSAGE .STRINGZ "Enter an LC-3 assembly language instruction (type QUIT to exit): " USER_INPUT .BLKW #20 MAIN LEA R0, MESSAGE ; Display the prompt message PUTS ; Read user input LEA R0, USER_INPUT ; Store user input here GETC ; Read the first character OUT ; Echo the character ; Loop to read input until Enter/Return is pressed LOOP GETC ; Read the next character OUT ; Echo the character BRZ END_LOOP ; Exit loop if Enter/Return is pressed STR R0, R1, #0 ; Store the character in memory ADD R1, R1, #1 ; Move to the next memory location BRNZP LOOP ; Repeat the loop END_LOOP ; Compare user input to known instructions LEA R1, USER_INPUT ; Load the input string pointer LEA R2, ADD_STR ; Load ADD instruction pointer LEA R3, AND_STR ; Load AND instruction pointer LEA R4, JMP_STR ; Load JMP instruction pointer LEA R5, BR_STR ; Load BR instruction pointer LEA R6, QUIT_STR ; Load QUIT instruction pointer ; Compare input with ADD CMP_ADD LDR R7, R1, #0 ; Load a character from input LDR R0, R2, #0 ; Load a character from ADD string NOT R0, R0 ; Get the 2's complement of ADD character ADD R0, R0, #1 ; Add 1 to compare with input character ADD R7, R7, #1 ; Prepare to compare next character BRnp ADD_CHECK ; Check condition ADD_CHECK BRz IS_ADD ; If both characters match, it's ADD instruction BRnzp CMP_AND ; Else, check for AND instruction IS_ADD ; Display the opcode for ADD LEA R0, ADD_STR ; Load ADD instruction string PUTS ; Display it BRnzp MAIN ; Go back to the main loop CMP_AND ; Similar comparison process for AND ; ... (Repeat the above process for AND, JMP, BR, QUIT) ; Handling for other instructions and errors can be written similarly ; Handle QUIT instruction QUIT_CHECK ; ... ; Other instruction checks and error messages go here ; Loop back to the beginning BRnzp MAIN .END
问题分析与修复方案
关键问题点
- 输入初始化缺失:读取输入时未将
R1初始化为USER_INPUT的起始地址,导致字符无法正确存入输入缓冲区。 - 输入未加结束符:输入完成后没有在缓冲区末尾写入字符串结束符
\0(x00),后续字符串比较无法识别输入结束。 - 字符串比较逻辑错误:原比较逻辑未逐字符对比直到结束符,分支条件错误,无法正确匹配指令。
- QUIT处理未实现:缺少
QUIT指令的匹配逻辑和退出程序的代码。
修复后完整代码
.ORIG x3000 ; Constants for comparison and output ADD_STR .STRINGZ "ADD" AND_STR .STRINGZ "AND" JMP_STR .STRINGZ "JMP" BR_STR .STRINGZ "BR" QUIT_STR .STRINGZ "QUIT" OPCODE_ADD .STRINGZ "Opcode: 0001\n" OPCODE_AND .STRINGZ "Opcode: 0101\n" OPCODE_JMP .STRINGZ "Opcode: 1100\n" OPCODE_BR .STRINGZ "Opcode: 0000\n" ERROR_MSG .STRINGZ "Error: Invalid instruction\n" NEWLINE .STRINGZ "\n" ; Memory locations MESSAGE .STRINGZ "Enter an LC-3 assembly language instruction (type QUIT to exit): " USER_INPUT .BLKW #20 MAIN ; Clear input buffer (optional but good practice) LEA R1, USER_INPUT CLEAR_BUFFER STR R0, R1, #0 ; Store x00 to clear ADD R1, R1, #1 ADD R2, R1, #-20 ; Check if we've cleared 20 bytes BRnp CLEAR_BUFFER ; Display prompt LEA R0, MESSAGE PUTS ; Read user input LEA R1, USER_INPUT ; R1 points to input buffer start GETC OUT STR R0, R1, #0 ; Store first character ADD R1, R1, #1 LOOP_INPUT GETC OUT BRz END_INPUT ; Exit loop on Enter (ASCII x00) STR R0, R1, #0 ; Store character ADD R1, R1, #1 BRNZP LOOP_INPUT END_INPUT ; Add string terminator to input AND R0, R0, #0 ; R0 = x00 STR R0, R1, #0 ; Check for QUIT first LEA R1, USER_INPUT LEA R2, QUIT_STR JSR STRING_COMPARE BRz EXIT_PROGRAM ; Check ADD LEA R1, USER_INPUT LEA R2, ADD_STR JSR STRING_COMPARE BRz PRINT_ADD_OPCODE ; Check AND LEA R1, USER_INPUT LEA R2, AND_STR JSR STRING_COMPARE BRz PRINT_AND_OPCODE ; Check JMP LEA R1, USER_INPUT LEA R2, JMP_STR JSR STRING_COMPARE BRz PRINT_JMP_OPCODE ; Check BR LEA R1, USER_INPUT LEA R2, BR_STR JSR STRING_COMPARE BRz PRINT_BR_OPCODE ; If none matched, print error LEA R0, ERROR_MSG PUTS BRNZP MAIN PRINT_ADD_OPCODE LEA R0, OPCODE_ADD PUTS BRNZP MAIN PRINT_AND_OPCODE LEA R0, OPCODE_AND PUTS BRNZP MAIN PRINT_JMP_OPCODE LEA R0, OPCODE_JMP PUTS BRNZP MAIN PRINT_BR_OPCODE LEA R0, OPCODE_BR PUTS BRNZP MAIN EXIT_PROGRAM LEA R0, NEWLINE PUTS HALT ; Exit program ; String comparison subroutine: R1 = string1, R2 = string2 ; Returns Z=1 if equal, Z=0 otherwise STRING_COMPARE COMPARE_LOOP LDR R3, R1, #0 ; Get char from string1 LDR R4, R2, #0 ; Get char from string2 NOT R4, R4 ADD R4, R4, #1 ; 2's complement of string2 char ADD R5, R3, R4 ; Compare chars BRnp COMPARE_END ; Chars don't match BRz COMPARE_END ; Both are null terminator ADD R1, R1, #1 ; Next char ADD R2, R2, #1 BRNZP COMPARE_LOOP COMPARE_END RET .END
代码说明
- 输入处理修复:初始化
R1为输入缓冲区起始地址,输入结束后添加\0结束符,确保字符串操作正常。 - 通用字符串比较子程序:提取
STRING_COMPARE子程序,避免重复代码,统一处理字符串匹配逻辑。 - QUIT处理:优先检查
QUIT指令,匹配后调用HALT退出程序。 - Opcode输出:为每个合法指令定义对应的opcode字符串,匹配后直接输出。
- 错误处理:所有合法指令匹配失败后,输出错误信息。
内容的提问来源于stack exchange,提问作者Felipe Cotrina
相关产品推荐
相关产品推荐

