Page 185 - DCAP507_SYSTEM_SOFTWARE
P. 185

Unit 11: Programming Languages Concept (II)




          11.3 Multitasking                                                                     Notes

          To understand the power of assembly language we discuss how to execute multitasking. In the
          debugger our thread of instructions was broken by the debugger; it got the control, used all
          registers, exhibited an detailed interface, waited for the key, and then reinstated processor state
          to what was instantly before disruption. Our program resumed as if nothing happened. The
          program execution was in the similar logical flow.
          If we have two dissimilar programs A and B. Program A is busted, its state saved, and returned
          to B rather than A. By looking at the instruction set, we can instantly say that nothing can prevent
          us from doing that. IRET will return to whatever CS and IP it locates on the stack. Now B is
          interrupted someway, its state saved, and we return back to A. A will have no way of recognizing
          that it was interrupted as its whole environment has been restored. It never knew the debugger
          took control when it was debugged. It sill has no way of gaining this knowledge. If this function
          of breaking and restoring programs is performed at high speed the user will sense that all the
          programs are executing simultaneously where actually they are being switched to and forth at
          high speed.

          In real meaning multitasking is simple, even though we have to be enormously cautious when
          implementing it. The surroundings of a program in the very simple case is all its registers and
          stack. Now to obtain control from the program without the program knowing regarding it, we
          can utilize the IRQ 0 highest priority interrupt that is from time to time coming to the processor.


                 Example: Now we show a very basic example of multitasking. We have two subroutines
          written in assembly language. All the techniques discussed here are pertinent to code written in
          higher level languages also. However, the code to control this multitasking cannot be simply
          written in a higher level language so we write it in assembly language. The two subroutines
          rotate bars by altering characters at the two corners of the screen and have infinite loops. By
          hooking the timer interrupt and saving and reinstating the registers of the tasks one by one, it
          occurs that both tasks are executing concurrently.


                 Example:
             001     ;  elementary  multitasking  of  two  threads[org  0x0100]  jmp  start  ;
             002     ax,bx,ip,cs,flags storage areataskstates: dw 0, 0, 0, 0, 0 ; task0 regsdw 0, 0, 0, 0,
             003     0  ;  task1  regsdw  0,  0,  0,  0,  0  ;  task2  regs  current:  db  0  ;  index  of  current
             004     taskchars: db ‘\|/-’ ; shapes to form a bar ; one task to be multitaskedtaskone:
             005     mov  al,  [chars+bx]  ;  read  the  next  shapemov  [es:0],  al  ;  write  at  top  left  of
             006     screen
             007
             008
             009
             010
             011
             012
             013
             014
             015
                                                                                 Contd...






                                           LOVELY PROFESSIONAL UNIVERSITY                                   179
   180   181   182   183   184   185   186   187   188   189   190