Page 1 of 1

mutlitasking in real mode

Posted: Mon Jul 22, 2002 6:54 am
by drizzt
I've a very big problem about multitasking in real mode, that makes me crazy!!
I would realize a little program to simulate a simple round robin scheduler using system clock interrupt.

The idea is to rewrite the interrupt service routine about 1Ch (system clock).
This procedure will simply invoke a dispatcher every temporal quantum...
...this is my procedure:

Interrupt_1Ch_handler:      ; system clock ISR

   ; Call dispatcher every temporal quantum
   inc byte [TICK]
   cmp byte [TICK], 10   ; temporal quantum = 10 tick

   jz   dispatcher
   iret

Dispatcher should save system registers, call a scheduler and restore the registers:

dispatcher:
   mov byte [TICK], 0

   ; Save general purpose regs
   mov word [ax_reg], ax
   mov word [bx_reg], bx
   mov word [cx_reg], cx
   mov word [dx_reg], dx

   mov word [si_reg], si
   mov word [di_reg], di

   ; Save old flag register & PC
   pop word [ip_reg]
   pop word [cs_reg]
   pop word [flag_reg]

   call    _sched   ; Call scheduler

   ; Restore new general purpose regs
   mov word ax, [ax_reg]
   mov word bx, [bx_reg]
   mov word cx, [cx_reg]
   mov word dx, [dx_reg]

   mov word si, [si_reg]
   mov word di, [di_reg]

   ; Restore new flag register & PC
   push word [flag_reg]
   push word [cs_reg]
   push word [ip_reg]   
   
   ; Jump to the next instruction of the new task
   iret

For now scheduler is very very simple... just a round robin policy and doesn't save general purpose regs content:

_sched:
   ; only switch between task1 start and task2 start

   cmp byte [TURN], 0
   jz   go_t1   
   cmp byte [TURN], 1
   jz   go_t2

go_t1:
   mov byte [TURN], 1
   mov   [cs_reg], cs
   mov word [ip_reg], task1
   ret   
go_t2:
   mov byte [TURN], 0
   mov   [cs_reg], cs
   mov word [ip_reg], task2
   ret   

TURN    db 0

At last these are the 2 task that I would like to execute:

task1:
   mov   ah, 0Eh
   mov   al, 65   ; Print 'A' char
   int   10h
   jmp   task1
task2:
   mov   ah, 0Eh
   mov   al, 66   ; Print 'B' char
   int   10h
   jmp   task2

Ok, sorry for this long introduction.. but it has been necessary to explain me better.. since my English is not so perfect :-)

So.. scheduler works, but when task1 is executed, system clock ISR is never invoked!!! ?task1 fill the screen with A and B is never written, it seems that it makes a cli?
I don't know where is the problem, since if I write a simple procedure in the scheduler like "print a character on the screen" it works?

Thanks to all that want to help me?

Re:mutlitasking in real mode

Posted: Mon Jul 22, 2002 10:57 am
by drizzt
[attachment deleted by admin]

Re:mutlitasking in real mode

Posted: Mon Jul 22, 2002 11:14 am
by drizzt
Ooops... I'm very stupid... ::) I forgot to clean up the 8255 state befor task switching...
now I've added this:

...
mov al, 0x20 ; Clean up 8255 state
out 0x20, al
...

... and it works... thanx the same! ;D

BTW:

Posted: Tue Jul 23, 2002 7:49 am
by Kenneth Garin
Thankyou for posting your code!!! I've been needing a good example like this for my project. I won't use your code but learn from it to make my own in house OS for my projects.
Can you post the working code now to? That would be sooooo great.
Thanks again, Kg.

Re:mutlitasking in real mode

Posted: Tue Jul 23, 2002 2:20 pm
by Pype.Clicker
hmmm .. something do confuse me ... are those [ax_regs] etc. locations some global variables ? if they are, when several task are running, what prevent the exitting task A to trash the state saved by sleeping task B ...

or are there several places (i.e. one [ax_regs] for every task) ??

Re:mutlitasking in real mode

Posted: Wed Jul 24, 2002 1:31 pm
by drizzt
Yes pype that's right... as I said, for now, this code doesn't save the state of the tasks... to do this in the scheduler procedure I should save all task A and task B registers.
So I should manage 2 PCBs, one for A and one for B task...

...but I've preferred to include a very short and simple example to focalize as soon as possible where was my error...

...so I've had to cut same code; yes it's true! in dispatcher is useless to save ax, bx, etc. because when the new task arrives the state is overwritten...

As soon as possible I'll include here the complete code to manage a true task switching... ;)

Re:mutlitasking in real mode

Posted: Wed Jul 24, 2002 1:44 pm
by drizzt
[attachment deleted by admin]

Re:mutlitasking in real mode

Posted: Thu Jul 25, 2002 10:29 am
by Kenneth Garin
Thanks! :)

Re:mutlitasking in real mode

Posted: Thu Jul 25, 2002 10:49 am
by drizzt
[attachment deleted by admin]

Re:mutlitasking in real mode

Posted: Thu Jul 25, 2002 10:51 am
by drizzt
[attachment deleted by admin]