function's prologue/epilogue

Programming, for all ages and all languages.
Post Reply
User avatar
devel
Member
Member
Posts: 62
Joined: Wed Nov 28, 2007 4:15 am
Contact:

function's prologue/epilogue

Post by devel »

Hi there,

has anybody experiences with implementing prologue/epilogue of function in gcc?
For example to change ret instruction to lret.

regards & thnx in advance,
devel.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

The Good Thing (tm) is to use assembler to wrap up GCC functions for interrupts.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
mathematician
Member
Member
Posts: 437
Joined: Fri Dec 15, 2006 5:26 pm
Location: Church Stretton Uk

Post by mathematician »

push ds
push es
push fs
push gs
push ebp
push eax
push ebx
push ecx
push edx
push esi
push edi

call _func

;next three lines for hardware interrupts only
; - assuming you're not using APIC

mov al, 20h
out 20h, al
out 0a0h, al
pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
pop ebp
pop gs
pop fs
pop es
pop ds
iret

adjust as necessary for 64 bit mode
User avatar
devel
Member
Member
Posts: 62
Joined: Wed Nov 28, 2007 4:15 am
Contact:

Post by devel »

Sorry for misunderstanding. I meant whether it is possible to override gcc's default
prolog/epilogue code? Is there any macro in gcc for handling this?

regards,
devel.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

No, it can't be done.

At least, when I was looking for a way to do it, I didn't find any.
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

I don't know much C but I remember with Visual Studio, I could create naked functions in which I got to write my own prologue and epilogue in pure assembly:

Code: Select all

int __declspec(naked) __stdcall Add (int a, int b){
  __asm{
    PUSH    EBP
    MOV     EBP , ESP
    MOV     EAX , DWORD PTR [EBP + 0x08]
    ADD     EAX , DWORD PTR [EBP + 0x0C]
    POP     EBP
    RET     0x08
  }
}
I don't know but maybe there is a way around it also in GCC.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
blound
Member
Member
Posts: 70
Joined: Sat Dec 01, 2007 1:36 pm

Post by blound »

XCHG wrote:I don't know much C but I remember with Visual Studio, I could create naked functions in which I got to write my own prologue and epilogue in pure assembly:

Code: Select all

int __declspec(naked) __stdcall Add (int a, int b){
  __asm{
    PUSH    EBP
    MOV     EBP , ESP
    MOV     EAX , DWORD PTR [EBP + 0x08]
    ADD     EAX , DWORD PTR [EBP + 0x0C]
    POP     EBP
    RET     0x08
  }
}
I don't know but maybe there is a way around it also in GCC.
From needing "naked" supoprt on another project, gcc only supports naked on some arches ( x86 not being one of them ), I got this from trying it myself, and a partner on the project reading the mailing lists and seeing one of the gcc devs saying "who would need this on x86..." - so it never got implemented
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

I don't see a use to this... considering in such a "naked" function, you couldn't actually "have" any C code, (The compiler couldn't automatically determine the convention you're using.. right?)

So what's the point? Why not just write the entire function in asm?

Just my opinion... :wink:
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply