Lab 5: Pointers, Subroutines

This lab has you convert a C program that uses pointers and subroutines into PIC24 assembly language. You will also use debugging techniques to examine the workings of a C program.

Basics

As shown above, erase lab_assem3 from the filepath when extracting, or you'll get a folder inside a folder.

Be sure you do the correct exercise or you will get a zero for the lab.

Prelab

The prelab assignment is expected to be completed before you walk into lab for checkoff by the TA. If you arrive more than fifteen minutes late to your assigned lab, you are not eligible for prelab credit unless you have made prior arrangements with your TA. 

If there's nothing in the window, or your UART window is missing, here's how to fix it:

3. Under Categories on the left, select Simulator:

4. In the dropdown under Options for Simulator/ Option categories, select "Uart1 IO Options":

5. Check the "Enable Uart1 IO" box

6. If it doesn't work, restart MPLAB. If it still doesn't work, close all projects, close MPLAB, delete your entire lab_assem3 folder (after saving any work you may have done in Notepad), and re-extract the archive. If the UART window still doesn't show up, don't worry, you don't need it to complete the lab. You can see the desired results of your program by running it and examining the variables sz_1 and sz_2, or by consulting the reference at the bottom of this page.

3. Open your strXXXX.c file. Change the block comments in and around the dostr function to single-line comments. When you're done, it should look like this:

5. Right-click on Source Files under your project name in the Projects window. Select Add Existing Files, and add your strXXXX.s file to the project.

6. Open your strXXXX.s file, then switch back to strXXXX.c in the editor.

7. Select and copy dostr, highlighted below:

8. Paste it into your strXXXX.s file near the top, right above the line .include "xc.inc"

9. Copy paste your UART window output above dostr

10. Enclose everything you just pasted with a block comment.

11. Change the name of the file and the file description. (Feel free to copy this from the Basics section above).

12. Put a header on the top of the file with your name, course number, lab section number, lab number, and the filename. When you're done, it should look something like this:

His friends just call him Mike.

It doesn't have to look exactly the same: You could use less asterisks, or include the date, for example. And you should use your own name and section number. But it should contain the essential information and have a tidy appearance.

13. There are two places in the program - one in code and one in comments - that contain the strings used in upcase: "Hello" and "UPPER/Lower". Find them and replace them with the correct strings: sz_1 = "Upper/LOWER." and sz_2 = "mIXeD CaSe.." Fix the typo in the comment that calls them both sz_1.

14. Finally, temporarily remove  strXXXX.c from your project (Right-click > Exclude file(s) from current configuration). Run the project. It should build without errors, but won't seem to do anything.

How to exclude

15. Watch the video of Dr. Jones performing tasks similar to those of Task 2.


** Using Debug Project to execute your project/program is referred to in the vernacular as "running it" to avoid confusion with "debugging it" which is used to denote finding and removing errors.


Prelab Checkoff

If you do not have a prelab component (distance student or excused absence), these tasks are to be completed as the first part of Task 1.


Task 1

What you should see if you've followed the steps above: The code in your file left over from upcase is making the strings uppercase.

A different view of the File Registers. Symbol format makes the strings harder to read, but gives us a better understanding of exactly what is at each memory location. For example, the ASCII code for 'P' is 0x50 and we see this character is at address 0x1001.

Watches window


Calling upcase twice.

dostr pointers

dostr:


  ;;          [w0]

  ;;  while (*psz_1 != 0) {

  cp0.b [w0]

  bra Z, dostr_end

There's upcase! (note: intentionally cropped screenshot)

TA Checkoff

Task 2

For more information about these and other debugging methods, consult the MPLAB X IDE User's Guide.

The screenshot should look similar to this but much different.

The PIC has a 24-bit instruction:

From Microchip DS30009715D.

In the first section of program memory, we see:

Program memory of upcase_c_version

A diagram of how the addresses are numerically arranged in this view would look like this, with xx representing phantom bytes:

Instructions have even-numbered addresses. So at address 0x00000, the instruction is 0x040200. At address 0x00002, the instruction is 0x000000.... At address 0x0000E, the instruction is 0x0002FA and etc. See the diagram below.

Odd-numbered memory addresses refer to the upper word of the instruction. The upper byte of the upper word is not implemented. So at address 0x00005 (and 0x00007, 0x00009, 0x0000B, 0x0000D, and 0x0000F) we see 0xFA.

Program memory at beginning of upcase_c_version (with Format set to Code).

(Note: There is a bug in MPLAB where the Go To function does not behave properly in Hex Format when the Hex Display Width is set to One Byte.)

TA Checkoff

Report

Grading

The report is worth 20 points: 10 for the quality of your code/comments, and 10 for neatly and coherently presenting your information to a reader.

The tasks are worth 60 points. If your report indicates that you did not complete or do not understand a task, you will lose credit, even if you performed it during the lab.  The same is true for tasks performed during the prelab.

There are four subtasks. The following non-exhaustive list of errors will result in losing full credit for a subtask:

The following non-exhaustive list of errors will result in losing partial credit for a subtask:

Lab reports that flagrantly violate submission policy (wrong lab, no screenshots, no title page, no text besides headings/labels, mostly blank, code pasted into pdf, paragraphs of lab text pasted in, did wrong case, etc.) will not be accepted. The student will receive a zero for the lab and may resubmit with late penalty.

str_swap

The following source code is from exercise 6.27 of B.A. Jones, R. Reese, and JW Bruce, Microcontrollers: From Assembly Language to C Using the PIC24 Family, 2nd ed. Cengage Learning, 2015. Comments have been added/modified.


// this subroutine implements a string swap.

void str_swap(char* psz_1, char* psz_2){

char c_char;

while (*psz_1 != 0) {

c_char = *psz_1;

*psz_1 = *psz_2;

*psz_2 = c_char;

psz_1++; psz_2++;

}

}


; W0 = psz_1, W1 = psz_2, W2 used for u8_char

str_swap:

; while *psz_1 != 0

cp0.b [W0]

bra Z, str_swap_exit

; u8_char = *psz_1

mov.b [W0], W2


; *psz_1 = *psz_2

mov.b [W1],[W0]


; *psz_2 = u8_char, psz_2++

mov.b W2, [W1++]


; psz_1++

inc W0, W0


bra str_swap


str_swap_exit:

return


; we could call str_swap like this:

;;           W0    W1

;; str_swap(sz_1, sz_2);

mov #sz_1, W0

mov #sz_2, W1

rcall str_swap



Reference (for TAs or the UART-less)

****** strdcase ******

Before...

sz_1: Upper/LOWER.

sz_2: mIXeD CaSe..

After...

sz_1: mixed case..

sz_2: mIXeD CaSe..


****** strxchg ******

Before...

sz_1: Upper/LOWER.

sz_2: mIXeD CaSe..

After...

sz_1: MixEd cAsE..

sz_2: Upper/LOWER.


****** strflip ******

Before...

sz_1: Upper/LOWER.

sz_2: mIXeD CaSe..

After...

sz_1: Upper/LOWER.

sz_2: uPPER/lower.


****** upcase ******

Before...

sz_1: Hello

sz_2: UPPER/lower

After...

sz_1: HELLO

sz_2: UPPER/LOWER