MARIE汇编双子程序合并问题:仅执行预定义打印,无法触发用户输入
Ah, I see the issue here—this is a classic program flow problem with MARIE's sequential execution model. When you just tack the second subroutine right after the first without adjusting the control flow, the first subroutine is probably ending with a Halt instruction, which stops the entire program dead in its tracks before it ever gets to your user input logic. Let's break down how to fix this step by step:
Key Problem
MARIE executes instructions in order from top to bottom unless explicitly told to jump elsewhere. If your first "print predefined string" subroutine ends with a Halt, the program will terminate immediately after printing the string—no chance for the second subroutine to run. Even if there's no Halt, you might still need to explicitly direct the program to jump to the start of the second subroutine to avoid accidentally executing leftover or unintended instructions between the two blocks.
Step-by-Step Fix
Remove or replace the
Haltin the first subroutine
Instead of ending the first subroutine withHalt, use aJumpinstruction to send the program counter directly to the start of your user input subroutine.Label your subroutines clearly
Give each subroutine a unique, descriptive label so you can easily reference them in jump commands.Add a proper termination to the second subroutine
Make sure the second subroutine has aHalt(or conditional exit logic) to end the program when needed—otherwise it might loop infinitely or behave unpredictably.
Example Working MARIE Code
ORG 100 ; First Subroutine: Print Predefined String PrintPredefined, Load MsgAddr ; Load address of our predefined string Output ; Print the string to console Jump ReadNPrint; Jump to start of user input subroutine MsgAddr, DEC "Hello from predefined string!" HEX 00 ; Null terminator for MARIE string ; Second Subroutine: Read and Print User Input Character-by-Character ReadNPrint, Input ; Accept a single character from user Output ; Print the input character immediately ; Optional: Add conditional exit here (e.g., check for Enter key: HEX 0D) Jump ReadNPrint; Loop to keep accepting input (remove for single input) Halt ; Uncomment if you add an exit condition above
Explanation
- After printing the predefined string,
Jump ReadNPrintforces the program to move directly to the user input subroutine instead of stopping. - The second subroutine uses a loop (
Jump ReadNPrint) to keep accepting and printing characters until you add a conditional check (like comparing the input to the ASCII value of the Enter key) to trigger theHalt.
If you originally had a Halt at the end of the first subroutine, that's definitely why the second subroutine never ran—MARIE stops execution as soon as it hits that instruction. Adjusting the control flow with jumps is the core fix here.
内容的提问来源于stack exchange,提问作者user9673560

