Program Assembly
To create the PIC program, the MPLAB IDE development system must be downloaded and installed from the Microchip website www.microchip.com. After starting the software, clicking on the new file button opens a source code edit window. The code for the demo programs can be entered and saved in a suitable folder, using the same name as the fileset,
e.g. BIN4. The source code is saved as APPNAME.ASM. If the source code already exists, it can be reopened in the usual way. The sample files may be downloaded from www.picmicros.org.uk. A workspace file will be found, BIN4.MCW, which will open all the relevant windows automatically (File, Open Workspace).
Note that in previous versions of MPASMWIN there was a limited file path length, so a folder near the root of the drive is desirable. If the file path is too long, an error message may be generated by the assembler, but it will not state the cause explicitly. This is simply a historical limitation of the assembler, but can prevent successful assembly for no obvious reason. This problem seems to have been fixed in the current version.
Once entered or opened in the edit window, the source code can be assembled in MPLAB by selecting ‘Quickbuild sourcefile.asm’ from the Project menu. The correct processor type must first be selected via the configuration menu, ‘select device’. The assembler program (MPASM) takes the source code text and decodes it character by character, line by line, starting at the top left. The corresponding machine code for each line in the source code is generated until the END directive is detected. The binary code created is automatically saved as a file called BIN4.HEX in the same folder as the source code. At the same time, several other files are also created, some of which are needed for debugging.
In Proteus VSM, the circuit schematic must be created first, and then the program attached to the MCU, by selecting Source, Add/Remove Source Files from the menu. The processor type and assembler must be selected and a New source file created or attached. The program is assembled by selecting Build All in the Source menu, and is automatically reassembled after editing when the simulator is set to run, which makes source code debugging quick and easy. Application creation in Proteus VSM is detailed in Appendix E.
Syntax Errors
If there are any syntax errors in the source code, such as spelling, layout, punctuation or failure to define labels properly, error messages will be generated by the assembler. These will be displayed in a separate window, indicating the type of error and line number. The messages and line numbers must be noted, or the error file, BIN4.ERR, printed out then the necessary changes made to the source code. The error is sometimes on a previous line to the one indicated, and sometimes a single error can generate more than one message. Warnings and information messages can usually be ignored, and can be disabled. There are more details about error messages in Chapter 9.
You may receive the following messages:
The first warning is caused by the special instruction TRIS, which is not part of the main instruction set. It is a simple way of initializing the port, and there is an alternative method using register bank selection, which is preferred in real applications. This will be introduced later.
The message about the ‘default destination’ is caused by the simplified syntax used in these programs, where the file register is not explicitly specified as the destination in instructions where the result can be placed in either the file register or the working register. The assembler assumes that the file register is the destination by default, and we are taking advantage of this to simplify the source code.
When all errors have been eliminated and the program has been successfully assembled, the machine code can be inspected by selecting ‘View’, ‘Program Memory’. Note that the source code labels are not reproduced, as the program code has been ‘disassembled’ (recreated) from the machine code. That is, the hex file has been converted back to mnemonic form so that it can be checked against the original.
List File
A program ‘list file’ BIN4.LST is produced by the assembler, which contains the source code, the machine code, error messages and other information all in one listing (Table 4.4). This is useful for analyzing the program and assembler operations, and debugging the source code.
The list file header shows the assembler version used and source file details. The column headings are:
LOC: memory location addresses at which the machine code will be stored VALUE: the numerical value with which equate labels will be replaced OBJECT CODE: machine code produced for each instruction
Note that no machine code is produced by lines that are occupied by a full line comment. The actual program starts to be produced at line 00041. The machine code for the first instruction is shown in column 2 (3000), and the address where it will be stored in the chip when downloaded is shown in column 1 (0000). The whole program will occupy locations 0000 to 000F (16 instructions).
If we study the machine code, we can see how the labeling works; for example, the last instruction ‘GOTO start’ is encoded as 2808, and the 08 refers to address 0008 in column 1, the location with the label ‘start’. The assembler program has replaced the label with the corresponding numerical address for the jump destination. Similarly, the label ‘porta’ is replaced with its file register number 05 in the instruction code to test the input, 1C05.
The label values are listed again in the symbol table. These values will be used by the simulator to allow the user to display the simulated registers by label. The amount of program memory used, 16 locations, 0000 to 000F, is shown in graphical format in the memory usage map, and finally a total of errors, warnings and messages given. If there are fatal errors, which prevent successful assembly of the program, the list file will not be produced.