mutlitasking in real mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
drizzt

mutlitasking in real mode

Post 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?
drizzt

Re:mutlitasking in real mode

Post by drizzt »

[attachment deleted by admin]
drizzt

Re:mutlitasking in real mode

Post 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
Kenneth Garin

BTW:

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:mutlitasking in real mode

Post 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) ??
drizzt

Re:mutlitasking in real mode

Post 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... ;)
drizzt

Re:mutlitasking in real mode

Post by drizzt »

[attachment deleted by admin]
Kenneth Garin

Re:mutlitasking in real mode

Post by Kenneth Garin »

Thanks! :)
drizzt

Re:mutlitasking in real mode

Post by drizzt »

[attachment deleted by admin]
drizzt

Re:mutlitasking in real mode

Post by drizzt »

[attachment deleted by admin]
Post Reply