Assembler macros - looking for info on what this means

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

I'm going through the assembly code generated by my Codevision compiler, and I'm getting a bit stuck on the following. I've spent some time Googling and just can't find any explanations.

 

This is the macro, and a subsequent piece of ASM code that calls the macro:

 

.MACRO __PUTW1R
MOV  R@0,R30
MOV  R@1,R31
.ENDM

__PUTW1R 13,14

 

Am I correct in saying that "13" is parameter "@0", so R@0 is replaced with R13

And that "14" is parameter "@1", so R@1 is replaced with R14

 

effectively making the macro code:

 

MOV  R13,R30
MOV  R14,R31

 

If anyone knows of some teaching material where this is explained that would be great also.

 

Keith

Last Edited: Sat. Mar 18, 2017 - 05:05 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just found a snippet of info on this matter:

 

https://www.codeproject.com/Arti...

A macro can take up to 10 parameters referred to as @0-@9 and given as a coma delimited list.

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

The manual for Atmel's assembler is online.

 

EDIT: Google says http://www.atmel.com/webdoc/avra...

 

.macro explained here: http://www.atmel.com/webdoc/avra...

Last Edited: Sat. Mar 18, 2017 - 01:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Artandsparks wrote:
If anyone knows of some teaching material where this is explained that would be great also.

 

Why don't you use your own application?

 

-- You have seen the macro definition, right?  E.g.

                 	.MACRO __PUTW1RNS
                 	MOVW R26,R@0
                 	ADIW R26,@1
                 	ST   X+,R30
                 	ST   X,R31
                 	.ENDM
                 
                 	.MACRO __PUTD1RNS
                 	MOVW R26,R@0
                 	ADIW R26,@1
                 	CALL __PUTDP1
                 	.ENDM
                 
                 	.MACRO __PUTB1RON
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	SUBI R26,LOW(-@2)
                 	SBCI R27,HIGH(-@2)
                 	ST   X,R30
                 	.ENDM
                 
                 	.MACRO __PUTW1RON
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	SUBI R26,LOW(-@2)
                 	SBCI R27,HIGH(-@2)
                 	ST   X+,R30
                 	ST   X,R31
                 	.ENDM
                 
                 	.MACRO __PUTD1RON
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	SUBI R26,LOW(-@2)
                 	SBCI R27,HIGH(-@2)
                 	CALL __PUTDP1
                 	.ENDM
                 
                 	.MACRO __PUTB1RONS
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	ADIW R26,@2
                 	ST   X,R30
                 	.ENDM
                 
                 	.MACRO __PUTW1RONS
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	ADIW R26,@2
                 	ST   X+,R30
                 	ST   X,R31
                 	.ENDM
                 
                 	.MACRO __PUTD1RONS
                 	MOV  R26,R@0
                 	MOV  R27,R@1
                 	ADIW R26,@2
                 	CALL __PUTDP1
                 	.ENDM
                 

And in that same .ASM file where you see that, you also see

                	.LISTMAC
 

And from the Studio Help or other reference for the Atmel assembler we find

LISTMAC - Turn macro expansion on

The LISTMAC directive tells the Assembler that when a macro is called, the expansion of the macro is to be shown on the listfile generated by the Assembler. The default is that only the macro-call with parameters is shown in the listfile.

Syntax:

.LISTMAC

Example:

.MACRO MACX ; Define an example macro 
add r0,@0 ; Do something 
eor r1,@1 ; Do something 
.ENDMACRO ; End macro definition

.LISTMAC ; Enable macro expansion 

MACX r2,r1 ; Call macro, show expansion

And indeed in the .LST file we find e.g. 

                +
003183 01da     +MOVW R26 , R20
003184 9612     +ADIW R26 , 2
003185 93ed     +ST X + , R30
003186 93fc     +ST X , R31
                 	__PUTW1RNS 20,2

...with the expanded lines conveniently demarked.

 

All seems quite straight forward.  Which of the steps above caused mystery?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

clawson wrote:

The manual for Atmel's assembler is online.

 

EDIT: Google says http://www.atmel.com/webdoc/avra...

 

.macro explained here: http://www.atmel.com/webdoc/avra...

 

Thanks Cliff,

 

I came across that page yesterday but never realised .MACRO was an assembler directive (think I was getting mixed up with compiler directives), and thus never clicked on that link. I've only touched the surface with assembler so I'm still quite green with it all.

 

I did come across some AVR Assembler PDF user guides while Googling but they never mentioned how macro parameters worked.

 

Theusch,

 

I wanted an explanation of how parameters worked with .MACRO and of course did not expect to find any such tutorial material like that in the ASM or LIST files. Thus I didn't go looking there. I did check in the Codevision manual but it didn't offer any such explanation.

 

Keith

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

Artandsparks wrote:

Am I correct in saying that "13" is parameter "@0", so R@0 is replaced with R13

And that "14" is parameter "@1", so R@1 is replaced with R14

 

effectively making the macro code:

 

MOV  R13,R30
MOV  R14,R31

 

 

Yes, that's how it works. The documentation explains well that the arguments are @0, @1, etc. but it's not immediately obvious that you can use them even if they are not separated by whitespace.

 

For example, this compiles to nop.

	.macro nope
	n@0p
	.endm

	nope o

 

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

Has anyone mentioned this? http://www.atmel.com/images/doc2...

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Thanks very much lads, more great info.

 

This assembler interest is all to do with checking for atomicity of the compiled code. For example checking that my "additional" C code to make a code block atomic, does indeed result in the ASM instructions being placed where I expect them too. And going through the individual ASM instructions where a main variable is shared with an ISR, and imagining what could happen if the ISR kicked in at any point.

 

I always remember what Cliff said when he referred to single stepping through C code with "that silly yellow line". Not always representative of the real ASM instructions that are produced, and how the code flows. 

 

Keith.