I'm currently writing my own operating system. It is a non-preemptive OS and my threads are working fine with the scheduler. I have one edge case though. If I don't call my yield()
function from C but do asm("call yield")
the calculations later on fails but only with -O2
optimization.
Like many have said, it is always our fault
I have tried all approaches i can think of but now I'm desperate.
So if anybody has some tips on what might be happening or what i should investigate please share.
I guess this gets shutdown for being offtopic but any tips is greatly appreciated.
When the compiler generates a call to a function, it preserves the contents of any registers which may be modified by the called function ("caller-save registers") before making the call.
Since you've buried this function call within an inline assembler block, the compiler doesn't know it needs to save and restore registers around the call.
Simple solution: Don't do that, then. If you want to call a function, use C function call syntax.
Complicated solution: Declare which registers this function call will clobber using extended inline assembler syntax.