On Mon, 2 Jun 1997 01:15:17 +0000 John Payson writes: >Unfortunately, no PIC (not even the 17Cxx) can at present handle the >general case of cooperative multi-tasking, where either/any process >may >execute a yield() from within nested procedures. The normal method >for >handling such task switching is to split the stack area into two or >more >pieces; on the 8x51, a task switch (assuming two tasks) would then be >performed as: > >Yield: > mov a,other_sp > xch a,sp > mov other_sp,a > ret > > >Calling Yield() will place the address following the call onto the >stack, >save the stack pointer and point it to the other stack, and then work >on >the other task. When that task calls Yield() the stack pointer will >be >restored with its old value, so RETurning will resume execution of the >former task. Simple and very efficient. I did this years ago in Turbo Pascal (though the task switch code was written in assembly). Worked out quite nicely. I called my procedure NextTask, and it did a stack switch. Besides holding return addresses, the stack also held local variables for the task. I generally called NextTask while in a loop waiting for I/O. I also called NextTask in computation intensive tasks that just took a long time. I'd just throw a NextTask in there now and then so it wouldn't hog the system. I think we had five or six tasks running. Harold