Department of Electrical and Computer Engineering

The University of Texas at Austin

EE 306, Fall 2006
Programming Assignment 3
Due: 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

Background:

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 produced.

Assignment:

Implement, in LC-3 assembly language, an encryption/decryption program that meets the following requirements:

Input:

Your program should prompt the user for two separate inputs from the keyboard, as follows:

  1. The prompt: Enter an E to Encrypt or a D to Decrypt:

  2. The prompt: Input a message, when done press the <Enter> key:

  3. 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.

Encyption Algorithm:

Each ASCII code in the message will be transformed as follows:
  1. The low 5 bits of the ASCII code will be left rotated by 3 bit positions. The high bits will remain the same.
  2. 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.

  3. 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].
Decryption Algorithm:

The decryption algorithm is the reverse of the encryption algorithm.
  1. The low 5 bits of the ASCII code will be right rotated by 3 bit positions. The high bits will remain the same.
  2. 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.

  3. Hint 4: Right rotating an n-bit value by b bit positions is the same as left rotating it by . . .
Output:

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.

Notes: