Colorado State University

Recitation R6 - LC-3 Programming Introduction


CS270: Computer Organization

Goals

  1. To learn how to write a basic LC-3 program with functions, conditionals, and a loop.
  2. To learn how to use the LC-3 assembler and simulator to debug assembly code.

The Assignment

Make a subdirectory called R6 for the recitation; all files should reside in this subdirectory. Copy the file from the link below to the R6 directory, a listing of the code with some comments removed is shown below. R6.asm contains:
; Recitation 6
; Author: 
; Date:   
; Email:  
; Class:  CS270
;
; Description: Implements integer (16-bit) addition and subtraction
;
;------------------------------------------------------------------------------
; Begin reserved section: do not change ANYTHING in reserved section!

                .ORIG x3000
                BR Main

; A jump table defined as an array of addresses
Functions       .FILL IntAdd         ; address of add routine      (option 0)
                .FILL IntSub         ; address of subtract routine (option 1)
                .FILL IntMul         ; address of multiply routine (option 2)

Main            LEA R0,Functions     ; get base of jump table
                LD  R1,Option        ; get option to use, no error checking
                ADD R0,R0,R1         ; add index of array
                LDR R0,R0,0          ; get address of function
                JSRR R0              ; call selected function
                HALT

; Parameters and return values for all functions
Option          .BLKW 1              ; which function to call
Param1          .BLKW 1              ; space to specify first parameter
Param2          .BLKW 1              ; space to specify second parameter
Result          .BLKW 1              ; space to store result

; End reserved section: do not change ANYTHING in reserved section!
;------------------------------------------------------------------------------
IntAdd                               ; Your code goes here
                                     ; Solution has ~4 instructions
                RET
;------------------------------------------------------------------------------
IntSub                               ; Your code goes here
                                     ; Solution has ~6 instructions
                RET

;------------------------------------------------------------------------------
IntMul                               ; Your code goes here
                                     ; Solution has ~9 instructions
                RET

;------------------------------------------------------------------------------
               .END
  1. Use the LC-3 assembler to transform your assembly code into object code that can run on the LC-3 simulator:
        ~cs270/lc3tools/lc3as R6.asm
        
  2. Load the LC-3 simulator and the TA will help you step through an invocation of one of the LC-3 subroutines:
        ~cs270/lc3tools/lc3sim-tk &
        
  3. Note: when programming in assembly, it is easy to forget the syntax of each instruction (e.g., where do the source and destination registers go). Please refer to this document (starting on page 6) for a description of the assembly syntax for each LC-3 instruction.
  4. Implement the IntAdd subroutine, using the following algorithm:
  5. Test the IntAdd subroutine in the simulator using Option = 0, Param1 = 0x1234, and Param2 = 0x3456. The answer in Result should be 0x468A. Try a negative value as well.
  6. Implement the IntSub subroutine, which is a clone of IntAdd, however you must negate the second operand before the addition. Use the 2’s complement to do this, as follows:
  7. Test the IntSub subroutine in the simulator using Option = 1, Param1 = 0x8765, and Param2 = 0x3456. The answer in Result should be 0x530F.
  8. Implement the IntMul subroutine, using this terrible algorithm:
  9. Test the IntMul subroutine in the simulator, using Option = 2, Param1 = 0x1234, and Param2 = 0x0003. The answer in Result should be 0x369C. Try a negative value for Param1 as well. You may assume that Param2 will be 0 or positive (why do you think this important?).