delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/06/05:04:25

From: Shawn Hargreaves <Shawn AT talula DOT demon DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Calling assembly procedures from DJGPP
Date: Tue, 5 Aug 1997 20:27:27 +0100
Organization: None
Distribution: world
Message-ID: <qSzpQDAf635zEwJ3@talula.demon.co.uk>
References: <199708050149 DOT LAA20334 AT wumpus DOT its DOT uow DOT edu DOT au>
NNTP-Posting-Host: talula.demon.co.uk
MIME-Version: 1.0
Lines: 74
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Brett Leslie Porter writes:
>OK, I've just looked through the FAQ and can't find anything to help me. 
>I know it's probably in INFO, but I can't seem to find it.
>
>I'm looking for information on how to set up a C procedure in an .S file, 
>how to get parameters off the stack, how to return a value, how to export 
>it so C files can call it, all that sort of assembly stuff.

I don't know that this is documented anywhere, but with the help of the
'-S' switch to gcc, it's easy to work out for yourself. Write a little C
routine:

    int x, y;

    void paramtest(int param1, int param2)
    {
        x = param1;
        y = param2;
    }

Compile with -O3 -m486 -fomit-frame-pointer -S, and you get the output:

            .align 4
    .globl _paramtest
    _paramtest:
            movl 4(%esp),%eax
            movl 8(%esp),%edx
            movl %eax,_x
            movl %edx,_y
            ret

So the first parameter is in 4(%esp), the next in 8(%esp), etc. If you
want to pass types other than integer (or should I say other than 32 bit
values, since all dword data is passed in the same way), write a similar
little test and see what gcc does with them.

To see how the return value works, try a test file:

    int x;

    int returntest(void)
    {
        return x;
    }

This produces:

            .align 4
    .globl _returntest
    _returntest:
            movl _x,%eax
            ret

So all you have to do is load the return value into %eax! 

>I've NEVER used an assembler to link to C before (I pretty much always 
>use inline assembly, but with DJGPP it makes your code look kinda messy 
>with all the "clobbered" registers, etc. I'm not having a go at the 
>technique: it is better than any other inline assembly I've ever seen in 
>a C compiler, it just messes up my prettily formatted code for biggish 
>functions).

Absolutely. I love the gcc inline asm syntex (sure, it's more
complicated than with most other compilers, but it is also more
efficient, and if you are using asm in the first place, you are probably
more concerned about performance than ease of coding). But for large,
self-contained functions, it's much simpler to write them in separate
asm files. especially if you run them through the C preprocessor to get
#include and #define processing for macros and constants...


--
Shawn Hargreaves - shawn AT talula DOT demon DOT co DOT uk - http://www.talula.demon.co.uk/
Beauty is a French phonetic corruption of a short cloth neck ornament.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019