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 is shown below. R6.asm contains:
; Recitation 6
; Author:      <name>
; Date:        <date>
; Email:       <email>
; Class:       CS270
;
; Description: Implements integer (16-bit) addition, subtraction, and
;              multiplication.
;
;------------------------------------------------------------------------------
; Begin reserved section: do not change ANYTHING in reserved section!

                .ORIG x3000
                BR Main

Functions       .FILL IntAdd         ; Address of IntAdd routine (option 0)
                .FILL IntSub         ; Address of IntSub routine (option 1)
                .FILL IntMul         ; Address of IntMul routine (option 2)

Main            LEA R0,Functions     ; The main routine calls one of the 
                LD  R1,Option        ; subroutines below based on the value of
                ADD R0,R0,R1         ; the Option parameter.
                LDR R0,R0,0          ;
                JSRR R0              ;
EndProg         HALT                 ;

; Parameters and return values for all functions
Option          .FILL #0             ; 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
                                     ; DON'T USE REGISTERS 5, 6, or 7!
                RET
;------------------------------------------------------------------------------
IntSub                               ; Your code goes here
                                     ; Solution has ~6 instructions
                                     ; DON'T USE REGISTERS 5, 6, or 7!
                RET

;------------------------------------------------------------------------------
IntMul                               ; Your code goes here
                                     ; Solution has ~9 instructions
                                     ; DON'T USE REGISTERS 5, 6, or 7!
                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. 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. Do the operation by hand to check your subroutine. Try a negative value as well. Make sure your program reaches the HALT in the main routine (otherwise, your program is considered wrong).

  6. Implement the IntSub subroutine which computes Param1 - Param2. It is basically 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. Do the operation by hand to check your subroutine. Try a negative value as well. Make sure your program reaches the HALT in the main routine (otherwise, your program is considered wrong).

  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. Do the operation by hand to check your subroutine. Try a negative value for Param1 as well. You may assume that Param2 will be 0 or positive (why do you think this important?). Make sure your program reaches the HALT in the main routine (otherwise, your program is considered wrong).