## Homework 3: Part 1 An LC-3 machine code program that rotates a 16-bit word

### Outcomes

After successfully completing this assignment, you will be able to...
• Enter, debug, and execute programs written in LC-3 machine code
• Explain the different addressing modes of the LC-3 ISA
• Use the LC-3 operate instructions, data movement instructions, and conditional branch instruction to solve a programming problem

### Before Starting

Read Chapters 4 and 5. Also, do Lab 1 to learn how to use the editor and simulator for the LC-3.

### Rotate a bit pattern n bits to the left

Using LC3edit, create a file named `rotate.bin`. Your task is to write an LC-3 machine code program that rotates a 16-bit word n bits to the left, where n is a non-negative integer between 0 and 16, inclusive. Your program should assume that R2 contains the address of the bit pattern to be rotated (in other words, before executing your program, you should manually enter the address of the word to be rotated into R2). Your program should also assume that the number of bit positions to rotate has been entered into R5. Upon completion, the rotated value should be stored in R4. Your program should begin at memory location x3000.

Example: If the original bit pattern is 1000 0000 0101 0101 (x8055), and the number of bits to rotate is 0000 0000 0000 1000, then upon completion of your program R4 should contain the value 0101 0101 1000 0000 (x5580).

Hint: Recall from our class discussion that adding a number to itself effectively shifts the bit pattern to the left by one bit position. A 1-bit left rotation is just like a 1-bit left shift except that the leftmost bit (bit 15) ends up as the rightmost bit (bit 0).

You will use the solution to this problem in Part 2 of Homework 3 next week.

### Documentation and Formatting

Your program should include a thorough and clear explanation of the process that you went through to come up with a machine code solution to the given problem. The explanation should be included as a comment at the top of your .bin file. Program documentation and formatting will be worth 50% of the grade for this program - your explanation should not be considered an afterthought to coding. It serves to document your thought process prior to coding and illustrates your understanding of the problem. It will serve as your outline when you code your solution. It will also serve as a guide to the TA who is trying to grade programs consisting entirely of 0's and 1's. It should contain (at least) the following information:
• An explanation of your thought process, and how it led you to the algorithm you used to solve the problem. When you first sat down to solve this problem, what steps did you take to understand the problem? To come up with a solution? As you consider each step in the solution, explain how you can accomplish that step using instructions from the LC-3. Use examples for illustration, if necessary. Your goal is to refine your algorithm to the point where it can easily be translated into LC-3 machine code.

• A list of the registers used in the program, and what they are used for

• If the program expects registers or memory locations to be initialized to certain values prior to the program's execution, that should be noted. The reader of the program should be told where to find the program's results upon completion of the program.

When coding the instructions themselves, leave a space between each field in the instruction, as we did in lab. For example, the following AND instruction is separated into its component parts (opcode field, destination and source registers, immediate bit, immediate value):

```0101 001 001 1 00000     ;clear R1, to be used as the running sum
```
Unlike programs written in higher-level languages, machine code programs should have a comment for every line of code. Also, main sections of code should be preceded by explanatory comments.

### Deliverables

You must use turnin to submit your LC-3 file `rotate.bin` before 5pm on Tuesday, November 16. Make sure you include your name and section number as comments at the top of your file.