ASM .macro syntax sugar, passing XL:XH and X

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

I honestly wasn't sure where to put this. I use Studio 4 because it's not as "chunky" as 6 but I use 6 for those chips that don't appear under 4 (such as the T828).

It's really an ASM question so if this is in the wrong forum, let me know. :)

So here goes, I have a macro that uses instructions like MOVW where the preferred format is:

MOVW Rd+1:Rd,Rr+1:Rr

The other instruction makes use of the single letter notation, such as:

ST Y, Rr

Right now, my macros look something like:

RandomMacro XH, XL, X

Which is kind of clunky IMHO. Is there a trick to just doing something like:

RandomMacro X

and automagically know that whenever I want XH:XL, I can derive that from the X passed in? Or the inverse where I can derive X from the fact I passed in XH and XL to the macro?

It's not mission critical or anything. I just want to tighten up the code.

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

You didn't actually say which assembler you are using. If it is avr-as then you have access to the full C pre-processor so you can use things like concatenation which would allow you to take just "X" as the input and concatenate H or L onto it where required.

If it's the Atmel assembler then there is a pre-processor but it only implements a sub-set of the C pre-pro.

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

I have every sympathy with you. Making your code readable and easy to maintain is important.

I am not an ASM programmer. However the Atmel Assembler uses two forms of macros:
1. regular C preprocessor
2. special assembler macros

So you can do things like paste two strings with ##
Note that you need to do this in two stages if you are pasting parameters.

Cliff and I disagree strongly on macros.

Cliff thinks they introduce obfuscation, and possible sources of error.

I think they are useful if they ultimately make life better. However this means you should write them VERY carefully and debug thoroughly.

DEscribe in English what you want your macro to do.
Someone will probably show you a solution.

Oh, I don't mind how clunky the macro definition looks. Just as long as the invocation is clear and simple to use.

David.

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

SavannahLion wrote:
I honestly wasn't sure where to put this. I use Studio 4 because it's not as "chunky" as 6 but I use 6 for those chips that don't appear under 4 (such as the T828).

It's really an ASM question so if this is in the wrong forum, let me know. :)

So here goes, I have a macro that uses instructions like MOVW where the preferred format is:

MOVW Rd+1:Rd,Rr+1:Rr

The other instruction makes use of the single letter notation, such as:

ST Y, Rr


Well, the assemhler allows you to do

    movw x, y
    movw z, r1:r0

, etc directly. Depending on how your macro looks, that may already be the solution for your problem.

Quote:
Right now, my macros look something like:

RandomMacro XH, XL, X

Which is kind of clunky IMHO. Is there a trick to just doing something like:

RandomMacro X

and automagically know that whenever I want XH:XL, I can derive that from the X passed in? Or the inverse where I can derive X from the fact I passed in XH and XL to the macro?

It's not mission critical or anything. I just want to tighten up the code.


If you invoked the macro like this

    foo x, y

You can write the macro like this, if you insist

.macro foo
    movw @0H:@0L, @1H:@1L
.endm

but given that the assembler accepts x and y directly I don't see why you want to do that.

- roland

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

And a few more macros

;Push temp registers (temp and temp1) register on stack
;
.MACRO PUSH_temps	;Arguments: nil
		push	r24
		push	r25
.ENDMACRO

;Push X register on stack
;
.MACRO PUSH_X 		;Arguments: nil
		push	xh
		push	xl
.ENDMACRO

;Push Y register on stack
;
.MACRO PUSH_Y 		;Arguments: nil
		push	yh
		push	yl
.ENDMACRO

;Push Z register on stack
;
.MACRO PUSH_Z 		;Arguments: nil
		push	zh
		push	zl
.ENDMACRO


;Pop temp registers (temp and temp1)  from stack
;
.MACRO POP_temps	;Arguments: nil
		pop		r25
		pop		r24
.ENDMACRO

;Pop X register from stack
;
.MACRO POP_X 		;Arguments: nil
		pop		xl
		pop		xh
.ENDMACRO

;Pop Y register from stack
;
.MACRO POP_Y 		;Arguments: nil
		pop		yl
		pop		yh
.ENDMACRO

;Pop Z register from stack
;
.MACRO POP_Z 		;Arguments: nil
		pop		zl
		pop		zh
.ENDMACRO

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Great thanks, those were the answers I was looking for.