Lab 4: 16/32-Bit, Signed Numbers Assembly Language Programming
This lab has you convert a C program that performs 16/32-bit signed operations into PIC24 assembly language.
Basics
Download the ZIP archive which contains the lab files and extract it directly into C:\ece3724. You should now have a folder called C:\ece3724\lab_assem2.
Inside this folder, there are three different exercises, called case4, case5 and case6. You only need to complete one of these.
If the last digit of your MSU student ID number is:
3,4,5, then you will be doing case4
6,7,8,9, then you will be doing case5
0,1,2, then you will be doing case6
Be sure you do the correct case or you won't get credit for the lab.
Just like Lab 3, for each case, there's a folder containing MPLAB files, an assembly file for you to complete (case#.s), and a C file (case#_check.c) which contains the C code you'll be translating, and also automatically checks your results.
Prelab
The prelab assignment is expected to be completed before you walk into lab.
Gather your materials. To complete the lab, you'll want to refer to:
The Chapter 5 lecture slides from Canvas, "32-bit Extension and Signed Operation"
Sections 1.1 - 1.4 of the ebook, Interacting with the PIC24 Family in Assembly and C.
Your code from lab 3 to remind you how to complete the initial parts of the program for the prelab.
Complete the first three TO DOs:
Declare your variables (5 pts)
Initialize your variables (5 pts)
Assign variables to the designated registers for printing (5 pts)
Make the "code fragment to implement" into a block comment and put your labels in the margin like this (5 pts):
A label put into the case4 code. This isn't enough to get the prelab checkoff. Put in your own labels.
Task 1
Your task is to convert these C fragments to PIC24 assembly language and get 100 (0x63) PASSes.
First, set the number of loops to 10.
Then put your labels into your assembly code to match the way you labeled your C code in the prelab.
Then, for each fragment:
Add register assignments
Write your code, using the provided Input/Process/Output format
Debug the project. Make sure your program runs before moving on to the next fragment.
Now turn your focus to passing the tests:
First make sure you have one PASS and that e is always correct.
Set a breakpoint in your C code and step over (F8). Watch how the C code branches in each iteration and make sure that your assembly code branches the same way.
If your code is taking the correct branches, look to see which variables are incorrect on the first failed iteration. Focus your debugging on the parts of the code that alter these variables.
Once you have 10 PASSes, set NUM_LOOPS back to 100.
When you get all passes, take a screenshot of the final few lines of the simulator output showing that your PIC24 assembly language program output matches the C results.
Debugging
Before receiving help from a TA, you must:
Have labels in your code that match those in the C fragment
Have followed directions by writing one fragment of the program at a time, debugging after each one
Have register assignments in your code
Have your code divided into Input/Process/Output
Have made an attempt to find the error yourself (setting breakpoints, adding printf statements, referring to reference material)
If you suddenly receive an "Error 255 compilation error," exit and restart MPLAB. If you continue to receive this error, close MPLAB again, hit Ctrl-Alt-Delete, and select Task Manager. Close any MPLAB tasks or processes, and restart MPLAB.
TA Checkoff
Show the TA that the values produced by your assembly language program match the C values on loop exit (i.e. you have a PASS for e:63)
Notes
You may not ‘optimize’ away any statements – you are to implement each line of C code exactly as shown. You must use the proper variable names. The variable names reflect their types: The int32_t variables are signed 32-bit values, int16_t variables are signed 16-bit variables, and uint8_t variables are unsigned 8-bit variables.
Use only registers W0 - W7 for this lab. Place 32-bit values with the MSW in the higher-numbered register.
W10 : W9 ; callee saved; the subroutine must preserve these register values
W0 : W1 ; wrong
W1 : W0 ; correct
i32_a+2: i32_a
It is strongly recommended to get into the habit of using odd-numbered registers for the MSW as well.
W2 : W1 ; do not recommend
W7 : W6 ; best practice
W5 : W4 ; best practice
W3 : W2 ; best practice
W1 : W0 ; best practice
i32_a+2: i32_a
Example of 32 bit bitwise AND conditional to help you get started:
; ----W5:W4----
; MSW:LSW MSW:LSW
; W1:W0 W3:W2
; if (i32_k & 0x12345678) {
; Input
mov i32_k, W0 ; LSW
mov i32_k+2, W1 ; MSW - you can swap these first two lines, but i32_k+2 has to go into the upper register
mov #0x1234, W3 ; MSW - see previous comment
mov #0x5678, W2 ; LSW
; Process
and W0, W2, W4 ; one benefit of using odd registers for MSW and even for LSW is finding bugs
and W1, W3, W5 ; you have to AND both MSW and LSW. (There is no andc. Just and and and.b)
cp W4, #0 ; don't forget these two lines
cpb W5, #0 ; cpb0 is not real
; Output
bra Z, ELSE ; if FALSE, go to ELSE:, otherwise execute the if branch
This lab uses SIGNED 32-bit and 16-bit variables. The inequality branches require SIGNED branches. When doing mixed 32-bit and 16-bit operations, the 16-bit value must be SIGN-EXTENDED to 32-bits. You CANNOT use the 'SE' (sign-extend) instruction for this as this sign-extends an 8-bit register value to a 16-bit register value. A handy way to perform sign extension of 16 to 32 bits without adding two extra labels:
B.A. Jones, R. Reese, and JW Bruce, Microcontrollers: From Assembly Language to C Using the PIC24 Family, 2nd ed. Cengage Learning, 2015.
Report
Write a few sentences about what you did in the lab so that an outside reader could understand, e.g. "I translated the following C code into PIC24 assembly..." not "I did case5.s"
Copy/paste the C code you translated (using a monospace font) or include a screenshot of it in the report.
Include the screenshot of your UART window. Write a few sentences about what the reader is looking at.
Look over your report and be sure:
It has a neat, professional appearance (Legible screenshots, no blank pages, etc).
It includes the aforementioned text.
It includes the required 1-2 screenshots, but not extraneous ones (like screenshots of your entire case#.s file, or all 100 PASSes).
It has a title page.
Clean up your case#.s file:
Put a header on it that includes your name, the filename, and lab section number.
Make sure your code has register assignments and correctly uses the Input/Process/Output format.
Remove or replace all lines that say:
Replace this line with your register assigments.
Your code goes here
Code may go here...
...and may also go here.
You do not have to remove the example block comments e.g. "TO DO: Declare other variables above...". However, if you choose to do so, replace them with a brief informative comment such as: ; Variable declarations
Tidy the file so there's not blank lines and random indentations everywhere.
Submit:
PDF file with report, including title page, screenshots, and appropriate text.
Formatted case#.s file with header and comments.