CS270 Recitation 9.1
LC-3 Macros

Goals

  1. To learn more about macros and the C preprocessor.
  2. Make your own custom LC3 macros

Setup

Make a subdirectory called R9.1 for the recitation, all files should reside in this subdirectory. Copy your factorial.asm code from R8 into your R9.1 directory, you will use this file as the basis for your LC3 macro experiments.

Getting started

As we saw with C programing, macros can be a useful tool for code reuse, function inlining, and increasing code portability. Because the C preprocessor is separate from the C compiler it is agnostic to whether the file it is processing is actually valid C code, and thus can be used to add macros to the LC3 assembly language. You can read more about the C preprocessor here.

The following command will use gcc to turn src.asm (an LC3 assembly file with C style macros) into src_p.asm (an LC3 assembly file that is ready to be assembled by the LC3 assembler). For this recitation you can replace src.asm with factorial.asm and src_p.asm with say factorial_p.asm.
    gcc -E -x c src.asm | sed '/^#/ d' > src_p.asm
Explanation of the command:

Create your own macros

First start by creating a macro that models the RET macro, test it by replacing the RET instruction in factorial.asm with you new macro. Keep in mind that the macros you create won't necessarily have the LC3 style like the macros added by Fritz. This won't matter after they have be preprocessed into LC3 assembly. If everything is working correctly the output file (factorial_p.asm) should assemble with the usual LC3 assembler.

Once you have RET working you should create a decrement instruction macro, and test it in a similar way. The decrement macro will take one parameter. To learn more about how to write macros with parameters and about macros in general you can look here or use google to find other resources.

Finally create a macro used to set up a stack frame in the callee function with the number of local variables as a parameter, as well as a corresponding macro to break down a stack frame in the callee function. Use these macros to replace the code at the beginning and end of the Factorial subroutine in factorial.asm. Keep in mind that you can write your macros in a separate file, for future reuse, and #include them in your current .asm file.

If you make it this far try to come up with other macros that could save writing redundant LC3 assembly code, you can also research other macro processing programs, one that I have used besides gcc is called m4.