This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

About function call

Is there any "naked" attribute function for Keil C?

This is because I would like to have complete control over the push/pop of the registers during function call.
If not, is that necessary for me to write the code in pure Asm? And is that possible to call the C macros inside the ASM?


Thank you~

  • "This is because I would like to have complete control over the push/pop of the registers during function call.
    If not, is that necessary for me to write the code in pure Asm?"

    If you want complete control of anything non-trivial at an assembler level you will have to write it in assembler.

    "And is that possible to call the C macros inside the ASM?"

    I'm not entirely sure what you mean by 'C macros' in this context but again, if you want complete control you'll have to use assembler.

  • I wanna do so as the if I defined some macro in C, it seems that it is simpler than using pure asm for implementation. I just don't wanna do any function call which would "pollute" the stack. I would like the stack for my context switching task. Is that using attribute _task_ can make the function call without any push/pop for the stack? thank you!

  • "I wanna do so as the if I defined some macro in C, it seems that it is simpler than using pure asm for implementation. I just don't wanna do any function call which would "pollute" the stack. I would like the stack for my context switching task. Is that using attribute _task_ can make the function call without any push/pop for the stack? thank you!"

    To be honest, I haven't a clue what you're on about.

  • A C function call will, at minimum, push the return address on the hardware stack. Keil C typically passes parameters in registers. When it cannot, the information spills over into memory, not the stack. See the sections of the manual on overlay processing. Routines declared "reentrant" use a "software" stack for parameters and locals. The 8051 hardware stack is used only for return addresses.

    Some library calls may temporarily use some stack space.

    To eliminate even the return address on the stack, you cannot use a function call, just as you could not use A/LCALL and RET in assembler. In this case, you would have to settle for inlining all your functions. Keil C does not have an inline extension, so you would have to use the preprocessor (#define macros) to define the code.

    I would be surprised if you could write any reasonably complicated system that used literally no stack at all, except for the one privileged context switch task you mention. If you could write such a program, I'd expect it would be simple enough not to need a context switcher in the first place.

    From the documentation, I've gotten the impression that Keil's RTX deals with stack usage by moving data up and down in the idata space to compact the stack and leave the "hole" free for the currently in-context task. Another solution would be to copy the used stack into your task control block. This will increase context switch times considerably, particularly if you have to use xdata to hold the stacks, but would allow tasks to have larger stacks than having them all trying to share the 128 bytes of idata.

  • Thanks a lot!
    Yet I wanna make sure one thing is that the use of macro. I want to know if that is possible to define some C macro so that it would be possible to be called by the asm code.

    As I would like to have complete control for context switching (in fact I need to do so if I try to implement a RTOS not using RTX). I would expect that I need to define all "NAKED" functions in ASM code. Right?

    if I can use C macro in Asm in Keil(better if I can make those "NAKED" function in C51), this could makes the life much easiler.

  • Pardon my lack of knowledge of 'C', but what is a 'NAKED' function?
    Bradford

  • I think the OP is using the term "naked function" to mean the block of code that is the body of the function, minus the call and return sequence, the prolog and epilog.

    C preprocessor macros do not exist as a separate piece of object code. The text is inlined into the source at the point where the macro is used. The compiler then sees that complete text and compiles the code. There is, for example, no single "entry point" for a macro. It's a macro, not a subroutine.

    An assembler could not make use of a C macro, in C syntax, unless it could similarly expand the macro according to the preprocessor rules, and also compile the C syntax.

    If you want to use a macro in assembler, use the assembler syntax for defining a macro. If you want to call a subroutine, you could write in either in C or assembler, but either way, you're going to have a CALL / RET sequence, whether generated by you or the compiler.

  • This is because I would like to have complete control over the push/pop of the registers during function call.
    ..
    I would like the stack for my context switching task.


    The '51 is not a "mini PC", variables do not go on the stack and the chip is singularily not suited for task switching.

    Erik