Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC assembly help

Status
Not open for further replies.

yohanevindra

New Member
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?
 

kpatz

New Member
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.
 

Gayan Soyza

Active Member
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:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top