LC3字符输入陷阱自定义提示:数字输入子程序实现咨询
Got it, let's tackle this LC-3 subroutine problem where we need to replace the default IN trap's prompt with our custom message. Here's how to fix and complete your code properly:
LC-3 Custom Input Prompt Subroutine
First, let's refine your existing code and build out the full solution, then break down how each part works.
Full Working Code
.ORIG x3000 ; Main program: Set up custom input handler, then trigger IN trap JSR SETUP_CUSTOM_IN TRAP x23 ; Calls our modified IN routine HALT ; Exit after input is received ; Subroutine to save registers (fixed address case and added R0) SAVE ST R7, SAVE_R7 ; Save return address ST R3, SAVE_R3 ST R2, SAVE_R2 ST R1, SAVE_R1 ST R0, SAVE_R0 ; R0 is used for output, so we need to save it too RET ; Subroutine to restore saved registers RESTORE LD R0, SAVE_R0 LD R1, SAVE_R1 LD R2, SAVE_R2 LD R3, SAVE_R3 LD R7, SAVE_R7 RET ; Subroutine to replace the default IN trap vector with our handler SETUP_CUSTOM_IN ST R7, SETUP_R7 ; Save return address for this setup routine ; Save the original IN trap routine address (stored at x0023) LD R0, TRAP_IN_ADDR LDR R1, R0, #0 ST R1, ORIG_IN_ROUTINE ; Overwrite the trap vector with our custom handler's address LD R1, CUSTOM_IN_ADDR STR R1, R0, #0 LD R7, SETUP_R7 ; Restore setup routine's return address RET ; Our custom IN trap handler: prints prompt, then uses original IN logic CUSTOM_IN_HANDLER JSR SAVE ; Save registers before modifying them ; Print our custom prompt using PUTS trap LD R0, PROMPT_ADDR TRAP x22 ; PUTS outputs the null-terminated string ; Call the original IN routine to get user input (stores char in R0) LD R0, ORIG_IN_ROUTINE JSRR R0 ; Jump to the saved original IN routine JSR RESTORE ; Restore all saved registers RTI ; Return from trap mode (required for trap handlers) ; Data storage section PROMPT_ADDR .FILL x3100 ; Address of our prompt string TRAP_IN_ADDR .FILL x0023 ; Address of the IN trap vector CUSTOM_IN_ADDR .FILL x3020 ; Address of our custom handler ORIG_IN_ROUTINE .BLKW 1 ; Holds the original IN routine address ; Saved registers storage SAVE_R0 .BLKW 1 SAVE_R1 .BLKW 1 SAVE_R2 .BLKW 1 SAVE_R3 .BLKW 1 SAVE_R7 .BLKW 1 SETUP_R7 .BLKW 1 ; Null-terminated prompt string (required for PUTS trap) .ORIG x3100 .STRINGZ "Please enter a number between 0 and 9:" .END
Key Breakdowns
- Register Safety: The
SAVEandRESTOREsubroutines ensure we don't overwrite any register values the main program relies on. We added R0 because we use it with the PUTS trap to print the prompt. - Trap Vector Modification: The
SETUP_CUSTOM_INroutine first saves the original IN trap address (stored atx0023), then replaces that vector with our custom handler. This makesTRAP x23run our code instead of the default. - Custom Prompt: We use the PUTS trap (
x22) to print our string, which is marked with.STRINGZto automatically add the null terminator PUTS needs to know when to stop printing. - Reusing Original IN: After printing the prompt, we jump to the original IN routine we saved—this lets us keep the standard input functionality (reading a character into R0) without rewriting it from scratch.
- RTI Instruction: Trap handlers must end with
RTIinstead ofRETto properly exit trap mode and return to the main program.
Quick Notes
- LC-3 assemblers are case-sensitive for address prefixes—always use lowercase
xfor hex addresses (fixed your originalX302Ftox302Fequivalents). - If you need to restore the default IN trap later, add a cleanup subroutine that writes
ORIG_IN_ROUTINEback tox0023.
内容的提问来源于stack exchange,提问作者Kacper Miklus
相关产品推荐
相关产品推荐

