Department of Electrical and Computer Engineering
The University of Texas at Austin
EE 306, Fall 2006
Programming Assignment 3
5 November, 11:59 pm 7 November, 11:59pm
Yale N. Patt, Instructor
TAs: Aseem Bathla, Cameron Davison, Lisa de la Fuente, Phillip Duran, Jose Joao,
Jasveen Kaur, Rustam Miftakhutdinov, Veynu Narasiman, Nady Obeid, Poorna Samanta
You must do the programming assignment by yourself. You are permitted to get help from ONLY the TAs and the instructor.
The file you submit should be an assembly language file called crypto.asm. This is the only file you
need to submit.
Encrypt or Decrypt a message from the keyboard
Cryptography, from the Greek word kryptos, meaning "hidden", deals with
the science of hiding a message from eyes you do not want to understand the
contents of the message. The desire to do this has existed ever since
humankind was first able to write. There are many ways to keep something
secret. One way is to physically hide the document. Another way is to
encrypt the text you wish to remain secret. Some people use this to keep
others from understanding the contents of the files in their computer.
Encrypting a file requires an input message, called the "plain text,"
and an encryption algorithm. An encryption algorithm transforms "plain text"
into "cipher text." Just like a door needs a key to lock and unlock it, an
encryption algorithm often requires a key to encrypt and decrypt a message.
Just like a homeowner can selectively give the key to the front door to only
those he/she wants to allow unaccompanied access, the author of a message can
selectively give the encryption key to only those he/she wants to be able to read
the message. In order for someone to read the encrypted message, he/she has to
decrypt the cipher text, which usually requires the key.
For example, suppose the plain text message is HELLO WORLD. An encryption
algorithm consisting of nothing more than replacing each letter with the next
letter in the alphabet would produce the cipher text IFMMP XPSME. If someone saw
IFMMP XPSME, he/she would have no idea what it meant (unless, of course, he/she could
figure it out, or had the key to decrypt it.) The key to encrypt in this case
is "pick the next letter in the alphabet," and the decryption key is "pick the
previous letter in the alphabet." The decryption algorithm simply replaces each
letter with the one before it, and presto: the plain text HELLO WORLD is
Implement, in LC-3 assembly language, an encryption/decryption program that
meets the following requirements:
Your program should prompt the user for two separate inputs from the keyboard,
- The prompt: Enter an E to Encrypt or a D to Decrypt:
- The user will type E or D. A real world program should also detect any other
character typed and respond with "You entered an Illegal Character, please try again."
We will assume in this assignment that the user can type a capital E or D correctly.
- After the user types an E or a D, you must echo that character so that it appears on the console.
You must also output a line feed (ASCII code x0A) so that the cursor moves to the next line.
- Based on whether the user typed an E or a D, your program will Encrypt or Decrypt
the message the user is about to enter.
The prompt: Input a message, when done press the <Enter> key:
- The user will input a character string from the keyboard, terminating the
message with the <Enter> key. The <Enter> key is not part of the message, but you will need to echo it onto the console.
- You may assume that the message that the user enters will be at most 20 characters long (including spaces).
Hint 1: To continually read from the keyboard without first printing a prompt
on the screen, use TRAP x20 (assembler name GETC). That is, for each key you wish
to read, the LC-3 operating system must execute the TRAP x20 service routine. If
you follow TRAP x20 with the instruction TRAP x21 (assembler name OUT), the character
the user types will be displayed on the screen as we showed in class on Wednesday.
Each ASCII code in the message will be transformed as follows:
- The low 5 bits of the ASCII code will be left rotated by 3 bit positions.
The high bits will remain the same.
- For example, if the input (plain text) is the letter A, the program should
replace the low five bits of the ASCII code for A, with the five bits obtained
by rotating them three bit positions to the left. In this case, since the
ASCII code for A is 01000001, the low five bits are 00001. Rotating them three
bit positions to the left yields 01000. Putting this into the low five bit
positions yields the encrypted code 01001000. Since this is the ASCII code
for H, we have thus encrypted A as H.
Hint 2: To extract the low 5 bits of the ASCII code, think about using a MASK.
Hint 3: There are several ways to rotate a field that is smaller than eight bits
with the LC-3. One way is to start by placing the field in both the high bits and low
bits of a word. If the field is five bits, the high bits are Bits[15:11] and the low
bits are Bits[4:0].
The decryption algorithm is the reverse of the encryption algorithm.
- The low 5 bits of the ASCII code will be right rotated by 3 bit positions.
The high bits will remain the same.
- For example, if the input (cipher text) is the letter H, the program should
replace the low five bits of the ASCII code for H, with the five bits obtained
by rotating them three bit positions to the right. In this case, since the
ASCII code for H is 01001000, the low five bits are 01000. Rotating them three
bit positions to the right yields 00001. Putting this into the low five bit
positions yields the decrypted code 01000001. Since this is the ASCII code
for A, we have thus decrypted H as A.
Hint 4: Right rotating an n-bit value by b bit positions is the same as left rotating it by . . .
Your program should output the encrypted/decrypted message to the screen, and then HALT.
We have posted a few examples of how your program should behave.
Your program should match this output exactly.
- The first line of your program must specify the memory address of the
first instruction of your program. The LC-3 simulator will place your program
starting at that address. For this assignment, you should place your program starting at
x3000 (i.e. the first line of your program should be .ORIG x3000).
- If you are using a Windows machine, use the LC3Edit program to type in your programs.
On the Linux workstations, pico, emacs (or xemacs), and vi (or gvim) are several of the
text editors you can use. Your program file needs to be in plain text format. Please
ask any TA if you have any questions.
- The file that you will submit for this assignment must be named crypto.asm.
- To submit program 3, follow the submission instructions.