Docsity

Lab 6: Using Stacks and Sub-Routines for Calculating Least Common Multiple on HCS12 Board, Lab Reports of Microcomputers

Toggle sidebar
bg1
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.
pf2

Partial preview of the text

Download Lab 6: Using Stacks and Sub-Routines for Calculating Least Common Multiple on HCS12 Board and more Lab Reports Microcomputers in PDF only on Docsity!

ECE-

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 $.
  • 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.

Hints

  • The 4 numbers used to calculate the LCM from do not have memory locations. Use $1400, $1401, $1402, and $1403.
  • For the LCM, return the quotient in D and the remainder in X.
  • Using the stack in the get_gcd subroutine will allow them to still use the code found in the book, with a few additions.
  • Draw the stack frames for each subroutine to help you visualize what’s going on
  • Debug the program:

a. Download the program , run the program at full speed until it stops, and verify that the final values are correct (as hex bytes and characters). You need to DEMO your program to your Lab Instructor.

_Approved: Lab TA _____________________ Date _____________

Questions

  1. Draw the stack frame for "get_gcd" after preserving the registers.
  2. Using the stack to return the result, rewrite the "get_lcm" subroutine, preserving registers as required.
  3. Should all data registers be preserved for every subroutine? Why or why not?

Things to turn in as your Lab Report, attached in this order:

  1. This assignment sheet, with your name at the top, signed by the TA where shown.
  2. Your uncorrected pre-lab document (commented source code).
  3. A printout of the final.asm and.lst files. You’ll need to print the listing file in landscape mode to make it fit. Use Notepad to print them.
  4. A printout from the terminal screen (method: highlight text, type ctrl+c, and paste into a Notepad or Word document, using Courier New as the font) that includes everything done. Add brief hand-written comments and highlight important values at each step in the procedure, showing the relevant memory contents and register contents.