News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Macros vs. Procedures vs. Functions

Started by Sergiu FUNIERU, February 27, 2010, 02:36:06 PM

Previous topic - Next topic

Sergiu FUNIERU

Quote from: qWord on February 26, 2010, 07:28:50 PMThere are two forms of macros: functions and procedures.
The first are called like: MacroName(arg1,arg2...)and the second: MacroName arg1,arg2....
I thought there are macros and functions (also called procedures).
So, there are 3 different kinds?

What are the differences between those 2 (3)? Why would anyone use one instead of the other (for instance, macros instead of functions)?

Can functions have an unspecified number of parameters, like a macro? If yes, how can I specify that in the prototype?

qWord

There are two forms of macros: functions, which returns something and procedures that doesn't do so.
Macros can only return literals using 'EXITM literal ', numeric constants must be converted using '%'. If a procedure use EXITM, it must be without argument. For detailed information about macros please read chapter 9 in MASM Programmer's Guide.

Here an macro that creates a string in .data section. Its using VARARG, so that the macro can have any number of arguments (in this example at least on is required):
; function like macro
@sz macro args:VARARG
LOCAL lbl
.data
    lbl db args,0
.code
EXITM <OFFSET lbl> ;return offset to string
endm

; procedure
sz macro LblName,args:VARARG
.data
    LblName db args,0
.code
EXITM ; not really needed
endm
...
sz szMyText,"text","text",10,13,"newline" ; create a string named szMyText
mov eax,@sz("text","text",10,13,"newline") ; create an anonym string an return offset


FPU in a trice: SmplMath
It's that simple!

tenkey

There are actually only macros and the other items (procedures, functions, subroutines, etc.).

When you create a macro, you are effectively creating a code template. The macro has no effect, not even generation of code, unless you use what you've created. Everywhere you use the macro, the specified code is generated in that place. The template property can be used to vary names, and other items you can't vary with procedures, etc.

Procedures, functions, and subroutines are the terms used by various high level languages to mean roughly the same thing. Code that is generated in one place. A compiler for a high-level language has the OPTION to treat these things as macros (this is an optimization known as "inlining", or what a Lisp/Scheme enthusiast would call "beta substitution").

Examples of terminology as used in other languages:

procedure - used in Algol (at one time there were tons of Algol derivatives), PL/I, and others for code that returns values or not
procedure - used in Pascal, and others for code that does not return values
subroutine - used in Fortran ("Sub" in VB) and others for code that does not return values
function - used in Pascal, Fortran, and others for code that always return values
function - used in C (and derivatives like C++, Perl, Javascript, PHP, etc.) and others for code that returns values or not

I've looked at code in all these languages, so I tend to use the above three terms interchangeably.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8