Continue to Site

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.

Difference in defining constant and memory location

Status
Not open for further replies.
I don't really understand the difference in defining a memory location and a constant. With a memory location, you can either use the cblock directive or "equ". How does MPLAB know which is assigning a memory location and which is just defining a constant? I'm trying to migrate over some application notes codes for the PIC16C over to the PIC18F and I'm running into some errors that I think may have to do with this.

If I'm doing this:
Code:
cblock	00h
	ACC0
	ACC1
	ACC2
	ACC3
	TEMP0
	TEMP1
	TEMP2
	TEMP3
	AEXP
	AARGB0

B0		equ	0
B1		equ	1
B2		equ	2
B3		equ	3
B4		equ	4
B5		equ	5
B6		equ	6
B7		equ	7
MSB		equ	7
LSB		equ	0

With the above, how did it know that the name of location 00h is not ACC0 and B0?

Thanks!
 
When you label a constant with an "EQU" directive, you can use the label whenever you want to use the constant whether it's being used to define a memory location or a literal value. The "f" instructions (i.e. bcf, bsf, movwf, etc etc) tell the assembler it's a memory location whereas a "movlw LABEL" tells the assembler that it's a literal value.
 
A problem with assembly language is that the assembler doesn't distinguish between types of numbers. It can't because of situations like this:-

Code:
samplereg  equ 0x20

clr	samplereg	;  clear address 0x20. samplereg used as an address
movlw	samplereg	; put 0x20 in the working register. 
			;  samplereg used as a constant
movwf	fsr
incf	indf, f		;two lines to show a reason for the line above

It is the code writer's job to make sure that he or she uses the constants or addresses correctly.

There is absolutely no problem with having many constants or addresses set to the same number. It can be helpful and it can make the code easier to read.

for instance:-

Code:
w	equ  0
f	equ  1   ;destination flags

c	equ 0
dc	equ 1
z	equ 2    ;status register bits

bsf	status, c	;set the carry flag
mov	samplereg, w	;move the contents of samplereg into the working register

Now in either of those two lines of code, the second arguments could be 0, c or w. All three will be compiled exactly the same and will run the same.

We use "c" and "w" because they make the code easier to write and easier to understand afterwards.

You have to remember that assemblers are only have to translate one way. When they come across "c" it is translated into 0. "w" is translated into 0. 0 is used as 0. That is a simple function.

The assembler never has to look at 0 and decide if the writer meant 0, "c" or "w" because that would be far more difficult, if not impossible.

So the answer to your question

With the above, how did it know that the name of location 00h is not ACC0 and B0?

is:-

The name of the location 00h could be B0, but it would be perverse to write code that used B0 as a register location.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top