Routine or a Macro

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

I would like to ask about the diferencies between a routine and a macro since the same think can be done with both of them and a macro seems more easier with the passed parameters.

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

Using static inline functions allows you to benefit from C's (limited!) type checking. A macro won't do this.

Avoid macros where-ever possible - you know it makes sense. In grown up languages such as C++ and especially Java (which doesn't have a preprocessor) they try to wean you off using the preprocessor "crutch" and do things programatically.

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

Can a C function be called a routine? Are we talking about assembler or C or...?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:

Can a C function be called a routine?

Ponder this. What does the R in ISR stand for?

So, yes, I think "routine" can be used to describe a C function ;-)

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

My question is about the assembly macros.

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

Quote:

My question is about the assembly macros.

Bugger - I think I see a hairline crack developing in my crystal ball!

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

Don't worry, your crystal ball can be fixed seamlessly - just review the OP's messages and see which language he's working with :lol:

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

clawson wrote:
Quote:

My question is about the assembly macros.

Bugger - I think I see a hairline crack developing in my crystal ball!

Sell it to js :lol:

He's allready "fluent in asm" , and doesn't need that function 8)

/Bingo

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

Cliff and I disagree.

IMHO, if a macro can make your program more readable and/or reduce silly mistakes, use them.

For example, a 16-bit subtraction or comparison macro means you will never have to worry about getting the carry wrong.

Remember that if your macro is wrong, the whole world collapses. So you write once (carefully) then use many times.

Note that macro syntax is likely to alter between different assemblers. All the same, it is not difficult to learn. The GNU avr-as is far more powerful than the Atmel assembler.

Most importantly, the avr-as macro is quite 'normal'. It has no connection with the C compiler's 'inline-gobbledygook'.

If you want some ideas for 'useful' macros, look at any CodeVision listing. Atmel has a useful macro app-note.

David.

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

Quote:

Cliff and I disagree.

Except it's now established that we are talking about Asm not C. Given the copious amount of typing required in Asm to do even a small amount of sensible work then I'm all in favour of Asm macros that localise the repeated typos to one location. Presumably you are using Asm as you really are concerned about the overhead that a CALL/RET sequence to a common routine would add?

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

Quote:
IMHO, if a macro can make your program more readable and/or reduce silly mistakes, use them.
Since for C macros tend to reduce readability and are far more likely to create silly mistakes, you then in fact, agree with Cliff's stance on macros in C.

Regards,
Steve A.

The Board helps those that help themselves.

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

A C war, great. :evil:

For me:
routine\subroutine = ASM or even BASIC
function = C or Pascal (if it returns a value)
procedure = Pascal

Don't know why an ISR is called that in C, a routine. Maybe because it should be written in ASM?? :lol:

Macros in ASM as for example those in AVR001 (+ few others I have) make sense, otherwise it seems to obscure the elegance and sheer beauty of ASM code. :mrgreen:

And Cliff if I were you I would sue the plastic surgeon for the crack in your crystal ball, they should have implanted a more suitable material....

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I think Von Neumann invented the Subroutine in the 40s. So I claim that the words subroutine and procedure are synonyms. Regardless of language, they end in a RET and are invoked with a CALL. We can include functions with no return values.

Imagecraft compiler user

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

Routine, function, procedure, method: all pretty much the same.

Regards,
Steve A.

The Board helps those that help themselves.

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

Cool. Me and the Lib agree on something. Is this a Great Country or what?

Imagecraft compiler user

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

geoelec wrote:
I would like to ask about the diferencies between a routine and a macro since the same think can be done with both of them and a macro seems more easier with the passed parameters.
Macros: bad. Incomprehensible 6 months later.
Inline functions: good.

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

Koshchi wrote:
Routine, function, procedure, method: all pretty much the same.
functions always return a value. Subroutines and procedures may or may not, depending on the language.

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

stevech wrote:
functions always return a value.

Does this function return any value?
void toggleport(void){
 PORTB = ~PORTB;
 }

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

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

no, not in the context of "function". Affects on global variables in RAM or I/O space, is a different topic.

In C, a function of type void in my opinion, is equivalent to a subroutine in other languages.

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

Everyone has their own style and opinions.

I started in (non-AVR) assembly language. Every subroutine would take parameters and return values.

I found the concept of void functions in C rather strange. In fact void was not part of the earliest C. The default function in C would always return a value.

Young people like the idea of everything being void. This ensures that you never know what has gone wrong (or right).

Regarding terminology. Surely function, subroutine, method, ... are all much the same. Some words may imply a bit more than others, but not to the extent of exclusivity. e.g.
function implies a return value.
method implies an owner.
subroutine implies King Samperi.

Any of these terms can have side-effects.
The same applies to macros.

David.

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

For example,this part of code blinks 3 leds,depending the value of seconds and the value of leds.

minutes 300,m5;//this call the macro

.MACRO minutes
clr delay_low
clr delay_high
ldi r16,1<<0
_m01:
clr delay
min0101:
sbr r16,@1
out DDRB,r16
cpi delay,50 
brne min0101
clr delay
min0102:
cbr r16,@1 
out DDRB,r16
cpi delay,50
brne min0102
inc delay_low
brne compare010
inc delay_high
compare010:
cpi delay_low,low(@0)
brne _m01
cpi delay_high,high(@0);////////600
brne _m01
cbi PORTB,0
.ENDMACRO

Seems more compact and more easy than to have to load the registers with different values before any relative call each time and seems a lot like a C function.
But you have to know for sure that a macro realy works,since it has no debugging options.

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

Quote:
But you have to know for sure that a macro realy works,since it has no debugging options.
What do you mean? If the macro is expanded then it's just another bit of debuggable code .

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

How a macro can be expanded for debugging.I think it's more easy to use,but also can cause troubles.

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

Quote:
How a macro can be expanded for debugging
.listmac
;Original Atmel macros
;======================
.listmac

.MACRO SETB 		;Arguments: Address, Bit, Register
	.if @1>7
		.message "Only values 0-7 allowed for Bit parameter"
	.endif
	.if @0>0x3F
		lds  @2, @0
		sbr  @2, (1<<@1)
		sts  @0, @2
	.elif @0>0x1F
		in   @2, @0
		sbr  @2, (1<<@1)
		out  @0, @2
	.else
		sbi  @0, @1
	.endif
.ENDMACRO

all a macro is doing is insert the code in it's definition at the point where it is located. .listmac just shows you ALL the inserted code.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly