how to declare procedure [nasm] 
Author Message
 how to declare procedure [nasm]

How do you declare a procedure in nasm?
I'm saying this because back then when I switched from masm to nasm, the syntax
of declaring one was different, so I didn't bother to find out what it was so
since then, my procedures are:

label:
        push ebp                    ;or bp
        mov  ebp, esp             ;bp, sp
        code....
        code...
        leave
        ret

so what's the correct way (or is there one)?



Fri, 01 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]

Quote:

>How do you declare a procedure in nasm?

NASM has no concept of a procedure.

Quote:
>I'm saying this because back then when I switched from masm to nasm, the syntax
>of declaring one was different, so I didn't bother to find out what it was so
>since then, my procedures are:

>label:
>        push ebp                    ;or bp
>        mov  ebp, esp             ;bp, sp
>        code....
>        code...
>        leave
>        ret

This is fine (except that setting up EBP as a frame pointer is not
usually an efficient way of doing things, because you can just use
ESP).

Quote:
>so what's the correct way (or is there one)?

If you find yourself writing the same prologue and epilogue code
over and over again then you could write some macros (PROC and
ENDPROC, say) to do it.  In fact, there are some contained in
the NASM distribution: look for c16.mac and c32.mac in the MISC
directory.  Of course, you may want to change them to suit your
needs (e.g. the 32-bit one sets up EBP as a frame pointer, so I
would do something about that for a start...).

When I used to use TASM, the only real use for PROC that I ever
found was that it allowed the use of local labels.  But in NASM
you have these anyway, as long as all your code labels other
than procedure names begin with a dot.

S.

PS.  I only realised the true horror of MASM's PROC yesterday when
I read X-Calibre of Diamond's article "WndProc, The Dirty Way"
in issue 6 of the Assembly Programming Journal (which can be
downloaded from http://asmjournal.freeservers.com).



Fri, 01 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]

InfamousX01 a crit :

Quote:
> How do you declare a procedure in nasm?
> I'm saying this because back then when I switched from masm to nasm, the syntax
> of declaring one was different, so I didn't bother to find out what it was so
> since then, my procedures are:

> label:
>         push ebp                    ;or bp
>         mov  ebp, esp             ;bp, sp
>         code....
>         code...
>         leave
>         ret

> so what's the correct way (or is there one)?

From NASM doc:

"7.4.5 c16.mac: Helper Macros for the 16-bit C Interface
"
"Included in the NASM archives, in the misc directory, is a file c16.mac of
macros."
"It defines three macros: proc, arg and"
"endproc. These are intended to be used for C-style procedure definitions, and
they"
"automate a lot of the work involved in"
"keeping track of the calling convention."

I have had a look at it > easy to adpapt 32 bits (or go and see at the site if
a new one is already done).

Remark: It says "for ... .....  C Interface" >>>

If i understand well, the author has exactely the same position than me at
this point: There is absolutely no reason to to use such things INSIDE one
assembly app. The only result whould be to make it confusing and slower like
in MASM. You have regs. You have memory. Much enough.

Transmitting parameters in through the stack in a great feature BETWEEN PROCESSES.

bye. betov.



Fri, 01 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]
You could replace the first two lines with an equivalent: enter 0,0
They are both just fine, although I don't know which one is best size/speed wise.
If you look at my Win32 source codes, you'll always see me using this mnemonic.

Quote:

> How do you declare a procedure in nasm?
> I'm saying this because back then when I switched from masm to nasm, the syntax
> of declaring one was different, so I didn't bother to find out what it was so
> since then, my procedures are:

> label:
>         push ebp                    ;or bp
>         mov  ebp, esp             ;bp, sp
>         code....
>         code...
>         leave
>         ret

> so what's the correct way (or is there one)?

--

Team2k PC/Palm Pilot Programming Team:
http://ppilot.homepage.com

To email me, remove '3*&' from my email address. This is to deter spam :)



Fri, 01 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]

Quote:

>You could replace the first two lines with an equivalent: enter 0,0
>They are both just fine, although I don't know which one is best size/speed wise.

"enter 0,0" takes 4 bytes, compared to 3 for "push ebp" plus "mov ebp,esp".
It's also much slower, at least on Intel processors.

S.



Sat, 02 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]



Quote:
> You could replace the first two lines with an equivalent: enter 0,0
> They are both just fine, although I don't know which one is best
size/speed wise.
> If you look at my Win32 source codes, you'll always see me using this

mnemonic.

ENTER is (as far as I can tell) always slower than the equivalent
straight-line
x86 code.  It is an especially bad choice when the two operands are both
zero.
ENTER does make sense when building activation records for statically
nested procedures (as in Pascal) when you need to build a display to allow
access to non-local variables.  The instruction is shorter than the
straight-line
code that does the same thing but always slower [if Intel's cycle times are
to
be believed :-( ] than the corresponding straight-line code.  E.g., in the
HLA language,
I chose to emit the the straight-line code to generate displays rather than
use the ENTER instruction.

ENTER is probably easier to type that the corresonding instruction sequence.
This is a benefit if space and speed don't matter.  However, I'd recommend
using a macro (to build the activation record) if saving keystrokes is a
priority.
Randy Hyde



Sat, 02 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]

Quote:

> ENTER is (as far as I can tell) always slower than the equivalent
> straight-line
> x86 code.  It is an especially bad choice when the two operands are both
> zero.
> ENTER does make sense when building activation records for statically
> nested procedures (as in Pascal) when you need to build a display to allow
> access to non-local variables.  The instruction is shorter than the
> straight-line
> code that does the same thing but always slower [if Intel's cycle times are
> to
> be believed :-( ] than the corresponding straight-line code.  

I found ENTER one of the most difficult non-privileged x86 opcodes to
understand. It must be a typical CISC thing. Though LEAVE = MOV
eSP,eBP; POP eBP is just fine. And in 32-bit code fortunately you can
just use ESP to address the stack frame.

Bart



Sun, 03 Nov 2002 03:00:00 GMT  
 how to declare procedure [nasm]


Quote:

> > ENTER is (as far as I can tell) always slower than the equivalent
> > straight-line
> > x86 code.  It is an especially bad choice when the two operands are both
> > zero.
> > ENTER does make sense when building activation records for statically
> > nested procedures (as in Pascal) when you need to build a display to
allow
> > access to non-local variables.  The instruction is shorter than the
> > straight-line
> > code that does the same thing but always slower [if Intel's cycle times
are
> > to
> > be believed :-( ] than the corresponding straight-line code.

> I found ENTER one of the most difficult non-privileged x86 opcodes to
> understand. It must be a typical CISC thing. Though LEAVE = MOV
> eSP,eBP; POP eBP is just fine. And in 32-bit code fortunately you can
> just use ESP to address the stack frame.

> Bart

I understand what ENTER is supposed to do (I've taught compiler
construction in the past) and I still have trouble figuring out exactly
what this instruction does.  The description in Chapter 12 of Art of Asm
is slightly incorrect.  I discovered this while implementing the ENTER
semantics in straight-line code for HLA.

Of course, the big problem today with ENTER is that very few common
HLLs need the power it provides.  Since most assembly language
programmers never use statically nested procedures (a feature present
only in HLA as far as I know), the capabilities of ENTER are wasted
on the majority of programmers.

ENTER was added to the 286 (186?) instruction set specifically
to support Pascal.  It's useful for Ada, Modula-2, and related languages
as well, but given that the straight-line code is more efficient, I suspect
even these compilers don't emit the ENTER instruction if their optimization
is decent.
Randy Hyde



Sun, 03 Nov 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Declaring nested structure in NASM.

2. NASM: How to declare a far pointer?

3. How to declare program entry point with NASM?

4. how do i declare external procedures?

5. How to declare external procedures???

6. How to declare external procedure?

7. Nasm procedures

8. Procedure Syntax in NASM

9. Nasm procedure declaration

10. Procedures in NASM -How?

11. New official netwide assembler (NASM) mailing list -- NASM Advocate

12. NASM and nasm.sl for JED editor mode question

 

 
Powered by phpBB® Forum Software