EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Macro VS Subroutine

Started by Devon Lee March 8, 2005
Hi there,

Do you guys mind to briefly explain the difference
between Macro and Subroutine? At what condition, it is
a must to use Macro or at what condition it is desired
to use Macro?

Thanks in advance.
__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/



Hello,
To do quick:

Macro are remplaced at compile time. This means each time you call a macro
in your code, the compiler will "copy/paste" all the lines of the macro in
the code.

A subroutine is different in that, it's written once and it occupies memory
code once, you just pass arguments and retrieve results at run time.

In both case the purpose is to shorten the code size when you write the
program.
But the result is very different, after compiling, in term of code space
occupation.

Hope this helped !

Vince
-----Original Message-----
From: Devon Lee [mailto:]
Sent: mardi 8 mars 2005 16:04
To:
Subject: [piclist] Macro VS Subroutine
Hi there,

Do you guys mind to briefly explain the difference
between Macro and Subroutine? At what condition, it is
a must to use Macro or at what condition it is desired
to use Macro?

Thanks in advance.
__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/ to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

Yahoo! Groups Links


macros put the code "inline". Faster than a jump to a
subroutine, but if you use it several times in your
code, you eat up code space. You have to judge the
tradeoffs. --- Devon Lee <> wrote:
> Hi there,
>
> Do you guys mind to briefly explain the difference
> between Macro and Subroutine? At what condition, it
> is
> a must to use Macro or at what condition it is
> desired
> to use Macro?
>
> Thanks in advance. >
> __________________________________
> Celebrate Yahoo!'s 10th Birthday!
> Yahoo! Netrospective: 100 Moments of the Web
> http://birthday.yahoo.com/netrospective/
>

__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/





A MACRO equates a line or lines of code to a simple phrase or even
word. Macros can make it faster and easier to write code that is
repeated several times in a program.

The main problem with MACROS as I see it is, they hide what the code
is doing and the size of the code from the programmer. This can be
bad if you are trying to write tight code for overall program size
purposes because at a glance you may not remember what the MACRO
consisted of. A macro by its nature would be used several times
throughout the program as a short form of writing that same piece of
code.

A subroutine is typically a CALLABLE routine that is used by the
program in many areas. You would write it once and call it wherever
you need it.

Personally when in a team programming envronment I would ask that the
team not use MACROS, but your mileage may vary.
--- In , Devon Lee <devonsc@y...> wrote:
> Hi there,
>
> Do you guys mind to briefly explain the difference
> between Macro and Subroutine? At what condition, it is
> a must to use Macro or at what condition it is desired
> to use Macro?
>
> Thanks in advance.
>




Sorry for my late reply. Thanks for the explanation
regarding the Macos and the Subroutine. Thanks a lot.
__________________________________




--- In , allenbv1 <no_reply@y...> wrote:
>
>
> A MACRO equates a line or lines of code to a simple phrase or even
> word. Macros can make it faster and easier to write code that is
> repeated several times in a program.
>
> The main problem with MACROS as I see it is, they hide what the
code
> is doing and the size of the code from the programmer. This can
be
> bad if you are trying to write tight code for overall program size
> purposes because at a glance you may not remember what the MACRO
> consisted of. A macro by its nature would be used several times
> throughout the program as a short form of writing that same piece
of
> code.
>
> A subroutine is typically a CALLABLE routine that is used by the
> program in many areas. You would write it once and call it
wherever
> you need it.
>
> Personally when in a team programming envronment I would ask that
the
> team not use MACROS, but your mileage may vary.

There are processors where the call stack is tiny - like 2 levels.
Whether this makes macros more desireable is an interesting point.

However, using macros to hide the vagaries of hardware is my
favorite use. I remember many years ago developing hardware to
interface a DAT tape drive to an 8085. Someone else was supposed to
do the application code but they kept complaining that the hardware
didn't function.

I wrote a set of macros to handle the tape drive and the interrupt
system. Then the discussions re: did the hardware work were cut
short. Here's the library, get on with the application!

I have also used macros to create complex data structures. One was
a huge lighting control project covering maybe 400k sq ft of office
space. The data structure was all you might expect from having
multiple buildings, multiple lighting panels of different capacities
per building, circuits arranged in odd ways on the panels and a
desired result of creating blocks somewhat related to occupancy and
schedule for turn-off according to user needs.

I am a big fan of macros and have been since I first created pseudo
instructions for doing boolean algebra on an IBM 1130 back in 1970.

My favorite guide for macros comes from the CP/M world:
http://www.cpm.z80.de/manuals/mac.pdf There are a number of
hardware hiding macros described. > --- In , Devon Lee <devonsc@y...> wrote:
> > Hi there,
> >
> > Do you guys mind to briefly explain the difference
> > between Macro and Subroutine? At what condition, it is
> > a must to use Macro or at what condition it is desired
> > to use Macro?
> >
> > Thanks in advance.
> >




Memfault Beyond the Launch