Mail Archives: djgpp/1997/10/18/16:02:28
At 06:46 10/13/1997 GMT, George Foot wrote:
>On Sat, 11 Oct 1997 23:51:37 GMT in comp.os.msdos.djgpp Nate Eldredge
>(eldredge AT ap DOT net) wrote:
>
>: SET DJGPP=c:/langdev
> ^^^^^^^^^^
>Don't you mean `c:/langdev/djgpp.env'?
I did. Sorry.
Nate Eldredge
eldredge AT ap DOT net
o stick to Intel sintax using NASM to compile external asm
>functions to COFF format.
> The only problem is that I've never used external asm functions, so I beg
>you for help.
> This is a sample function that comes with NASM in cofftest.asm:
>
>GLOBAL _lrotate
>
>_lrotate:
> push ebp
> mov ebp,esp
> mov eax,[ebp+8]
> mov ecx,[ebp+12]
>.label rol eax,1
> loop .label
> mov esp,ebp
> pop ebp
> ret
>
> These are the declarations in the sample cofftest.c:
>
>extern int lrotate(long, int);
>
> These are my questions *g*
>
>Do I need to declare _lrotate as global in order to have access to it in my
>C code?
Yes. Otherwise only the source file it's in can know about it.
>If I got it right we need to jump ebp by 8 due to:
> The size of ebp itself (4)
> The size of the pushed previous ebp (4)
>Is this right or did i confuse something?
No. The way gcc calls a function is like this:
push RIGHTMOST_ARG
...
push LEFTMOST_ARG
call FUNCTION ; this is a near call
It is traditional to use ebp as the frame pointer, so the previous ebp is
pushed and ebp gets the value of esp. Now the stack looks like this (each
line = 1 dword, 4 bytes):
[RIGHTMOST_ARG]
...
[LEFTMOST_ARG]
[Caller's eip pushed by call]
[Caller's ebp] <= EBP and ESP point here
[any local variables...]
So the 8 bytes is 4 bytes of the pushed ebp, and 4 bytes of return address.
The previous ebp is stored way up above somewhere.
>Since the second value is only a int why is it moved to ecx and not cx?
>Won't this store junk at the high order bytes of ecx?
On DJGPP ints are 32 bits.
>Why are esp and ebp swaped? Are the parameters stored based to esp and not
>ebp?
As you can see by my diagram above, they both point at the same place. On
the 8086, you could not address relative to sp, the stack pointer. On the
386 you can. Therefore, you could dispense with pushing ebp altogether and
have your args start at [esp+4]. The downside is that some debugging stuff
will not be able to give tracebacks.
>Where is the value returned? Eax?
Yes. 64-bit integers (long long) are returned in edx:eax.
>How do i link the 2 files (cofftest.asm and cofftest.c)? Is this done by a
>special parameter to gcc (if so, any way of doing this straight from
>RHIDE?) or do the files have to have the same name?
No. Actually, it might avoid some confusion if they had different names.
Basically, you compile or assemble both to .o files:
nasm -f coff asmfile.asm
gcc -c cfile.c
And then you link them:
gcc -o myprog asmfile.o cfile.o
I think nasm takes a shortcut to avoid a name collision:
nasm -f coff cofftest.asm
gcc -o cofftest cofftest.o cofftest.c
Not sure how to do this from RHIDE but you should be able to figure it out.
Hope this helps.
Nate Eldredge
eldredge AT ap DOT net
- Raw text -