3.1. Basic Input

To get input from the user, we will use the OS call GETC. This OS call represents the LC3 instruction TRAP x20. When called, GETC polls the keyboard until a key is pressed then returns the character’s ASCII value in register R0.

GETC only polls one character at a time, so it must be called many times if the user input is a string with more than 1 character.

Note that GETC polls the keyboard and will hang the program until a key is pressed. Check out this page, advanced input, on how to get user input without hanging up the program.

Below is a user program that takes in an upper case letter and outputs the letter in lowercase. Note the user input is not checked so you can input non-letters. Check out this page, limiting user input to see how the user input can be limited to letters.

Note that users input is not displayed be default. This can be enabled by uncommenting line 12. GETC does not output the keystroke, it only stores it in R0. It is useful feedback to echo the users input. We can use OUT to echo each keystroke from the user. This page on basic output talks about OUT and PUTS.




© 2024 LC3Tutor

Privacy

results matching ""

    No results matching ""