Re: Assembler question #DEFINE or EQU?



On Sun, 3 Apr 2005 10:37:57 -0400, "Glenn Ashmore" <gashmore@xxxxxxx>
wrote:

>What is the difference? It looks like they do the same thing.

I'm not so sure I like Don's way of saying it, though it may be
correct, in part. Since you don't say which system you are working
on, I'll just assume you mean a comparison of a c-style #define and an
assembler style of EQU, when I speak, since that seems more likely to
me. (Sometimes, assemblers will actually support a c-style #define,
too, though I cannot think of a c-compiler that supports an assembler
style EQU.) Also, there is an aspect of #define that does support
parameters, but I'll assume that you are really just talking about
comparing:

#define MYSYM 10
and,
MYSYM EQU 10

rather than,

#define MYSYM(a) 10*(a)
and,
MYSYM EQU 10*(a)

which is a different comparison.

In an assembler like MASM for the PC, which supports an EQU, the EQU
actually creates a symbol (or may, anyway) and an associated value
that is placed into the object file that the linker can examine,
later. THis means that the symbol can be referenced in other source
code files.

In c (and probably any assembler also supporting it), the #define only
creates that symbol in the compiler's temporary internal memory for
the duration of the compilation. In that case, the symbol does NOT
then appear in the object file, at all. It's kind of like entering a
search-and-replace in your editor, that replaces all cases of the
symbol in your text to the indicated value before compilation. The
compiler never even "sees" the symbol. It's just not there, when the
c compiler gets around to parsing the code.

For example,

--- assembler source file #1 ---
SIZE EQU 10
PUBLIC SIZE
D DW SIZE DUP( 1 )
--- end ---

--- assembler source file #2 ---
EXTERN SIZE:ABS
mov cx, SIZE
--- end ---

If you assemble #1, it will not only use the value of SIZE in creating
the list of 10 constant-one's for the D-array, but it will also place
a symbol called SIZE into its object file for use by the linker. When
the assembler assembles #2, it places a reference in its object file
to link up with the value of SIZE to move into the CX register. When
the linker gets around to pasting these two files together, it finds
the value defined in #1 and replaces the reference to it in #2 with
the correct value and then completes the link up, which means that the
value 10 will be correctly placed into CX when the program runs.

In c, there is no equivalent to EQU.

However, in x86 assembler, there is an equivalent to #define. It's
simply the = sign. For example,

MYSYM = 10

In this case, the value in MYSYM only exists for the duration of the
assembly processes and the symbol MYSYM will not be placed into the
object file.

It's one of the many examples where the available semantics to the
assembly writer exceeds the semantics available to the c programmer.

Jon
.



Relevant Pages

  • Re: variable declration in 8051 assembly
    ... I used the metalink assembler to create an expansion to BASIC-52 and created constants and tables required in my program, as well as the tables, flags, and vector addresses required by the interpreter at the addresses specified in the MCS-51 BASIC Manual. ... The MAKE_TOKEN macro is defined below the code examples, and shows how to generate a basic "structure" in this assembler. ... SMALL_SCRN EQU 0; SHORTEN MOST DISPLAYS TO FIT ON 40 CHAR SCREENS ... REG < OR = VALUE ...
    (comp.arch.embedded)
  • Re: String copying throwing segmentation fault
    ... `.equ SYMBOL, EXPRESSION' ... movl $1, %eax ... The absolute section contains symbols which are not names of variables in our code and are not otherwise relative to the location in memory of our code, but which are instead absolute values and will not be relocated. ... Dump of assembler code for function _start: ...
    (comp.lang.asm.x86)
  • Re: Need LISA help
    ... keep in mind that there are assembler specific mnemonics that may ... Things like ASC, DS, DB, DW, EQU, ORG and such are assembler ... nlc cnd asl lsr rol ror dec ...
    (comp.sys.apple2)
  • Re: Assembly Console Programming in Windows
    ... > as a re-usable and sharable Assembler base, ... Gas was available back then. ... way GCC achieve OS object file independence. ... Writing an encoder is a lot of grunt work. ...
    (alt.lang.asm)
  • Re: I think i finally got a definition of an Assembler!
    ... definition - you can _never_ have an assembler for that system. ... production of an object file, ... A compiler that produces a object file from transalting the source ... I was refering as Randallīs HLA as he hole system...It does not ...
    (alt.lang.asm)