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.

(SET VARIABLE) and (EQU CONSTANT and CBLOCK)

Status
Not open for further replies.

lord loh.

Member
I did post this question at the microchip forum and got a few answers that to some extent cleared my doubts... But I am still not very clear about these...

I know the difference between (SET VARIABLE) and (EQU CONSTANT and CBLOCK)

I want to know three things..
1. Difference between SET and Variable.
2. Difference between CBLOCK CONSTANT and EQU
3. Difference between Symbol(const/var) and Assembler(const/var)

Thank you.
 
an equ is equivalent of the #define in C , which is useful in developing readable code
like
Code:
LCD_E    equ  6
LCD_PORT equ PORTA
.
.
bsf     LCD_PORT,LCD_E

is more readable than 
.
.
bcf    PORTA,6

the CBLOCK is a compiler directive to allocate a memory block
Code:
CBLOCK equ 0x20
var1
var2
ENDC
allocates variable var1 to GPR @ 0x20 and var2 to GPR @ 0x21
 
Assembler Pseudo Operations

I'm going out on a limb here. At the dawn of computer time, when assemblers were the only dance in town, the bright folks who invented the first assemblers decreed that a line of text could contain a machine instruction or an assembler directive also known as a pseudo-operation.

EQU and SET were among the first pseudo-operations invented. The syntax was:
Code:
<symbol>  EQU  <constant_expression>
<symbol>  SET  <constant_expression>
Notice the correspondence to the syntax for a machine instruction. Once a <symbol> was equated to a <constant_expression> it had to retain that value for the entire assembly process. In other words the scope of the declaration was from the point of declaration to the end of the file. Many early assemblers were one-pass so symbol declarations had to preceed their usage. The SET is used to change the value of <symbol> to another constant expression with a scope from the point of declaration to the next SET or the end of file whichever comes first. A common use of SET is to generate unique labels inside a macro expansion. To create a label a constant string is concatenated with a numeric constant. Then the numeric constant is incremented so that it will have a different value on the next expansion. NOTE: these are assembly time variables, they do not consume any bytes in the processor's data space.

The syntax of CONSTANT and VARIABLE appears to be another permutation of <operator>,<operand1>,<operand2>. So it is an alternate syntax with the same effect as EQU and SET.

CBLOCK on the other hand is a completely different animal. The purpose is to implicitly define the addresses of a group(block) of symbols. In the early assemblers similar pseudo-operations were called BSS(Block Starting Symbol) or BES(Block Ending Symbol). The operand to the right of the pseudo-operation was a length in words. For example to declare an array "number" of 10 words and a "stack" of 60 words the following would be used:
Code:
number   BSS  10
stack    BES  60
The array number would occupy addresses 0 to 9, and the symbol number would evaluate to address 0. The stack would occupy addresses 10-69, and the symbol stack would evaluate to address 69. Later these symbols were replaced by the generic DS(Define Storage). The nature of the implicit definition of addresses is that the first item begins at some location and each successive item is mapped to the next available location. As CPU's evolved they had alignments restrictions which is a whole other discussion.

Hope this helps
 
Okay... So EQU and CONSTANT are just a permutation of operand and Operators... More of a relic from the past.

But why are the syntax named differently in the MPASM guide... One is called a symbol variable (or constant) and the other an assembler variable(or constant).

Is this also a synonym type of a thing?
 
Could you post the paragraph that is causing you the heartburn?

I see one difference between using SET and using VARIABLE. In VARIABLE no value is required.

CONSTANT and EQU appear to be behaviorly identical

CBLOCK has an implied connection with reserving register file addresses at absolute locations. It seems kind of silly for naming a group of sequential constants.

The RES directive and the IDATA and UDATA are also involved in allocating data space when there are multiple module and a linker involved.
 
Last edited:
Papabravo said:
Could you post the paragraph that is causing you the heartburn?
:D :D :D :D :D

Nice selection of words....

What is causing me the heartburn is the title.

set - Define an Assembler Variable
variable - Declare Symbol Variable

equ - Define an Assembler Constant
constant - Declare Symbol Constant

I was wondering.... surely the developers wont toil to throw in a new directive when the already is one to do the task...

Or is there something like backward compatability and depreciation.
I am able to perform arithmatics on both (SET and VARIABLE)
 
As near as I can tell "assembler" and "symbol" as adjectives modifying the nouns "constant" and "variable" are equivalent except for the case where you can say

Code:
  variable  symbol1
  variable  symbol2 = 9

In the first line there is no value given to symbol1. This would be equivalent to
Code:
symbol1  SET
symbol2  SET  9
The first SET above would probably produce an error. Why you might want to do that is analogous to the conditional compile feature of the C preprocessor which has the following construct
Code:
#if  defined(symbol1)
....<if_part>
#else
...<else_part>
#endif
In this usage it is not necessary for symbol1 to have a value, only that it be defined, which I interpret to mean that there is an entry in the assemblers symbol table.

To further investigate this situation I suggest you put together a "test" file which attempts to highlight these issues. I have found this to be a most educational way of understanding the behavior of complex pieces of software like assemblers and compilers. The program does not have to make sense or be executable, it just has to do both legal and illegal things to expose the limits of behavior. Let us know what you find.
 
Results of experiment.

Okay...

I did something of the sort
Code:
variable var1

IFDEF var1
    movlw 0x01
ENDIF
now the WREG did contain 0x01
The movlw 0x01 was skipped if var1 was not defined. Initialization was not needed.

Next I tried
Code:
variable var1
#undefine var1

IFDEF var1
    movlw 0x01
ENDIF
The #undefine did not have any effect.

I also tried this...
Code:
var1 SET 0

IFDEF var1
    movlw 0x01
ENDIF
movlw 0x01 is executed.
 
Last edited:
Bravo! That's the spirit of invention and creativity.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top