Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Page 2 of 3 CS 2 8 1 0 Programming Assignment 2 Hello World! Objective: The objective of this lab assignment is to demonstrate your
Page
of
CS
Programming Assignment
Hello World!
Objective:
The objective of this lab assignment is to demonstrate your ability in
Using TRAP routines to get input from keyboard and output to the console
Detecting the end of user input with a newline character xA
Lab Statement:
You will write a program in LC machine code that will do the followings:
Detect a user input from the keyboard and echo the input to the console
Store the user input beginning at x The first character will be stored in location
x The second character will be stored in location x etc.
The user input will end with a return key which will be interpreted by LC as a newline
character xA
Upon detecting a newline character, display the entire user input to the console.
Terminate the program.
LC Syntax:
Get a character from keyboard.
LC has a TRAP service routine, trap vector x that will get a character from the
keyboard. It will store the character in register R
Opcode:
Operand : xtrapvect
Note: This instruction moves the user input to R directly without echoing the
user input to the console. If you want to see the user input, you need to
display it through your code, see next bullet item.
Instruction: xF
Echo a user input to the console
LC has a TRAP service routine, trap vector x that will put the character in R to the
console. If you call this service routine right after you get a user input, the user input will
be echoed to the console.
Opcode:
Operand : xtrapvect
Note: This instruction displays the content in R to the console if it is a printable
ASCII character.
Instruction: xF
Detecting a Newline Character
The ASCII code of the newline character is xA To detect a newline character, you need
to compare the user input with xA One of the ways to do this is to add the user input
with the s complement of xA ASCII code of newline character. If the result is then
the user entered a newline character.
Move the input to memory
One of the ways to move the user input to the memory is using an STR instruction. STR
requires a source register whose content is the data that needs to be stored, a register
to store the base address, and a sixbit offset. In our case, the register that contains the
data is R You can choose a register to store the base address. One suggestion is to
store x in that register. For the first character, we will use an offset of For the
additional character, instead of increasing the offset, it is easier to increment the base
address in the register.
Opcode: STR
Operand : SR
Operand : register of your choice. BaseR
Operand : required offset based on your design
Instruction: If I use R as BaseR and for the offset, the instruction will be
or x
Stay in the loop
You can use the BR opcode to stay in a loop that reads the input from keyboard
until the newline character is detected.
Display a string
LC has a trap service routine, trap vector x that displays a nullterminated string to
the console. To utilize this routine, you need to put the beginning address of the string
into R before you call this routine. In our case, the beginning address of the user input
is x Make sure you add a null character at the end of string when you detect a
newline character.
Testing:
Enter Hello LC this is from the keyboard. For example, if I am
testing the program that I write, I will enter Hello LC this is ACT.
Note: It is too complicated to implement the backspace key at this point. Make sure you
enter the correct string.
Submission:
Your machine code, either in binary or hex, in a text file.
A screenshot shows your interaction with LC see sample screenshot below.
please remember to write in machine code, as well to make sure this works for the LC machine code simulator thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started