next up previous contents index
Next: 3.10 Functions using private Up: 3. Using SDCC Previous: 3.8 Critical Functions   Contents   Index

3.9 Naked Functions

A special keyword may be associated with a function declaring it as _naked. The _naked function modifier attribute prevents the compiler from generating prologue and epilogue code for that function. This means that the user is entirely responsible for such things as saving any registers that may need to be preserved, selecting the proper register bank, generating the return instruction at the end, etc. Practically, this means that the contents of the function must be written in inline assembler. This is particularly useful for interrupt functions, which can have a large (and often unnecessary) prologue/epilogue. For example, compare the code generated by these two functions:

data unsigned char counter; 
void simpleInterrupt(void) interrupt 1 
{ 
    counter++; 
} 
 
void nakedInterrupt(void) interrupt 2 _naked 
{ 
    _asm 
      inc     _counter 
      reti    ; MUST explicitly include ret in _naked function. 
    _endasm; 
}

For an 8051 target, the generated simpleInterrupt looks like:

_simpleIterrupt: 
    push    acc 
    push    b 
    push    dpl 
    push    dph 
    push    psw 
    mov     psw,#0x00 
    inc     _counter 
    pop     psw 
    pop     dph 
    pop     dpl 
    pop     b 
    pop     acc 
    reti

whereas nakedInterrupt looks like:

_nakedInterrupt: 
    inc    _counter 
    reti   ; MUST explicitly include ret(i) in _naked function.

While there is nothing preventing you from writing C code inside a _naked function, there are many ways to shoot yourself in the foot doing this, and is is recommended that you stick to inline assembler.


next up previous contents index
Next: 3.10 Functions using private Up: 3. Using SDCC Previous: 3.8 Critical Functions   Contents   Index
Johan Knol
2001-07-13