0% found this document useful (0 votes)
17 views

Lab02 CTEN415

Uploaded by

Stewart Pihelo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab02 CTEN415

Uploaded by

Stewart Pihelo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

BIUST

Faculty of Engineering
Electrical, Computer and Telecommunications Department

Lab 03:

8/16 Bit Assembly Language Programming


This lab has you convert a C program that performs 8/16-bit unsigned operations into PIC24
assembly language. The C programs are provided as MPLAB projects.

Prelab
The prelab assignment is expected to be completed before you walk into lab for checkoff by the
TA.

1. Unzip the ZIP archive which contains the lab files.


2. Study the files in the example folder -- this is a solved problem and will give you hints on
what to do for your assigned task. The example folder also shows the correct way to
comment your assembly code.
3. Compile and run your assigned C program. Demonstrate its output to your TA. (10
points)
4. Make a good faith effort at writing at least some of the assembly language program for
your assigned task before lab. Compile and execute this program in MPLAB and
demonstrate to the TA the first place where it does not agree with the C program. (10
points)
5. Please go through lecture slides of chapter 4 of the text book:
1. Chapter 4: 8/16-bit Operations in PIC24 Assembly Language
6. Watch the 'lab3_howto' video, which shows you to solve this problem in a step-by-step
manner.
7. Watch the hints video, which provides essential tips on debugging approaches.

Task 1 (60 points)


Unzip the ZIP archive with the lab. You will see three directories (folders) labeled case1\,
case2\, and case3\. If the last digit of your MSU student ID is: (Select only one case)

o 0,1,2 then use case1


o 3,4,5 then use case2
o 6,7,8,9 then use case3

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.

If you do not understand a C operator, then ask the TA for assistance.

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.

Implement your assembly language solution one step at a time:

1. Get variable initialization working correctly.


2. The loop executes a fixed number of times, so implement the loop with an empty body
and verify that the loop executes the correct number of times with the loop variable
containing the correct value at loop exit.
3. Trace the C code to determine which if, else clauses are executed the first time through
the loop. Implement those if, else clauses with empty bodies for the other clauses, then
test execution through the loop one time, verifying that you get correct values.
4. Implement the other clauses to complete your implementation. Trace variable values each
time through the loop, and debug problems when you see a deviation from the C output.

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

1. Left click on case1 like this

2. Left click on File and then choose "Project Properties(case 1)"

3. Left click on Simulator


4. In the option categories, choose Uart1 IO Options.

5. Enable Uart1 IO like this


6. Restart MPLAB and you will see UART1

How to fix stack error (UART 1 Output doesn't display properly) for xc16 v1.25

1. Left click on case1 like this

2. Left click on File and then choose "Project Properties(case 1)"

3. Left click on XC16 (Global Options)

4. In the "Additional options" field, type "-no-legacy-libc"

5. Click OK

You might also like