Lab02 CTEN415
Lab02 CTEN415
Faculty of Engineering
Electrical, Computer and Telecommunications Department
Lab 03:
Prelab
The prelab assignment is expected to be completed before you walk into lab for checkoff by the
TA.
Each directory contains a MPLAB project file, an assembly file for you to complete, and C
source file which checks your results. Portions of a typical file are shown below:
;
*************************************************************************
; TO DO: Translate the assignments below to PIC24 assembly. Be sure to
; declare these variables after the previous TO DO comment.
;
*************************************************************************
/*
uint16 u16_a = 0x34Af;
uint16 u16_b = 0x29FE;
uint16 u16_c = 0xA458;
uint8 u8_d = 0x45;
*/
; u8_e = 0;
clr.b u8_e
; do {
do_top:
mov.b u8_e, WREG
mov.b W0, W4
;
*********************************************************************
; TO DO: To print out the variables in your code, set:
; W0 = u16_a
; W1 = u16_b
; W2 = u16_c
; W3 = u8_d
; W4 = u8_e
; Note: since only u8_e is used in this sample,
; this sample code just sets it. You'll have to do all of them. Make
; sure your code comes AFTER this comment but BEFORE the call
; instruction.
;
*********************************************************************
; Your code goes here.
call _check
;
*********************************************************************
; TO DO: Implement the code fragment below.
;
*********************************************************************
/*
if (u16_c & 0x0001) { //this is a bittest of the LSb
u16_b = u16_a + (((uint16) u8_d) << 1);
u16_a = u16_a ^ u16_b; //exclusive OR
u8_d = ~u8_d +8;
} else {
if (u16_a < u16_b) {
u16_b = u16_b - u16_a ;
} else {
u16_b = (u16_a >> 2) + u8_d;
}
}
u16_c = u16_c >> 1;
*/
; The two lines of C code below have already been implemented.
; Do not modify them.
; u8_e++
inc.b u8_e
; } while (u8_e < NUM_LOOPS);
; WREG W1
mov.b u8_e, WREG
mov.b #NUM_LOOPS, W1
cp.b W0, W1
bra LTU, do_top
bra GEU, do_end
do_end:
done:
goto done
The computations are not meaningful in themselves; we are only interested in the final variable
values that are produced.
Use the MPLAB X IDE to open the project, compile it, then run it in the simulator. To run step
by step, place a breakpoint at the first instruction then click run. You'll see the usual green arrow
indicating the current line; step your program as usual.
The assembly language program is a loop that calls a C function named check to compare the
variables in your assembly language program with the correct variable values during each loop
iteration. The check function implements the same C code that you are supposed to implement in
your assembly language file. When you compile and run for the first time, you will see FAIL
messages as shown below because you have not implemented the assembly code that computes
the new variable values according to the C code in the comment section. Once you have
implemented the assembly code correctly, the messages printed out by the check function will
have 'PASS' instead of 'FAIL'. Below is portion of the MPLAB X output when the project is
initially compiled and run:
Test started.
a:34af, b:29fe, c: a458, d:45, e:00 is correct; saw
a1:0000, b1:0d60, c1: 0000, d1:00, e1:00 FAIL
a:34af, b:0d70, c: 522c, d:45, e:01 is correct; saw
a1:5201, b1:0d64, c1: 0001, d1:58, e1:01 FAIL
a:34af, b:0d70, c: 2916, d:45, e:02 is correct; saw
a1:2902, b1:0d64, c1: 0001, d1:2c, e1:02 FAIL
a:34af, b:0d70, c: 148b, d:45, e:03 is correct; saw
a1:1403, b1:0d64, c1: 0001, d1:16, e1:03 FAIL
a:0196, b:3539, c: 0a45, d:c2, e:04 is correct; saw
a1:0a04, b1:0164, c1: 08c2, d1:8b, e1:04 FAIL
Your task is to manually convert the C code fragment in the comments to PIC24 assembly
language. The uint16_t variables are unsigned 16-bit values, while uint8_t variables are unsigned
8-bit variables.
You may find it useful to insert additional printf statements into the C check function to
determine when each particular ‘if’ or ‘else’ clause is executed, such as printf("If 1 entered\n");,
etc. Or else, you can use the debugging tips above and have two MPLAB instances running, and
use Watch windows to compare the variables in your assembly language with the variables in the
C check function during single stepping.
Approach
You may not ‘optimize’ away any statements – you are to implement the C code exactly as
shown. You must place a comment on a line by itself giving the line of C code translated
followed a register assignment then by the assembly instructions which implement this C code.
; W0 W0
; u8_i = 100;
mov.b #100, W0
mov.b WREG, i
; i++;
inc.b u8_i
; W0 W0
; u8_j = u8_i
mov.b i, WREG
mov.b WREG, j
Be careful of mixed unsigned 8-bit and 16-bit operations -- be sure to zero-extend the 8-bit
value.
TA Checkoff
Show the TA that the values produced by your assembly language program match the C values
on loop exit.
Report
1. Take a screenshot of the final data memory contents/variable values of your PIC24
assembly language program and show that it matches the C results. (10 points)
2. Attach your program source code in the lab submission. (10 points)
The TA will take off a significant number of points (20 points, a zero for the lab report) if
your assembly language source does not have the required comments. The comments should
indicate which assembly language source lines implement which C statements and give a register
assignment for each line of C.
Notes
How to show UART 1 Output
How to fix stack error (UART 1 Output doesn't display properly) for xc16 v1.25
5. Click OK