Answered step by step
Verified Expert Solution
Link Copied!

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
2
of 3
CS 2810
Programming Assignment 2
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 (x0A)
Lab Statement:
You will write a program in LC-3 machine code that will do the followings:
1. Detect a user input from the keyboard and echo the input to the console
2. Store the user input beginning at 0x3011. The first character will be stored in location
0x3011. The second character will be stored in location 0x3012, etc.
3. The user input will end with a return key which will be interpreted by LC-3 as a newline
character (0x0A)
4. Upon detecting a newline character, display the entire user input to the console.
5. Terminate the program.
LC-3 Syntax:
1. Get a character from keyboard.
LC-3 has a TRAP service routine, trap vector x20, that will get a character from the
keyboard. It will store the character in register R0.
Opcode: 1111
Operand 1: x20(trapvect8)
Note: This instruction moves the user input to R0 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: 1111000000100000=xF020
2. Echo a user input to the console
LC-3 has a TRAP service routine, trap vector x21, that will put the character in R0 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: 1111
Operand 1: x21(trapvect8)
Note: This instruction displays the content in R0 to the console if it is a printable
ASCII character.
Instruction: 1111000000100001=xF021
3. Detecting a Newline Character
The ASCII code of the newline character is x0A. To detect a newline character, you need
to compare the user input with x0A. One of the ways to do this is to add the user input
with the 2s complement of x0A, ASCII code of newline character. If the result is 0, then
the user entered a newline character.
4. 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 six-bit offset. In our case, the register that contains the
data is R0. You can choose a register to store the base address. One suggestion is to
store x3011 in that register. For the first character, we will use an offset of 0. For the
additional character, instead of increasing the offset, it is easier to increment the base
address in the register.
Opcode: 0111(STR)
Operand 1: 000(SR)
Operand 2: register of your choice. (BaseR)
Operand 3: required offset based on your design
Instruction: If I use R5 as BaseR and 0 for the offset, the instruction will be
0111000101000000(or x7140)
5. Stay in the loop
You can use the BR (0000) opcode to stay in a loop that reads the input from keyboard
until the newline character is detected.
6. Display a string
LC-3 has a trap service routine, trap vector x22, that displays a null-terminated string to
the console. To utilize this routine, you need to put the beginning address of the string
into R0 before you call this routine. In our case, the beginning address of the user input
is x3011. Make sure you add a null character at the end of string when you detect a
newline character.
Testing:
Enter Hello LC-3, this is . from the keyboard. For example, if I am
testing the program that I write, I will enter Hello LC-3, this is ACT.
Note: It is too complicated to implement the backspace key at this point. Make sure you
enter the correct string.
Submission:
1. Your machine code, either in binary or hex, in a text file.
2. A screenshot shows your interaction with LC-3, see sample screenshot below.
please remember to write in machine code, as well to make sure this works for the LC-3 machine code simulator thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions