Debugging with Microchip's MPLAB-IDE

Getting Ready

First of all, you must make sure that MPLAB is installed on your computer. Go back to CSCI 255 Lab 1 to make sure this is done.

Now, create a project called lab02.prj and add a node lab02.asm to it. Be sure you are in MPLAB-SIM development mode for the PIC12C671 processor. Again, if you need help, go back to Lab 1 or get the attention of your lab instructor.

Entering the first pass

Open and save a new empty file called lab02.asm with

Your program is supposed to be an PIC assembly language implementation of the following C code:

        squ = 0 ;
        odd = 1 ;
                squ = squ + odd ;
                odd = odd + 2 ;

which is supposed to compute, in squ, the square numbers, 0, 1, 4, 9, ..., while computing, in odd, the odd numbers, 1, 3, 5, 7, ...

Because the file registers of the PIC can only hold eight bits, we'll have to use multiple registers to store numbers greater than 255. We'll use two PIC registers, 30h and 31h, which we'll call odd1 and odd0 in our program, for the C variable odd. We'll use three PIC registers, 20h, 21h, and 22h, or squ2, squ1, and squ0, for C's squ.

Type, or cut and paste, the following code into the assembler text window:

        list    p=12c671
        include <>

        org     0h      ; reset vector
reset   goto    start

        org     5h
start                   ; this is where execution begins

squ2    equ     20h     ; These three bytes are for squ
squ1    equ     21h
squ0    equ     22h

odd1    equ     30h     ; These two bytes are for odd
odd0    equ     31h

        clrf    squ2
        clrf    squ1
        clrf    squ0

        clrf    odd1
        clrf    odd0
        incf    odd0,F

loop1   movf    odd0,W
        addwf   squ0,F
        btfsc   STATUS,C
        incf    squ1,F
        movf    odd1,W
        addwf   squ1,F
        btfsc   STATUS,C
        incf    squ2,F

        incf    odd0,F
        btfsc   STATUS,C
        incf    odd1,F
        incf    odd0,F

        goto    loop1


By the way, when I did the cut-and-paste as a single operation at home, MPLAB crashed. Doing the cut-and-paster in smaller operations worked.

Once you've entered the program, use the following command to assemble it. Don't go on until your code assembles without a problem.

Commenting code

Now, sit back for a mini-lecture on the PIC assembly language instructions used in this program. You might do well to open up a second browser window loaded with our Essence of PIC Arithmetic page. You also need to know a bit about the C and Z bits. This is explained in Special Registers in the PIC.

Even PIC assembler programmers are expected to comment code. Go through and add at least four instructive comments like:

to your code. When you're done, get the instructor over for an inspection.

Watch windows and F-keys

Bring up a file register window and use the tool bar buttons to step through your program for a few instructions.

There's way too much stuff in the file register window. Let's create a watch window to reduce the screen clutter.

When you get to the Add Watch Symbol dialog, add your five program variables for the odd and square numbers.
MPLAB Add Watch Symbol dialog
Hopefully, you'll end up with a watch window similar to:
MPLAB watch window

Arrange your code and watch windows so they don't overlap and start moving through your program. But this time use the following F-keys.

To reset the program.
To move a single instruction.
To run.
To halt.

Step through your program until squ0 has the value H'24', or 36, and then show your setup to the lab instructor.


Now go into warp speed by pressing F9. Your watch window should show some interesting values, such as:

30      odd1   H'00'                              
31      odd0   H'96'                              
20      squ2   H'00'                              
21      squ1   H'95'                              
22      squ0   H'F9'    

We're going to see if your program is working by using the Windows calculator program to see if you have a real square number. Start up the calculator program and put it into scientific view. Type in your alleged square, in my case 95F9, as a hexadecimal number and then display it in decimal. Now see if your number really is a square by raising it to the .5 power. Alas, I got 195.941317745900647497516371350066. Time to debug.

We'll make this easy for you. Alternate between F9 and F5 a few times and look at the value of odd1. It's always zero! Notice the instructions:

        incf    odd0,F
        btfsc   STATUS,C

There's a problem here, incr doesn't set the C bit. It sets Z.

Fix the bug and try again. Let's see this time I get:

30      odd1   H'06'                              
31      odd0   H'A0'                              
20      squ2   H'07'                              
21      squ1   H'F9'                              
22      squ0   H'00'    

odd1 is looking much better. Let's try out the square number. Hexadecimal 7F900 is 522496, and the square root of 522496 is 722.838847876897108873183914844197!

Break points

Let's try out some more MPLAB tools. Use

to bring up the Break Point Settings dialog. This is a dialog with a picky interface. Let's add a break points that Start and End at loop1. Don't forget to check the little green check box!
MPLAB Break Point Settings dialog

Now close the break point dialog and reset (F6) your program. From now on your program will stop at loop1. Continually press F9 to examine the values at the beginning of the loop. Show off this skill to your lab instructor.

A big hint

By trial and error (binary search), the instructor has determined that something bad happens when the loop is executed for the 363'rd time. Go back into the Break Point Settings dialog. Select your loop1 break. Check on Address is Qualified. Make the Pass Count to be 363. And finally press the Set button.
MPLAB Break Point Settings dialog
This will cause the program to stop the 363'rd time it reaches loop1.

Now reset the program and run it. You should halt with the following values.

30      odd1   H'02'                              
31      odd0   H'D5'                              
20      squ2   H'01'                              
21      squ1   H'FF'                              
22      squ0   H'E4'    

Notice how close squ2 is to rolling over. Single step through the code using F7. When you get to

        incf    squ1,F

call the instructor.

The final fix

Using the skills of your fellow lab mates, fix the bug!

Beyond the final fix

If you have time, add a fourth byte to keep up with the square numbers.

Return to CSCI 255 home page