
ECE-3120
Fall 2008
LAB 6 – USING STACK & SUB-ROUTINES
The purpose of this lab is to learn the basic techniques for using subroutines and to use
stack with subroutines.
Pre-Lab:
Prepare the first draft of the program and calculate the expected results. Also prepare a
stack frame for all the subroutines that are used. The main program should not perform
any calculation. This must be completed before coming to the lab and shown to the lab
instructor at the start of the lab session.
Approved: Lab TA _____________________ Date ____________
Programming Assignment:
• Write a fully-commented program for the HCS12 board to calculate the Least
Common Multiple (LCM) for four 8-bit unsigned numbers. Use appropriate
directives and labels for memory operands and constants. It must use a main
routine that calls some subroutines to perform whatever arithmetic operation that
needs to be done.
• It should Consist of a main (caller) routine and two subroutines.
• The algorithm for finding LCM is, LCM = Product of numbers / GCD.
• The stack pointer must be initialized to $3FFF.
• Use the simplest parameter passing method, using registers, as specified in the
following.
• Preserve the values of all registers (except the result registers and CCR) in the
subroutines.
• Subroutine “get_gcd” will calculate the GCD for two numbers.
o The two numbers are passed in registers A and B.
o The result will be returned in register A.
• Subroutine “get_lcm” will calculate the LCM for all four numbers.
o The starting address of the array of four numbers is passed in register X.
o The final calculated GCD is passed in register A.
o The result, LCM, is returned in registers D and X; with D being the most
significant word and X the least significant word.
• The code must start at $1000.
• The program must use data memory locations beginning at $1500:
o Store the LCM at memory location $1500-03.
o Store the GCD at memory location $1504.
o Store the product of the four numbers at $1505-08.
• Temporary data, if needed, may be stored following the program data.
• The 4 numbers are: 8, 12, 32, 48.