+ Reply to Thread
Results 1 to 3 of 3

Thread: PIC assembly help

  1. #1
    yohanevindra Newbie
    Join Date
    Nov 2008
    Location
    Sydney, Australia & Colombo, Sri Lanka
    Posts
    93

    Default PIC assembly help

    I have a few questions about assembly language with PIC. Is there is a commonly used number you can define it like in C like this
    Code:
    VARIABLE EQU 100
    
    But then to assign a memory location to a variable you use something similar
    Code:
    VARIABLE EQU 0x200
    
    Does this mean you can assign a variable the value 100 and then assign another variable the memory location 0x100?But by default with the PIC isnt 100 = 0x100?

    How do you allocate memory locations with PIC in assembly?


  2. #2
    kpatz Good kpatz Good
    Join Date
    May 2009
    Location
    NH
    Posts
    305

    Default

    I would suggest googling "PIC Tutorial" and reading up on the basics.

    But to answer your immediate question:

    In MPASM, 100 and 0x100 are the same (both hex 100, or decimal 256). You can specify decimal with a dot in front of the number (e.g. .100 = 100 decimal, or d'100'), or binary (b'10101010').

    You can make your code "absolute" or "relocatable" in the assembler. Which way you go is up to you, but relocatable is more flexible, since you don't have to worry about absolute locations; the assembler takes care of this for you.

    In absolute mode, you designate fixed locations for each variable:
    Code:
    var1   equ   0x21
    var2   equ   0x22
    ...
    
    Then when you use var1 in your code, the assembler substitutes 0x21 for it, so that the instruction points to that address.

    In relocatable mode, it's coded like this:

    Code:
          udata
    var1  res 1
    var2  res 1
          code
    (your code goes here)
    
    and then the assembler finds available file registers to put var1 and var2 into, and handles assigning them when you reference them.

    To answer your other question, about literals vs. addresses, most instructions use file register addresses, so for example:

    Code:
    var1      equ  0x21
    ...
              movf var1,w     ; Move contents of file register 0x21 into W
    
    But there are also "literal" instructions, that work with a literal value:

    Code:
    var1      equ  0x21
    ...
              movlw var1     ; W contains 0x21 after this executes
    
    So it's not how you define your symbols that determines if they're "variables" or "literals", it's how you use them.

    To make it easiest transitioning from C, use relocatable mode, declare your variables in udata blocks and just use EQUs for literals.

    One last thing: for special function registers (e.g. STATUS, PORTx, etc.), use the include file for the particular PIC you're using, as it will contain EQUs for all the SFRs and other things that will make your code easier to follow.

  3. #3
    Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent
    Join Date
    Oct 2006
    Location
    Colombo
    Posts
    1,664
    Blog Entries
    1

    Default

    You must know what are variables & constants!

    I don't define them on top of the program.

    I'm doing like this.

    Code:
    	movlw	.100
    	movwf	Value
    
    So you can modify the variables whenever you want.

    To assign a memory location I use cblock

    Code:
    	cblock	20h
            Value
    	Var1
    	Var2
    	Var3
    	endc
    
    Last edited by Gayan Soyza; 16th November 2009 at 09:33 AM.

+ Reply to Thread

Similar Threads

  1. learning PIC assembly
    By t.man in forum Micro Controllers
    Replies: 6
    Latest: 24th September 2009, 02:27 PM
  2. 16bit pic assembly
    By Daniek in forum Micro Controllers
    Replies: 4
    Latest: 27th May 2008, 05:18 PM
  3. Need PIC assembly! Translation....
    By david_5727 in forum Micro Controllers
    Replies: 2
    Latest: 28th December 2007, 02:52 PM
  4. PIC assembly programming
    By aniketrane in forum Micro Controllers
    Replies: 5
    Latest: 11th April 2004, 09:48 PM
  5. PIC Assembly
    By lastiku in forum Micro Controllers
    Replies: 1
    Latest: 25th February 2004, 09:04 PM

Tags for this Thread