Macro Assembler Clarifications

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Platform is AVR Studio 4.

When writing in an assembler program, I set out to write macro procedures & call it & pass a variable(s), there are times I use the @0 up to @9.

Questions
1) Where does it store the variable say @0 or @5 ?
My guess is somewhere in the stack area or some predefined registers been allocated.

2) How can I step or trace into the macro using F10 or is there another way of doing it. ?

3) If I pass a 4Bytes (2^32) number to the macro, how can this be handle ?

Ken

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But macros are just (at the end of the day) a string substitution process (like #defines in C) the @0 to @5 are not "stored" anywhere, the assmbler first runs a pass where it expands out the macros to the underlying "long version" of the code that it implies then it assembles that so YOU are in control of where @0 to @5 are "stored"

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The "C" code part in the *.lst, that I understand.
This does shows up when I use the .LISTMAC & veiwing where the "+" C code reside.

Then how can I be in control to where the @0 to @5 be stored ?
Must be missing something very simple.

Ken

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yep, I'm afraid it is a simple thing...

In AVR Studio/ASM the macros are just used during the Assemble activity to expand the macro you have in the source with the macro definition, including resolving the @ arguments, that you have made.

So the @ arguments are never stored anywhere.

Have a look at the Macros section (under AVR Assembler) on www.avrbeginners.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Before the assembler actually uses the source for generating code macros are expanded. In this respect you can look at macro expansion as an assembler version of the "C" preprocessor.

So one "explanation" would be to say that an assembler macros are treated in somewhat the same way as a "C" preprocessor #define. In an early "pass" the assembler (or the "C" preprocessor) expands macros, literally insering their translation into the code, the result still being an assembler source readable by humans (or a "C" source readable by humans). It is this source that is actually ssembled (compiled) into machine code.

So, just as

#define sum(t1,t2,theSum) theSum = t1 + t2

will not generate a chunk of code that you can uniquely identify at one certain place in the machine code, neither will

.macro sum
   lds @1, r16
   lds @2, r17
   add r16, r17
   sts @3, r16
.endmacro

if you do this in your "C" source

sum(a,b,c);
sum(d,e,f);

what the compiler will actually se (after the preprocessor has actually done its job) is

c = a + b;
f = d + e;

OK?

Now, if you would do the following in assembler

   sum a,b,c
   sum d,e,f

what the assembler's parser and code generator would actually see (after macro expansion has taken place) would be

   lds a, r16
   lds b, r17
   add r16, r17
   sts c, r16
   lds d, r16
   lds e, r17
   add r16, r17
   sts f, r16

So, the question "Where is e.g. @1 stored?" can't be answered unless you qualify it with "for the first macro call/expansion", and then the answer would be "Where you told it to" i.e. in a.

Did all this make any sense?

[Please note that all code above is out of my head, directly into the forum without testing with any compiler or assembler so there might be typos, or even blunt errors. I hope that I have made myself understadable though...]

[Assembler conosseurs: Yes, I know that I am using the term "pass" above in a sloppy, or even erroneous, way.]

Also:
Please note that you can tell the Atmel AVR Assembler to produce a list file, somewhat akin to the one that e.g. avr-gcc creates. Setting this option, writing a small experiment assembler source, and inspecting the list file might give some clues as to what is happending. You'll have to be able to read machine code in hex, though...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Thanks for your explanations from Cliff MartinM57 & JohanEkdahl.
Had another reread from the "www.avrbeginners.net".
Also your analogy is making it much clearer, JohanEkdahl.
Now it is making sense to what is going on.

Appreciated your time.

Ken