Syntax Errors
When the program source code for a PIC program has been created in the editor, it must be converted into machine code for downloading to the chip. This is carried out by the
assembler program, MPASMWIN.EXE, which analyzes the source code text line by line, converts the instruction mnemonics into a list of corresponding binary codes and saves it as PROGNAME.HEX for downloading into the chip. Only valid statements as defined in the PIC instruction set will be recognized and successfully converted. Assembler directives provide additional programming instructions, but these are not converted to machine code (see Chapter 6).
Before starting a project, create a folder to keep the project files in, named, for example, MOT1. In MPLAB, the source code is created in an edit window opened by hitting the New File button. Type in the file name and save it immediately in the application folder as MOT1.ASM, or any file name with an ASM extension. It is a good idea to keep a backup version of the file set on a different drive (USB stick, portable drive or network).
When the program has been entered and saved, the project menu item ‘Quickbuild’ will assemble a single file. If required, a project can be created, but this is only really necessary for more complex applications using multiple source files or a higher level language (usually C). In the Project menu, select ‘New .’ and call the project MOT1, or the same name as the source code. Now select ‘Add Files to Project .’ and select the source code created above. The program can now be assembled by selecting ‘Build All’, and the project saved. Alternatively, the workspace can be saved, that is, the screen configuration and working files.
In the source code file, numerical formatting, assembler directives and so on must all be used correctly. If they are not, error messages will be generated when the program is assembled. These describe the syntax errors that have been found. The error messages are saved in a text file PROGNAME.ERR, and displayed when the assembler is finished. A typical set of messages is shown below:
To generate this list, deliberate errors were introduced into the demo program MOT1.ASM, and the messages selected from the error file MOT1.ERR. There are three levels of error shown: ‘Message’, ‘Warning’ and ‘Error’. The source code line number where the problem was found is indicated, and the type of problem that the assembler thinks is present. However, a word of warning: owing to the presence of the error itself, the assembler may be misled as to the actual error. Consequently, the message generated is not always entirely accurate. For example, the incorrect instruction mnemonic at line 58 caused the assembler to misinterpret ‘Count’ as an illegal op-code.
The PROCESSOR directive was misplaced, causing a non-fatal warning, which would not itself prevent successful assembly of the program. The TRIS instruction also caused a warning in the MPLAB assembler, because its use is not recommended, but will still be successfully assembled. It is used in our examples because the alternative method of port initialization, using register bank selection, is more complicated.
The instruction mnemonic DECF was misspelt as DEC, causing the errors at line 58. This contributed to the register label count being misinterpreted. The register label ‘Timer’ was missed out of the EQU statements at the top of the program, causing the error at line 71. The jump destination ‘again1’ has been incorrectly labeled ‘again’, causing the error at line 73. Finally, the END directive had been omitted at the end of the program, causing the message ‘Expected (END)’.
The message ‘Using default destination of 1 (file)’ refers to the fact that the full syntax for MOVWF instruction has not been used. Using the full syntax, the destination for the result of the operation is specified as the file register or the working register, by placing a W (0) or F (1) after the destination register number or label. In the examples throughout this text, we take advantage of the assumption by the assembler that the destination is the file register if not specified in the instruction; this simplifies the source code. When the error messages have been studied carefully, and printed out if necessary, the source code must be re-edited and reassembled until it is correct. The different levels of error message (message, warning and error) can be selectively suppressed in the list file output using the list file options with the directive LIST. These options can also be set in the Project, Build Options dialogue in MPLAB by selecting the Output Category under the Assembler tab, then the preferred Diagnostics level.