help doing assignment lab of microcontroller using HC12
EG-CP/EE 280 Microcontrollers
LAB 4, Spring 2016
Instructor: Dr. Jidong Huang
Student Name:
Group Partner:
Objective: This lab covers the use of stack to store temporary information in an assembly language program.
Equipment and Software provided: PC, Metrowerks CodeWarrior.
Evaluation: Grade for labs will be based on 1) In-lab activities, and 2) Lab report
Lab Duration: 2 lab sessions
Lab Report: Lab report based on this instruction sheet will be collected on Wed., Apr. 6 at the end of the class. The completed lab report shall be uploaded to Titanium using homework assignment tool. Name your lab report as LAB4_EE280_YourName.doc, e.g., LAB4_EE280_DoeJohn.doc.
Lab Activity:
For the following lab activities, please create your test project using the cpe280 project template as provided in Code Warrior. For each coding task, you may fill in the code in cpe280 asm file under the main section below “Entry:”.
1. Practicing the use of Stack
a. An Example
Implement the following program in CodeWarrior
NUM1 EQU $15
NUM2 EQU $2F
LDS #$3C00
LDAA #NUM1
LDAB #NUM2
PSHA
PSHB
CLRA
CLRB
PULB
PULA
Please step through (F11) the program and observe how register value and memory content changes as you step through the code. Please record the results after you execute each instruction code starting from the first line.
|
Instructions |
A |
B |
SP |
M[$3BFE] |
M[$3BFF] |
M[$3C00] |
|
LDS #$3C00 |
|
|
|
|
|
|
|
LDAA #NUM1 |
|
|
|
|
|
|
|
LDAB #NUM2 |
|
|
|
|
|
|
|
PSHA |
|
|
|
|
|
|
|
PSHB |
|
|
|
|
|
|
|
CLRA |
|
|
|
|
|
|
|
CLRB |
|
|
|
|
|
|
|
PULB |
|
|
|
|
|
|
|
PULA |
|
|
|
|
|
|
Now, implement the following program in CodeWarrior
NUM1 EQU $15
NUM2 EQU $2F
LDS #$3C00
LDAA #NUM1
LDAB #NUM2
PSHA
PSHB
CLRA
CLRB
PULA
PULB
Please step through (F11) the program and observe how register value and memory content changes as you step through the code. Please record the results after you execute each instruction code starting from PSHA.
|
Instructions |
A |
B |
SP |
M[$3BFE] |
M[$3BFF] |
M[$3C00] |
|
PSHA |
|
|
|
|
|
|
|
PSHB |
|
|
|
|
|
|
|
CLRA |
|
|
|
|
|
|
|
CLRB |
|
|
|
|
|
|
|
PULA |
|
|
|
|
|
|
|
PULB |
|
|
|
|
|
|
Do you notice any difference in the final results between this program and the previous one? Can you explain why?
2. Design and Implementation of Assembly Programs using Stack
Based on what you have learned so far, design and implement some small programs that complete the following tasks.
a. Task 1: Use stack to move data between memory locations.
Now design a program to swap the memory contents for the two memory arrays.
Before you start moving data between the two memory locations, please use appropriate directives under “Data Definition Section” to initialize the following memory bytes as:
M[$2050] = $32, M[$2051] = $28, M[$2052] = $81, M[$2053] = $71,
M[$2060] = $27, M[$2061] = $8c, M[$2062] = $50, M[$2063] = $6a,
Basically, you need to write a program to swap the memory contents between $2050 and $2060; $2051 and $2061; $2052 and $2062; and $2053 and $2063. That is to say,
M[$2060] = M[$2050]_old = $32, M[$2050] = M[$2060]_old = $27,
M[$2061] = M[$2051]_old = $28, M[$2051] = M[$2061]_old = $8c,
M[$2062] = M[$2052]_old = $81, M[$2052] = M[$2062]_old = $50,
M[$2063] = M[$2053]_old = $71, M[$2053] = M[$2063]_old = $6a,
In your program, please use stack as the location for storing temporary data. Please initialize your stack pointer to be $3C00. You can then follow the following steps to move the data between two locations.
1) Move 4 bytes from memory location A to the stack
2) Move 4 bytes from memory location B to the stack
3) Move 4 bytes from the stack to memory location A
4) Move 4 bytes from the stack to memory location B
Please use branch instructions to create loops for moving data to and from the stack.
Your program is:
The CodeWarrior test result is:
Register and memory window before the swap:
Register and memory window after the swap:
b. Task 2: Sorting an Array.
Now, design a program to sort the data in an array.
In the first part of your program, you shall use appropriate directives to store the following 5 unsigned numbers: $1C, $5D, $72, $48, $32 into the memory locations starting from $2000. Also initialize your stack pointer to be $3C00.
In your program, assume that you know the size of the array, meaning you can use a fixed loop counter like 5 to end your iteration.
For sorting numbers, there are many ways to sort the data in an array. The following algorithm is one way of sorting the data in an array.
1) Based on the number of data in an array, search for the largest number in the array and remember its memory location. To end your search, you may use index addressing 0, X to access data and then compare the index X value to the end location of the memory array, i.e. $2004.
2) Once you find the largest number in an array, push it into a stack, and fill in the memory location as $00. For example, after first round of search, starting from memory location $2000, the data in the array becomes: $1C $5D $00 $48 $32; and the largest number in the array is now in the stack, i.e. M[$3BFF] = $72. Similarly, after two rounds of search, the data in the array becomes $1C $00 $00 $48 $32; and the largest and the second largest number in the array are now in the stack, i.e. M[$3BFE] = $5D M[$3BFF] = $72.
3) Repeat step 2 for 5 times
4) Retrieve data from the stack and save them to specified memory locations.
Please sort the data in the array using a descending order. Basically, this means the results of your program shall be:
M[2010] = 72; M[2011] = $5D; M[2012] = $48; M[2013] = $32; M[2014] = $1C;
Your program is:
The CodeWarrior test result is:
Register and memory window before the sorting:
Register and memory window after the sorting:
PAGE
4