Computer Engineering Lab

profileAdam.Abu
Lab3.pdf

CEG 3310/5310 Laboratory 3

Loop Control PURPOSE The purpose of this lab is to introduce you to HCS12 C and assembly programming using loop control. This is a two-week project. EXPERIMENT (1) (50%) Using CodeWarrior to create a new project that uses C language. During the project setup, most

steps are similar to when you created an assembly project except the following two extra steps:

 For “Rapid Application Development Option”, select None and press “Next>”.  Select “ANSI startup code”, “Small” memory model, and “None” for floating point format. Press

“Next>”. Write a simple C program that declares a character array of 32 elements and uses a for loop, with an ascending loop index variable, to write a constant value of -1 to every array element. The program also declares an integer array of 32 elements and uses a while loop with a descending loop index variable to write a constant value of -1 to every array element. At the end, the program should get into an infinite loop using a while loop. Make sure both arrays and both loop indices are all declared as global variables. Using CodeWarrior debug window to find out and record the following information inside the listing file (generated by the C compiler).

 The assembly code segment that corresponds to the for loop. Put comments on the code segment after studying it. For each assembly instruction, also record its starting address and its machine code.

 Repeat the above process for the while loop.

Turn in (only) the listing file that contains the above information Hint: From time to time, it helps to study the assembly listing file produced by the C compiler. In order for

CodeWarrior C compiler to produce assembly listing, do the following: 1. Get the “Standard Setting” dialog box by using Alt F7 (alternatively, clicking the corresponding icon, or

select Menu “Edit” and then SubMenu “Standard Setting”). 2. Select “Compiler for HC12” at the left hand side of the box and click on the “Options” button on the

right hand side. An “HC12 Compiler Option Settings” dialog box shows up. 3. Select the “Output” tab and put a check mark on “Generate Listing File”. Click the “OK” button. 4. Make the project. 5. Use Windows Explorer to reach the project folder. Inside the “bin” folder, there are multiple files with

their file types as “MASM Listing”. Double click the file “main” to open the file with a text editor.

(2) (50%) Write an assembly program that calculates the (two-byte-long) sum of N unsigned two-byte values in a memory area. It also turns on LED1 (refer to Lab 1, part 2) if and only if the sum is more than a value, THRESHOLD. In the data section, use assembler directives ds.b or ds.w to reserve the following:

 a two-byte space for THRESHOLD, followed by  a one-byte space that specifies N, the number of bytes to be added up in that memory area, followed

by

 a ten-byte space to store N double-byte values, followed by  a two-byte space for the sum.

Assume N is between two and five. All values in the data section should be treated as unsigned. Use the debug window to fill in the values of THRESHOLD, N and the data to be added up. The program gets into an infinite loop at the end. Demonstrate your program by taking a screenshot of the debug window that shows all data values and LED1 status (i.e., DDRA and PORTA location contents). Turn in main.asm and the screenshot

Hint: You may use the DBNE instruction for the loop control.