Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.59 MB, 687 trang )
570
Microprocessors
unsigned char i,j,k;
main() {
i = 0x24; j = 0xC5; k = 0x4D;
k++; // (a) increment
i--; // (b) decrement
k = j & i; // (c)bitwise and
i = i >> 3; // (d) right shift by 3
k = ~k; // (e) bitwise negation
k = k ^ j; // (f) bitwise XOR
i = j - k; // (g) subtraction
j = j << 2; // (h) left shift by 2
j = j | 0xC0; // (i) bitwise OR
k = k + j; // (j) addition
k = bitset(k,3); // (k) set a bit to ‘1’ */
j = bitclr(j,5); // (l) clear a bit to ‘0’
printf (“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
}
1. For your assignment, if the last digit of your student ID is 0 or 1, use
ops_var1.c; if it is 2 or 3, use ops_var2.c, and so forth. Compile this program
with your favorite C compiler and note the value of i, j, k at each step. The
bitset, bitclr macros simply implement the equivalent bsf and bcf PIC18
operations.
2. The loop.zip archive contains five C program variations (loops_var{1-5}.c);
the main() code for a typical one is shown in Listing E.2. The program variations re-arrange the order of blocks a, b, c, d, and e.
LISTING E.2
Conditional operations.
unsigned char i,j,k;
main(){
i = 0x24; j = 0x45; k = 0xFD;
// begin ‘a’
while ( !bittst(j,7) ) { j = j << 1; }
printf(“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
// end ‘a’
// begin ‘b’
for (j=0; j != 10; j++) {i = i + j; }
printf(“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
// end ‘b’
// begin ‘c’
do {
k--;
} while (bittst (k,3));
printf(“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
// end ‘c’
// begin ‘d’
if (i < k) {
Appendix E: Suggested Laboratory Exercises
571
k--;
} else {
j++;
}
printf(“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
// end ‘d’
// begin ‘e’
if (k > j) {
i++;
} else {
j++;
}
printf(“i: 0x%x, j: 0x%x, k: 0x%x\n”,i,j,k);
// end ‘e’
}
For your assignment, if the last digit of your student ID is 0 or 1, use
loops_var1.c; if it is 2 or 3, use loops_var2.c, and so forth. Compile this program with
your favorite C compiler and note the value of i, j, k at the conclusion of each code
block a, b, c, d, and e. The bittst(var,bitno) macro returns the value of bit #bitno
in variable var.
Lab Activity
1. Convert your assigned ops_var{1-5}.c and loops_var{1-5}.c programs to
PIC18 assembly language and verify their operation in MPLAB. The printf
statements in the C code listings are only included for debugging purposes;
use the WATCH window of MPLAB to observe the values of i, j, and k as
you single step through your assembly language program. Verify that the
assembly language produces the same values for i, j, k at each step.
2. For the ops_var program, calculate the total number of 8-bit operations
performed (each 8-bit assignment counts as 1, each logical/add/sub/inc/
dec counts as 1, and each shift operation counts as 1). For shift operations,
count each required shift as 1 (a >> 3 counts as 3 shift operations). Make
this calculation based on the C code, not the assembly code. Take the execution time value recorded for the ops_var program, and divide by the total
number of 8-bit operations to get an average execution time per 8-bit operation. Take the inverse of this value to get the number of 8-bit operations
per second that can be expected from the PIC18F242 running at 20 MHz.
Compare this to what is obtained by taking the addwf instruction and computing the same values based on its execution time. Why are the values different? Discuss this in your report.
572
Microprocessors
E.5 EXPERIMENT 4: EXTENDED PRECISION AND SIGNED
OPERATIONS (CHAPTER 5)
This experiment explores PIC18xx2 extended precision and signed operations
(Chapter 5). This experiment assumes the students have access to an x86 C compiler on either a Windows or Linux machine to verify C code results.
Prelab
1. The intop.zip archive contains five program variations intop_var{1-5}.c
similar to the op variations of the previous experiment except the variables
are unsigned int types. Pick a program variation in the same manner in the
previous experiment and record the values of the i, j variables after each
operation.
2. The compare.zip archive contains five program variations named unsigned_var{1-5}.c that use unsigned int types and five variations named
signed_var{1-5}.c that use signed int types. These are similar to the
loop_ops program of the previous experiment. Pick one program variation
of each type in the same manner as the previous experiment and record the
values of the i, j variables after each code block.
Lab Activity
1. Convert each of your assigned program variations intop_var{1-5}.c, unsigned_var{1-5}.c, and signed_var{1-5}.c to PIC18 assembly language and
verify within MPLAB that your code produces the same values for i, j as
the original C code.
2. For the intop_var program, calculate the total number of 16-bit operations
performed (each 16-bit assignment counts as 1, each logical/add/sub/
inc/dec counts as 1, and each shift operation counts as 1). For shift operations, count each required shift as 1 (a >> 3 counts as three shift operations). Make this calculation based on the C code, not the assembly code.
Take the execution time value recorded for the intop_var program, and divide by the total number of 16-bit operations to get an average execution
time per 16-bit operation. Take the inverse of this value to get the number
of 16-bit operations per second that can be expected from the PIC18 running at 20 MHz. Compare this to what is obtained by taking the addwf instruction, computing the same values based on its execution time, and
Appendix E: Suggested Laboratory Exercises
573
multiplying by 2. Compare these two values and discuss reasons for any
differences.
3. Pick the machine code for either a bov or bnov in your signed_var program
and show that the displacement value in the machine code is correct given
the PC and target address values.
E.6 EXPERIMENT 5: POINTERS AND SUBROUTINES (CHAPTER 6)
This experiment covers PIC18 subroutine and pointers, which are discussed in
Chapter 6. This experiment assumes the students have access to an x86 C compiler
on either a Windows or Linux machine to verify C code results.
Prelab
The ptrlab.zip archive contains some C programs for experimenting with pointers
and subroutines. Table E.2 describes these programs and gives student assignments
based on the last digit of their student ID number.
TABLE E.2 Program Assignments
Last Digit of Student ID
Assigned C Program, Parameter Block Location
0, 1, 2 or 3
strflip.c; copies s1 to s2, reversing the case of all letters.
Use 0x58 for the location of the dostr() parameter
block (ptr1, ptr2).
4, 5, or 6
strup.c; exchange s1 and s2, upcasing all chars in the
new s2. Use 0x5C for the location of the dostr()
parameter block (ptr1, ptr2).
7, 8, or 9
strxchg.c; exchange s1 and s2, reversing the case of all
chars in the new s1. Use 0x60 for the location of the
dostr() parameter block (ptr1, ptr2).
Make a good faith effort to convert your assigned program to PIC18 assembly
language before your assigned lab time; use the lab time for seeking TA assistance
and debugging of your program.