MARIE汇编语言:实现含$终止符的姓名输入及带空格打印功能的技术问询
Hey there! Let's get that space showing up between the first and last name when your program prints. The root issue is that your current code doesn't insert a space character into memory after capturing the first name, right before starting to store the last name. Here's how to adjust your code and resolve this:
Modified Full Code
/main 主程序 FirstNameLoop, JnS SubInputFirstName Load Name Add One Store Name Jump FirstNameLoop LastNameLoop, JnS SubInputLastName Load Name Add One Store Name Jump LastNameLoop /输入名的子程序 SubInputFirstName, Hex 0 Input Store Temp Subt Comma Skipcond 400 Jump StoreFirstName ; When comma is detected, store space first Load Space StoreI Name Load Name Add One Store Name Jump LastNameLoop StoreFirstName, Load Temp StoreI Name JumpI SubInputFirstName End, JnS subPrintString Load NamePrint Add One Store NamePrint Jump End Finish, Halt /输入姓的子程序 SubInputLastName, HEX 0 Input Store Temp Subt Dollar Skipcond 400 Jump StoreLastName Jump End StoreLastName, Load Temp StoreI Name JumpI SubInputLastName /打印姓名的子程序 subPrintString, HEX 0 LoadI NamePrint Store Temp Subt Dollar ; Check for end marker ($) instead of period Skipcond 400 Jump PrintName Jump Finish PrintName, Load Temp Output JumpI subPrintString NamePrint, HEX 300 Dollar, Dec 36 Name, HEX 300 One, DEC 1 Temp, DEC 0 Space, DEC 32 Comma, DEC 44 Period, DEC 46
Key Changes Explained
Inserting the Space: In the
SubInputFirstNameroutine, when the program detects the comma (your first/last name separator), we now:- Load the
Spacevalue (ASCII 32) - Store it at the current
Namememory location - Increment the
Namepointer to move past the space - Jump to the last name input loop
This ensures a space is stored between the end of the first name and start of the last name.
- Load the
Fixing the Print Termination: I also adjusted the
subPrintStringroutine to check for theDollarcharacter (ASCII 36) instead ofPeriodto properly stop printing when it reaches the end marker you're using (since users input$to finish). Your original code was checking for a period, which wasn't aligned with your input termination logic.
Now when you run the program:
- Enter your first name characters, finish with a comma (e.g.,
John,) - Enter your last name characters, finish with
$(e.g.,Doe$) - The program will print
John Doewith the proper space in between!
内容的提问来源于stack exchange,提问作者Bryan Hii

