#LyX 1.3 created this file. For more info see http://www.lyx.org/ \lyxformat 221 \textclass book \language english \inputencoding default \fontscheme times \graphics default \paperfontsize 10 \spacing single \papersize letterpaper \paperpackage a4 \use_geometry 1 \use_amsmath 0 \use_natbib 0 \use_numerical_citations 0 \paperorientation portrait \leftmargin 34mm \rightmargin 34mm \secnumdepth 2 \tocdepth 2 \paragraph_separation indent \defskip medskip \quotes_language english \quotes_times 2 \papercolumns 1 \papersides 1 \paperpagestyle default \layout Title \added_space_top vfill \added_space_bottom vfill gputils 0.13.3 \layout Author James Bowman and Craig Franklin \layout Date July 31, 2005 \layout Standard \begin_inset LatexCommand \tableofcontents{} \end_inset \layout Chapter Introduction \layout Standard gputils is a collection of tools for Microchip ( \shape smallcaps TM \shape default ) PIC microcontrollers. It includes gpasm, gplink, and gplib. Each tool is intended to be an open source replacement for a corresponding Microchip (TM) tool. This manual covers the basics of running the tools. For more details on a microcontroller, consult the manual for the specific PICmicro product that you are using. \layout Standard This document is part of gputils. \layout Standard gputils is free software; you can redistribute it and/or modify it under the terms of the \begin_inset LatexCommand \index{GNU} \end_inset GNU General Public \begin_inset LatexCommand \index{License} \end_inset License as published by the Free Software Foundation; either version 2, or (at your option) any later version. \layout Standard gputils is distributed in the hope that it will be useful, but WITHOUT ANY \begin_inset LatexCommand \index{NO WARRANTY} \end_inset WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. \layout Standard You should have received a copy of the GNU General Public License along with gputils; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. \layout Section Tool Flows \layout Standard gputils can be used in two different ways: absolute asm mode and relocatable asm mode. \layout Subsection Absolute Asm Mode \layout Standard In absolute asm mode, an assembly language source file is directly converted into a hex file by gpasm. This method is absolute because the final addresses are hard coded into the source file. \layout Subsection Relocatable Asm Mode \layout Standard In relocatable asm mode, the microcontroller assembly source code is divided into separate modules. Each module is assembled into an object using gpasm. That object can be placed \begin_inset Quotes eld \end_inset anywhere \begin_inset Quotes erd \end_inset in microcontroller's memory. Then gplink is used to resolve symbols references, assign final address, and to patch the machine code with the final addresses. The output from gplink is an absolute executable object. \layout Subsection Which Tool Flow is best? \layout Standard Absolute mode is simple to understand and to use. It only requires one tool, gpasm. Most of the examples on Microchip's website use absolute mode. So why use relocatable mode? \layout Itemize Code can be written without regard to addresses. This makes it easier to write and reuse. \layout Itemize The objects can be archived to create a library, which also simplifies reuse. \layout Itemize Recompiling a project can be faster, because you only compile the portions that have changed. \layout Itemize Files can have local name spaces. The user chooses what symbols are global. \layout Standard Most develpment tools use relocatable objects for these reasons. The few that don't are generally microcontroller tools. Their applications are so small that absolute mode isn't impractical. For PICs, relocatable mode has one big disadvantage. The bank and page control is a challenge. \layout Section Supported processors \layout LyX-Code gputils currently supports the following processors: \layout LyX-Code eeprom8 gen p10f200 p10f202 p10f204 p10f206 \layout LyX-Code p12c508 p12c508a p12c509 p12c509a p12c671 p12c672 \layout LyX-Code p12ce518 p12ce519 p12ce673 p12ce674 p12cr509a p12f508 \layout LyX-Code p12f509 p12f629 p12f635 p12f675 p12f683 p14000 \layout LyX-Code p16c5x p16cxx p16c432 p16c433 p16c505 p16c52 \layout LyX-Code p16c54 p16c54a p16c54b p16c54c p16c55 p16c55a \layout LyX-Code p16c554 p16c557 p16c558 p16c56 p16c56a p16c57 \layout LyX-Code p16c57c p16c58a p16c58b p16c61 p16c62 p16c62a \layout LyX-Code p16c62b p16c620 p16c620a p16c621 p16c621a p16c622 \layout LyX-Code p16c622a p16c63 p16c63a p16c64 p16c64a p16c642 \layout LyX-Code p16c65 p16c65a p16c65b p16c66 p16c662 p16c67 \layout LyX-Code p16c71 p16c710 p16c711 p16c712 p16c715 p16c716 \layout LyX-Code p16c717 p16c72 p16c72a p16c73 p16c73a p16c73b \layout LyX-Code p16c74 p16c745 p16c747 p16c74a p16c74b p16c76 \layout LyX-Code p16c765 p16c77 p16c770 p16c771 p16c773 p16c774 \layout LyX-Code p16c781 p16c782 p16c84 p16c923 p16c924 p16c925 \layout LyX-Code p16c926 p16ce623 p16ce624 p16ce625 p16cr54 p16cr54a \layout LyX-Code p16cr54b p16cr54c p16cr56a p16cr57a p16cr57b p16cr57c \layout LyX-Code p16cr58a p16cr58b p16cr62 p16cr620a p16cr63 p16cr64 \layout LyX-Code p16cr65 p16cr72 p16cr83 p16cr84 p16f505 p16f54 \layout LyX-Code p16f57 p16f59 p16f627 p16f627a p16f628 p16f628a \layout LyX-Code p16f630 p16f636 p16f639 p16f648a p16f676 p16f684 \layout LyX-Code p16f685 p16f687 p16f688 p16f689 p16f690 p16f716 \layout LyX-Code p16f72 p16f73 p16f737 p16f74 p16f76 p16f767 \layout LyX-Code p16f77 p16f777 p16f785 p16f818 p16f819 p16f83 \layout LyX-Code p16f84 p16f84a p16f87 p16f870 p16f871 p16f872 \layout LyX-Code p16f873 p16f873a p16f874 p16f874a p16f876 p16f876a \layout LyX-Code p16f877 p16f877a p16f88 p16f913 p16f914 p16f916 \layout LyX-Code p16f917 p16hv540 p17cxx p17c42 p17c42a p17c43 \layout LyX-Code p17c44 p17c752 p17c756 p17c756a p17c762 p17c766 \layout LyX-Code p17cr42 p17cr43 p18cxx p18c242 p18c252 p18c442 \layout LyX-Code p18c452 p18c601 p18c658 p18c801 p18c858 p18f1220 \layout LyX-Code p18f1320 p18f2220 p18f2320 p18f2331 p18f2410 p18f242 \layout LyX-Code p18f2420 p18f2431 p18f2439 p18f2455 p18f248 p18f2480 \layout LyX-Code p18f2510 p18f2515 p18f252 p18f2520 p18f2525 p18f2539 \layout LyX-Code p18f2550 p18f258 p18f2580 p18f2585 p18f2610 p18f2620 \layout LyX-Code p18f2680 p18f2681 p18f4220 p18f4320 p18f4331 p18f4410 \layout LyX-Code p18f442 p18f4420 p18f4431 p18f4439 p18f4455 p18f448 \layout LyX-Code p18f4480 p18f4510 p18f4515 p18f452 p18f4520 p18f4525 \layout LyX-Code p18f4539 p18f4550 p18f458 p18f4580 p18f4585 p18f4610 \layout LyX-Code p18f4620 p18f4680 p18f4681 p18f6310 p18f6390 p18f6410 \layout LyX-Code p18f6490 p18f64j15 p18f6520 p18f6525 p18f6585 p18f65j10 \layout LyX-Code p18f65j15 p18f6620 p18f6621 p18f6627 p18f6680 p18f66j10 \layout LyX-Code p18f66j15 p18f6720 p18f6722 p18f67j10 p18f8310 p18f8390 \layout LyX-Code p18f8410 p18f8490 p18f84j15 p18f8520 p18f8525 p18f8585 \layout LyX-Code p18f85j10 p18f85j15 p18f8620 p18f8621 p18f8627 p18f8680 \layout LyX-Code p18f86j10 p18f86j15 p18f8720 p18f8722 p18f87j10 rf509af \layout LyX-Code rf509ag rf675f rf675h rf675k sx18 sx20 \layout LyX-Code sx28 \layout LyX-Code \layout Chapter gpasm \layout Section Running gpasm \begin_inset LatexCommand \index{gpasm options} \end_inset \layout Standard The general syntax for running gpasm is \layout LyX-Code gpasm [options] asm-file \layout Standard Where options can be one of: \layout Standard \added_space_top 0.3cm \added_space_bottom 0.3cm \align center \begin_inset Tabular \begin_inset Text \layout Standard Option \end_inset \begin_inset Text \layout Standard Meaning \end_inset \begin_inset Text \layout Standard a \end_inset \begin_inset Text \layout Standard Produce \begin_inset LatexCommand \index{hex file} \end_inset hex file in one of four formats: inhx8m, inhx8s, inhx16, inhx32 (the default). \end_inset \begin_inset Text \layout Standard c \end_inset \begin_inset Text \layout Standard Output a relocatable object \end_inset \begin_inset Text \layout Standard d \end_inset \begin_inset Text \layout Standard Output debug messages \end_inset \begin_inset Text \layout Standard D symbol[=value] \end_inset \begin_inset Text \layout Standard Equivalent to \begin_inset Quotes eld \end_inset #define \begin_inset Quotes erd \end_inset \end_inset \begin_inset Text \layout Standard e [ON|OFF] \end_inset \begin_inset Text \layout Standard Expand macros in listing file \end_inset \begin_inset Text \layout Standard g \end_inset \begin_inset Text \layout Standard Use debug directives for COFF \end_inset \begin_inset Text \layout Standard h \end_inset \begin_inset Text \layout Standard Display the help message \end_inset \begin_inset Text \layout Standard i \end_inset \begin_inset Text \layout Standard \begin_inset LatexCommand \index{case} \end_inset Ignore case in source code. By default gpasms to treats \begin_inset Quotes eld \end_inset fooYa \begin_inset Quotes erd \end_inset and \begin_inset Quotes eld \end_inset FOOYA \begin_inset Quotes erd \end_inset as being different. \end_inset \begin_inset Text \layout Standard I \end_inset \begin_inset Text \layout Standard Specify an include directory \end_inset \begin_inset Text \layout Standard l \end_inset \begin_inset Text \layout Standard List the supported processors \end_inset \begin_inset Text \layout Standard L \end_inset \begin_inset Text \layout Standard Ignore nolist directives \end_inset \begin_inset Text \layout Standard m \end_inset \begin_inset Text \layout Standard Memory dump \end_inset \begin_inset Text \layout Standard M \end_inset \begin_inset Text \layout Standard Output a dependency file \end_inset \begin_inset Text \layout Standard n \end_inset \begin_inset Text \layout Standard Use DOS style newlines (CRLF) in hex file. This option is disabled on win32 systems. \end_inset \begin_inset Text \layout Standard o \end_inset \begin_inset Text \layout Standard Alternate name of hex output file \end_inset \begin_inset Text \layout Standard p \end_inset \begin_inset Text \layout Standard Select target processor \end_inset \begin_inset Text \layout Standard q \end_inset \begin_inset Text \layout Standard Quiet \end_inset \begin_inset Text \layout Standard r \end_inset \begin_inset Text \layout Standard Set the \begin_inset LatexCommand \index{radix} \end_inset radix, i.e. the number base that gpasm uses when interpreting numbers. can be one of \begin_inset Quotes eld \end_inset oct \begin_inset Quotes erd \end_inset , \begin_inset Quotes eld \end_inset dec \begin_inset Quotes erd \end_inset and \begin_inset Quotes eld \end_inset hex \begin_inset Quotes erd \end_inset for bases eight, ten, and sixteen respectively. Default is \begin_inset Quotes eld \end_inset hex \begin_inset Quotes erd \end_inset . \end_inset \begin_inset Text \layout Standard v \end_inset \begin_inset Text \layout Standard Print gpasm version information and exit \end_inset \begin_inset Text \layout Standard w [ 0 | 1 | 2] \end_inset \begin_inset Text \layout Standard Set the message level \end_inset \begin_inset Text \layout Standard y \end_inset \begin_inset Text \layout Standard Enable 18xx extended mode \end_inset \end_inset \layout Standard Unless otherwise specified, gpasm removes the \begin_inset Quotes eld \end_inset .asm \begin_inset Quotes erd \end_inset suffix from its input file, replacing it with \begin_inset Quotes eld \end_inset .lst \begin_inset Quotes erd \end_inset and \begin_inset Quotes eld \end_inset .hex \begin_inset Quotes erd \end_inset for the list and hex output files respectively. On most modern operating systems case is significant in filenames. For this reason you should ensure that filenames are named consistently, and that the \begin_inset Quotes eld \end_inset .asm \begin_inset Quotes erd \end_inset suffix on any source file is in lower case. \layout Standard gpasm always produces a \begin_inset Quotes eld \end_inset .lst \begin_inset Quotes erd \end_inset file. If it runs without errors, it also produces a \begin_inset Quotes eld \end_inset .hex \begin_inset Quotes erd \end_inset file or a \begin_inset Quotes eld \end_inset .o \begin_inset Quotes erd \end_inset file. \layout Subsection Using gpasm with \begin_inset Quotes eld \end_inset make \begin_inset Quotes erd \end_inset \begin_inset LatexCommand \index{make} \end_inset \layout Standard On most operating systems, you can build a project using the make utility. To use gpasm with make, you might have a \begin_inset Quotes eld \end_inset makefile \begin_inset Quotes erd \end_inset like this: \layout LyX-Code tree.hex: tree.asm treedef.inc \layout LyX-Code gpasm tree.asm \layout Standard This will rebuild \begin_inset Quotes eld \end_inset tree.hex \begin_inset Quotes erd \end_inset whenever either of the \begin_inset Quotes eld \end_inset tree.asm \begin_inset Quotes erd \end_inset or \begin_inset Quotes eld \end_inset treedef.inc \begin_inset Quotes erd \end_inset files change. A more comprehensive example of using gpasm with makefiles is included as example1 in the gpasm source distribution. \layout Subsection Dealing with errors \begin_inset LatexCommand \index{error file} \end_inset \layout Standard gpasm doesn't specifically create an error file. This can be a problem if you want to keep a record of errors, or if your assembly produces so many errors that they scroll off the screen. To deal with this if your shell is \begin_inset LatexCommand \index{sh} \end_inset \begin_inset Quotes eld \end_inset sh \begin_inset Quotes erd \end_inset , \begin_inset LatexCommand \index{bash} \end_inset \begin_inset Quotes eld \end_inset bash \begin_inset Quotes erd \end_inset or \begin_inset LatexCommand \index{ksh} \end_inset \begin_inset Quotes eld \end_inset ksh \begin_inset Quotes erd \end_inset , you can do something like: \layout LyX-Code gpasm tree.asm 2>&1 | tee tree.err \layout Standard This redirects standard error to standard output ( \begin_inset Quotes eld \end_inset 2>&1 \begin_inset Quotes erd \end_inset ), then pipes this output into \begin_inset Quotes eld \end_inset \begin_inset LatexCommand \index{tee} \end_inset tee \begin_inset Quotes erd \end_inset , which copies it input to \begin_inset Quotes eld \end_inset tree.err \begin_inset Quotes erd \end_inset , and then displays it. \layout Section Syntax \layout Subsection File structure \layout Standard gpasm source files consist of a series of lines. Lines can contain a \begin_inset LatexCommand \index{labels} \end_inset label (starting in column 1) or an operation (starting in any column after 1), both, or neither. \begin_inset LatexCommand \index{comments} \end_inset Comments follow a \begin_inset Quotes eld \end_inset ; \begin_inset Quotes erd \end_inset character, and are treated as a newline. Labels may be any series of the letters A-z, digits 0-9, and the underscore ( \begin_inset Quotes eld \end_inset _ \begin_inset Quotes erd \end_inset ); they may not begin with a digit. Labels may be followed by a colon ( \begin_inset Quotes eld \end_inset : \begin_inset Quotes erd \end_inset ). \layout Standard An operation is a single identifier (the same rules as for a label above) followed by a space, and a comma-separated list of parameters. For example, the following are all legal source lines: \layout LyX-Code ; Blank line \newline loop sleep ; Label and operation \newline incf 6,1 ; Operation with 2 parameters \newline goto loop ; Operation with 1 parameter \layout Subsection Expressions \layout Standard gpasm supports a full set of \begin_inset LatexCommand \index{operators} \end_inset operators, based on the C operator set. The operators in the following table are arranged in groups of equal precedence , but the groups are arranged in order of increasing precedence. When gpasm encounters operators of equal precedence, it always evaluates from left to right. \layout Standard \added_space_top 0.3cm \added_space_bottom 0.3cm \align center \begin_inset Tabular \begin_inset Text \layout Standard Operator \end_inset \begin_inset Text \layout Standard Description \end_inset \begin_inset Text \layout Standard = \end_inset \begin_inset Text \layout Standard assignment \end_inset \begin_inset Text \layout Standard || \end_inset \begin_inset Text \layout Standard logical or \end_inset \begin_inset Text \layout Standard && \end_inset \begin_inset Text \layout Standard logical and \end_inset \begin_inset Text \layout Standard & \end_inset \begin_inset Text \layout Standard bitwise and \end_inset \begin_inset Text \layout Standard | \end_inset \begin_inset Text \layout Standard bitwise or \end_inset \begin_inset Text \layout Standard ^ \end_inset \begin_inset Text \layout Standard bitwise exclusive-or \end_inset \begin_inset Text \layout Standard < \end_inset \begin_inset Text \layout Standard less than \end_inset \begin_inset Text \layout Standard > \end_inset \begin_inset Text \layout Standard greater than \end_inset \begin_inset Text \layout Standard == \end_inset \begin_inset Text \layout Standard equals \end_inset \begin_inset Text \layout Standard != \end_inset \begin_inset Text \layout Standard not equals \end_inset \begin_inset Text \layout Standard >= \end_inset \begin_inset Text \layout Standard greater than or equal \end_inset \begin_inset Text \layout Standard <= \end_inset \begin_inset Text \layout Standard less than or equal \end_inset \begin_inset Text \layout Standard << \end_inset \begin_inset Text \layout Standard left shift \end_inset \begin_inset Text \layout Standard >> \end_inset \begin_inset Text \layout Standard right shift \end_inset \begin_inset Text \layout Standard + \end_inset \begin_inset Text \layout Standard addition \end_inset \begin_inset Text \layout Standard - \end_inset \begin_inset Text \layout Standard subtraction \end_inset \begin_inset Text \layout Standard * \end_inset \begin_inset Text \layout Standard multiplication \end_inset \begin_inset Text \layout Standard / \end_inset \begin_inset Text \layout Standard division \end_inset \begin_inset Text \layout Standard % \end_inset \begin_inset Text \layout Standard modulo \end_inset \begin_inset Text \layout Standard UPPER \end_inset \begin_inset Text \layout Standard upper byte \end_inset \begin_inset Text \layout Standard HIGH \end_inset \begin_inset Text \layout Standard high byte \end_inset \begin_inset Text \layout Standard LOW \end_inset \begin_inset Text \layout Standard low byte \end_inset \begin_inset Text \layout Standard - \end_inset \begin_inset Text \layout Standard negation \end_inset \begin_inset Text \layout Standard ! \end_inset \begin_inset Text \layout Standard logical not \end_inset \begin_inset Text \layout Standard ~ \end_inset \begin_inset Text \layout Standard bitwise no \end_inset \end_inset \layout Standard \align left Any symbol appearing in column 1 may be assigned a value using the assignment operator (=) in the previous table. Additionally, any value previously assigned may be modified using one of the operators in the table below. Each of these operators evaluates the current value of the symbol and then assigns a new value based on the operator. \layout Standard \added_space_top 0.3cm \added_space_bottom 0.3cm \align center \begin_inset Tabular \begin_inset Text \layout Standard Operator \end_inset \begin_inset Text \layout Standard Description \end_inset \begin_inset Text \layout Standard = \end_inset \begin_inset Text \layout Standard assignment \end_inset \begin_inset Text \layout Standard ++ \end_inset \begin_inset Text \layout Standard increment by 1 \end_inset \begin_inset Text \layout Standard -- \end_inset \begin_inset Text \layout Standard decrement by 1 \end_inset \begin_inset Text \layout Standard += \end_inset \begin_inset Text \layout Standard increment \end_inset \begin_inset Text \layout Standard -= \end_inset \begin_inset Text \layout Standard decrement \end_inset \begin_inset Text \layout Standard *= \end_inset \begin_inset Text \layout Standard multiply \end_inset \begin_inset Text \layout Standard /= \end_inset \begin_inset Text \layout Standard divide \end_inset \begin_inset Text \layout Standard %= \end_inset \begin_inset Text \layout Standard modulo \end_inset \begin_inset Text \layout Standard <<= \end_inset \begin_inset Text \layout Standard left shift \end_inset \begin_inset Text \layout Standard >>= \end_inset \begin_inset Text \layout Standard right shift \end_inset \begin_inset Text \layout Standard &= \end_inset \begin_inset Text \layout Standard bitwise and \end_inset \begin_inset Text \layout Standard |= \end_inset \begin_inset Text \layout Standard bitwise or \end_inset \begin_inset Text \layout Standard ^= \end_inset \begin_inset Text \layout Standard bitwise exclusive-or \end_inset \end_inset \layout Subsection Numbers \begin_inset LatexCommand \index{radix} \end_inset \layout Standard \begin_inset LatexCommand \label{sec:numbers} \end_inset gpasm gives you several ways of specifying numbers. You can use a syntax that uses an initial character to indicate the number's base. The following table summarizes the alternatives. Note the C-style option for specifying hexadecimal numbers. \layout Standard \added_space_top 0.3cm \added_space_bottom 0.3cm \align center \begin_inset Tabular \begin_inset Text \layout Standard base \end_inset \begin_inset Text \layout Standard general syntax \end_inset \begin_inset Text \layout Standard 21 decimal written as \end_inset \begin_inset Text \layout Standard binary \end_inset \begin_inset Text \layout Standard B'[01]*' \end_inset \begin_inset Text \layout Standard B'10101' \end_inset \begin_inset Text \layout Standard octal \end_inset \begin_inset Text \layout Standard O'[0-7]*' \end_inset \begin_inset Text \layout Standard O'25' \end_inset \begin_inset Text \layout Standard decimal \end_inset \begin_inset Text \layout Standard D'[0-9]*' \end_inset \begin_inset Text \layout Standard D'21' \end_inset \begin_inset Text \layout Standard hex \end_inset \begin_inset Text \layout Standard H'[0-F]*' \end_inset \begin_inset Text \layout Standard H'15' \end_inset \begin_inset Text \layout Standard hex \end_inset \begin_inset Text \layout Standard 0x[0-F]* \end_inset \begin_inset Text \layout Standard 0x15 \end_inset \end_inset \layout Standard When you write a number without a specifying prefix such as \begin_inset Quotes eld \end_inset 45 \begin_inset Quotes erd \end_inset , gpasm uses the current radix (base) to interpret the number. You can change this radix with the RADIX directive, or with the \begin_inset Quotes eld \end_inset -r \begin_inset Quotes erd \end_inset option on gpasm's command-line. The default radix is hexadecimal. \layout Standard If you do not start hexadecimal numbers with a digit, gpasm will attempt to interpret what you've written as an identifier. For example, instead of writing C2, write either 0C2, 0xC2 or H'C2'. \layout Standard Case is not significant when interpreting numbers: 0ca, 0CA, h'CA' and H'ca' are all equivalent. \layout Standard Several legacy mpasm number formats are also supported. These formats have various shortcomings, but are still supported. The table below summarizes them. \layout Standard \added_space_top 0.3cm \added_space_bottom 0.3cm \align center \begin_inset Tabular \begin_inset Text \layout Standard base \end_inset \begin_inset Text \layout Standard general syntax \end_inset \begin_inset Text \layout Standard 21 decimal written as \end_inset \begin_inset Text \layout Standard binary \end_inset \begin_inset Text \layout Standard [01]*b \end_inset \begin_inset Text \layout Standard 10101b \end_inset \begin_inset Text \layout Standard octal \end_inset \begin_inset Text \layout Standard q'[0-7]*' \end_inset \begin_inset Text \layout Standard q'25' \end_inset \begin_inset Text \layout Standard octal \end_inset \begin_inset Text \layout Standard [0-7]*o \end_inset \begin_inset Text \layout Standard 25o \end_inset \begin_inset Text \layout Standard octal \end_inset \begin_inset Text \layout Standard [0-7]*q \end_inset \begin_inset Text \layout Standard 25q \end_inset \begin_inset Text \layout Standard decimal \end_inset \begin_inset Text \layout Standard 0-9]*d \end_inset \begin_inset Text \layout Standard 21d \end_inset \begin_inset Text \layout Standard decimal \end_inset \begin_inset Text \layout Standard .[0-9]* \end_inset \begin_inset Text \layout Standard .21 \end_inset \begin_inset Text \layout Standard hex \end_inset \begin_inset Text \layout Standard [0-F]*h \end_inset \begin_inset Text \layout Standard 15h \end_inset \end_inset \layout Standard You can write the \begin_inset LatexCommand \index{ASCII} \end_inset ASCII code for a \begin_inset LatexCommand \index{character} \end_inset character X using 'X', or A'X'. \layout Subsection Preprocessor \layout Standard \begin_inset LatexCommand \index{include} \end_inset A line such as: \layout LyX-Code include foo.inc \layout Standard will make gpasm fetch source lines from the file \begin_inset Quotes eld \end_inset foo.inc \begin_inset Quotes erd \end_inset until the end of the file, and then return to the original source file at the line following the include. \layout Standard Lines beginning with a \begin_inset Quotes eld \end_inset # \begin_inset Quotes erd \end_inset are preprocessor directives, and are treated differently by gpasm. They may contain a \begin_inset Quotes eld \end_inset #define \begin_inset Quotes erd \end_inset , or a \begin_inset Quotes eld \end_inset #undefine \begin_inset Quotes erd \end_inset directive. \layout Standard Once gpasm has processed a line such as: \layout LyX-Code #define X Y \layout Standard every subsequent occurrence of X is replaced with Y, until the end of file or a line \layout LyX-Code #undefine X \layout Standard appears. \layout Standard The preprocessor will replace an occurance of #v(expression) in a symbol with the value of \begin_inset Quotes eld \end_inset expression \begin_inset Quotes erd \end_inset in decimal. In the following expression: \layout LyX-Code number equ 5 \layout LyX-Code label_#v( (number +1) * 5 )_suffix equ 0x10 \layout Standard gpasm will place the symbol \begin_inset Quotes eld \end_inset label_30_suffix \begin_inset Quotes erd \end_inset with a value of 0x10 in the symbol table. \layout Standard The preprocessor in gpasm is only \shape italic like \shape default the C preprocessor; its syntax is rather different from that of the C preproces sor. gpasm uses a simple internal preprocessor to implement \begin_inset Quotes eld \end_inset include \begin_inset Quotes erd \end_inset , \begin_inset Quotes eld \end_inset #define \begin_inset Quotes erd \end_inset and \begin_inset Quotes eld \end_inset #undefine \begin_inset Quotes erd \end_inset . \layout Subsection Processor header files \layout Standard gputils distributes the Microchip processor header files. These files contain processor specific data that is helpful in developing PIC applications. The location of these files is reported in the gpasm help message. Use the INCLUDE directive to utilize the appropriate file in your source code. Only the name of the file is required. gpasm will search the default path automatically. \layout Section Directives \layout Subsection Code generation \layout Standard In absolute mode, use the ORG directive to set the PIC memory location where gpasm will start assembling code. If you don't specify an address with ORG, gpasm assumes 0x0000. In relocatable mode, use the CODE directive. \layout Subsection Configuration \layout Standard You can choose the fuse settings for your PIC implementation using the __CONFIG directive, so that the hex file set the fuses explicitly. Naturally you should make sure that these settings match your PIC hardware design. \layout Standard The __MAXRAM and __BADRAM directives specify which RAM locations are legal. These directives are mostly used in processor-specific configuration files. \layout Subsection Conditional assembly \layout Standard The IF, IFNDEF, IFDEF, ELSE and ENDIF directives enable you to assemble certain sections of code only if a condition is met. In themselves, they do not cause gpasm to emit any PIC code. The example in section \begin_inset LatexCommand \ref{sec:macros} \end_inset for demonstrates conditional assembly. \layout Subsection Macros \layout Standard \begin_inset LatexCommand \label{sec:macros} \end_inset gpasm supports a simple macro scheme; you can define and use macros like this: \layout LyX-Code any macro parm \newline movlw parm \layout LyX-Code endm \layout LyX-Code ... \layout LyX-Code any 33 \layout Standard A more useful example of some macros in use is: \layout LyX-Code ; Shift reg left \layout LyX-Code slf macro reg \layout LyX-Code clrc \layout LyX-Code rlf reg,f \layout LyX-Code endm \layout LyX-Code \layout LyX-Code ; Scale W by \begin_inset Quotes eld \end_inset factor \begin_inset Quotes erd \end_inset . Result in \begin_inset Quotes eld \end_inset reg \begin_inset Quotes erd \end_inset , W unchanged. \layout LyX-Code scale macro reg, factor \layout LyX-Code if (factor == 1) \layout LyX-Code movwf reg ; 1 X is easy \layout LyX-Code else \layout LyX-Code scale reg, (factor / 2) ; W * (factor / 2) \layout LyX-Code slf reg,f ; double reg \layout LyX-Code if ((factor & 1) == 1) ; if lo-bit set .. \layout LyX-Code addwf reg,f ; .. add W to reg \layout LyX-Code endif \layout LyX-Code endif \layout LyX-Code endm \layout Standard This recursive macro generates code to multiply W by a constant \begin_inset Quotes eld \end_inset factor \begin_inset Quotes erd \end_inset , and stores the result in \begin_inset Quotes eld \end_inset reg \begin_inset Quotes erd \end_inset . So writing: \layout LyX-Code scale tmp,D'10' \layout Standard is the same as writing: \layout LyX-Code movwf tmp ; tmp = W \layout LyX-Code clrc \layout LyX-Code rlf tmp,f ; tmp = 2 * W \layout LyX-Code clrc \layout LyX-Code rlf tmp,f ; tmp = 4 * W \layout LyX-Code addwf tmp,f ; tmp = (4 * W) + W = 5 * W \layout LyX-Code clrc \layout LyX-Code rlf tmp,f ; tmp = 10 * W \layout Subsection $ \layout Standard $ expands to the address of the instruction currently being assembled. If it's used in a context other than an instruction, such as a conditional, it expands to the address the next instruction would occupy, since the assembler's idea of current address is incremented after an instruction is assembled. $ may be manipulated just like any other number: \layout LyX-Code $ \layout LyX-Code $ + 1 \layout LyX-Code $ - 2 \layout Standard and can be used as a shortcut for writing loops without labels. \layout LyX-Code LOOP: BTFSS flag,0x00 \layout LyX-Code GOTO LOOP \layout LyX-Code \layout LyX-Code BTFSS flag,0x00 \layout LyX-Code GOTO $ - 1 \layout Subsection Suggestions for structuring your code \layout Standard \begin_inset LatexCommand \label{sec:structuring} \end_inset Nested IF operations can quickly become confusing. Indentation is one way of making code clearer. Another way is to add braces on IF, ELSE and ENDIF, like this: \layout LyX-Code IF (this) ; { \layout LyX-Code ... \layout LyX-Code ELSE ; }{ \layout LyX-Code ... \layout LyX-Code ENDIF ; } \layout Standard After you've done this, you can use your text editor's show-matching-brace to check matching parts of the IF structure. In vi this command is \begin_inset Quotes eld \end_inset % \begin_inset Quotes erd \end_inset , in emacs it's M-C-f and M-C-b. \layout Subsection Directive summary \begin_inset LatexCommand \label{sec:directivesummary} \end_inset \layout Subsection* __BADRAM \begin_inset LatexCommand \index{BADRAM} \end_inset \layout LyX-Code __BADRAM [, -. See any processor-specific header file for an example. \layout Standard See also: __MAXRAM \layout Subsection* __CONFIG \begin_inset LatexCommand \index{CONFIG} \end_inset \layout LyX-Code __CONFIG \layout Standard Sets the PIC processor's configuration fuses. \layout Subsection* __IDLOCS \begin_inset LatexCommand \index{IDLOCS} \end_inset \layout LyX-Code __IDLOCS or __IDLOCS , \layout Standard Sets the PIC processor's identification locations. For 12 and 14 bit processors, the four id locations are set to the hexadecimal value of expression. For 18cxx devices idlocation expression1 is set to the hexadecimal value of expression2. \layout Subsection* __MAXRAM \begin_inset LatexCommand \index{MAXRAM} \end_inset \layout LyX-Code __MAXRAM \layout Standard Instructs gpasm that an attempt to use any RAM location above the one specified should be treated as an error. See any processor specific header file for an example. \layout Standard See also: __BADRAM \layout Subsection* BANKISEL \begin_inset LatexCommand \index{BANKISEL} \end_inset \layout LyX-Code BANKISEL