Skip to content

Instantly share code, notes, and snippets.

@JJL772
Last active May 29, 2019 00:15
Show Gist options
  • Save JJL772/1019b903ea9b95aa5858b3110c7ed764 to your computer and use it in GitHub Desktop.
Save JJL772/1019b903ea9b95aa5858b3110c7ed764 to your computer and use it in GitHub Desktop.
Reference for XC8's assembler
; Comment
; Assemble for PIC16(L)F15325
processor 16LF15325
; Declare global symbol _main
global _main
; Identifiers are user-defined symbols that represent memory locations/numbers
; Syntax of identifer:
; <identifier>
; for example:
; my_identifier
; the $ identifier is a pretty sick location counter that expands to an address
; $ represents the current line, $+2 is 2 lines down
; Note on instructions:
; in picasm instructions like addwf have a parameter after that specifies where they are to be stored
; 0 and 1 specify where the op result is stored. I actually dont remember which is which...so they have
; some special operands w and f which can be used in place.
addwf 100h, w ; this means the result is stored in w
addwf 100h, f ; this means the result is stored in f
; Bank/page selection
; when accessing memory it's important to select the correct page/bank
; banksel(addr) is used to select the bank of the address
banksel(100h) ; select the bank of 100h
; pagesel(addr) is used in the same way, but for ROM
pagesel($) ; this selects the current page
; use fcall and ljmp for long calls/jumps
; Creates a new psect (program section) called code
; syntax is psect <name>, [flags, ...]
; See xc8 manual for extra flags
; class flag specifies the class for the section, CODE is a class for code.
; delta flag specifies the size of the addressing unit, this means the number of bytes in a pointer
; for pic16 devices this will be 2, (because 16-bit addresses)
; for devices where 32bit addresses are supported, this will be 4, etc.
; global tells the linker to merge this section with other sections of the same name
psect code, class=CODE, delta=2, global
; Creates a new psect called eeprom
; class is EEDATA which means eeprom data. For the PIC16LF15325 this isn't actually valid, as there is no EEPROM
; noexec specifies that the section cannot be exectued
; once again, delta is 2
; space refers to the type of data memory this is:
; 0 - program memory
; 1 - data memory
; 2 - reserved
; 3 - eeprom
psect eeprom, class=EEDATA, delta=2, noexec, space=3
; other flags for psects:
; the "with" flag allows this psect to be placed in the same page as another psect, for example with=text puts the psect
; on the same page as text
;
; optim=[optim1:optim2:...] this flag sets linker optimizations to be used with this psect.
; See xc8 user manual page 282 for optimization options
;
; abs specifies the psect as absolute, meaning it starts at a specific address.
; use the ORG psudo-op to specify the origin address
;
; reloc=[number] (ex: reloc=100h) this flag is used to specify a relocation. if 100h is specified, it means that this
; section MUST reside on a boundry that is a multiple of 100h
;
; local flag specifies the psect as non-mergeable. meaning the psect cannot be merged with other sections of the same name
;
; To use a previously defined psect, use psect <name>, you need not specify all the previous flags
;
psect code
; Labels are declared as follows:
my_label:
; Using the EQU instruction, a new symbol can be defined, NOTE: This is functionally identical to #define my_symbol 10
; this does not actually define a symbol in the data section, for example
my_symbol equ 10
; my_symbol is now 10. it cannot be redefined using equ, but rather by using set
my_symbol set 11
; my_symbol is now 11
; declare an external symbol _function, defined elsewhere
extrn _function
; the DB op can be used to reserve storage for something
my_new_symbol: db 'x', 'y', '\0'
; DW can be used to declare a word, DDW can be used to declare a double word.
; DS is used to reserve, but not initialize memory locations
label: ds 23 ; this reserves 23 bytes of memory
; Dabs is a pretty cool directive.
; it is used to reserve memory at a specific location in memory
; syntax is dabs <memory space>, <address>, <num of bytes> [, symbol]
; symbol is optional
; remember the memory space from before: 0 is prog mem, 1 is data mem, 2 is reserved, 3 is eeprom
dabs 1, 100h, 10 ; declares 10 bytes at addr 100h in data memory, no symbol is associated with it
dabs 1, 200h, 10, var ; does the same as before but at addr 200h and var is the symbol at that addr
; the align directive can be used to align the following items on a specifc boundry
; syntax: align <bytes>
align 2 ; aligns all following code on 2 byte boundry
; rept is an interesting directive that defines an anonymous macro and expands it 3 times
; use with endm
; example:
rept 3
addwf 100h
endm
; ^ that will add the data at address 100h to w 3 times
; Other controls for the assembler are available:
; OPT <option> is used to invoke an option
; To enable/disable optimizations:
; OPT ASMOPT_ON and OPT ASMOPT_OFF can be used
; For a cool title in the assembly listing:
; OPT TITLE "my title"
; and for a subtitle:
; OPT SUBTITLE "my subtitle"
; including the contents of another file
; OPT INCLUDE <path>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment