CS 270
CS 270 Documentation
CS270 Recitation

More LC-3 Programming

What to turn in

You will get credit for this recitation by turning in R10.asm to Checkin.


  1. To extend your knowledge of LC-3 programming with a more complex algorithm.
  2. To solidify your knowledge of how to use the LC-3 assembler and simulator to debug assembly code.

The Assignment

  1. Make a subdirectory called R10 for the recitation; all files should reside in this subdirectory. Copy the file from R10.asm to the R10 directory, a listing of the code is shown below.
        ; Recitation 10
        ; Author: <name>
        ; Date:   <date>
        ; Email:  <email>
        ; Class:  CS270
        ; Description: Mirrors least significant byte to most significant
                        .ORIG x3000
                        JSR mirror           ; call function
        ; Parameter and return value
        Param           .BLKW 1              ; space to specify parameter
        Result          .BLKW 1              ; space to store result
        ; Constants
        One             .FILL #1             ; the number 1       
        Eight           .FILL #8             ; the number 8
        Mask            .FILL x00ff          ; mask top bits
        mirror                               ; Mirrors bits 7:0 to 15:8
                                             ; ~20 lines of assembly code
                        LD R0,Param          ; load pattern
                                             ; your code here
                        ST R1,Result         ; store result
  2. Use the LC-3 assembler to transform your assembly code into object code that can run on the LC-3 simulator:
    ~cs270/lc3tools/lc3as R10.asm
  3. Load the LC-3 simulator. The TA will help you step through an invocation of one of the LC-3 subroutines:
    ~cs270/lc3tools/lc3sim-tk &
  4. Implement the mirror subroutine, using the following algorithm. It’s a poor algorithm. Resist the urge to improve it.
    • Load the parameter into register 0
    • Make a copy of register 0 to register 1, which stores the result.
    • Load Mask into register 2
    • Use the mask to clear bits 15:8 in the result
    • Initialize register 2 to 1, this is the source mask
    • Initialize register 3 to 1, this is the destination mask
    • Initialize register 4 to 8, this is the counter
    • Write a loop to shift register 3 left by eight bits, by adding it to itself
    • Initialize register 4 to 8, again.
    • Write a loop that:
      1. checks if the bit in the source mask is set in the parameter.
        This is very similar to the operation you used in the RightShift route in P5 to examine a value using a mask.
        You'll need to put the result of the AND into R5. Don't get into the habit of using R5, but it's OK in this specific case.
      2. if so, adds the bit in the destination mask to the result
      3. then shifts the source mask left by adding it to itself
      4. then shifts the destination mask left by adding it to itself
      5. and finally decrements the counter until it reaches zero
    • Store the result into the Result memory location and return
  5. Test the mirror subroutine in the simulator using Param = 0x1234. The answer in Result should be 0x3434.