1 / 6

8.6 Multitasking

8.6 Multitasking. Switching context from one user to the next one. User 1 waits while other users are serviced. User 1. User 2. User 3. User 4. User 1. User 2. User 3. User 4. User 1. User 2. User 3. User 4. the time slice to work for user 3.

argyle
Télécharger la présentation

8.6 Multitasking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8.6 Multitasking Switching context from one user to the next one User 1 waits while other users are serviced User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4 ... the time slice to work for user 3 Saving all the information stored in the microprocessor, needed by the previous user, but being destroyed by the subsequent users (registers, flags) - usually on stack =>each user has to have its own stack area. User 1 User 2 User 3 User 4 P time

  2. 8.6 Multitasking ;Program SWITCHER.ASM: Automatically ;switch between two simple tasks. .MODEL SMALL .CODE TASK DB 0 ;task number (0...1) COUNT DB '0' ;initial count (0...9) T1KNT DB 4 ;count rate value (4...0) ALPHA DB 'A' ;initial alpha (A...Z) T2KNT DB 12 ;alpha rate value (12...0) OLDIPCS DD ? ;old interrupt address .STARTUP SUB AH,AH ;set video mode function MOV AL,3 ;25 by 80 color INT 10H ;BIOS call MOV AH,35H ;get interrupt vector function MOV AL,1CH ;timer interrupt number INT 21H ;DOS call MOV WORD PTR CS:OLDIPCS,BX ;save old IP MOV WORD PTR CS:OLDIPCS[2],ES ;save old CS MOV AX,CS ;load current CS value MOV DS,AX LEA DX,CS:SWAP ;load address of new ISR MOV AH,25H ;set interrupt vector function MOV AL,1CH ;timer interrupt number INT 21H ;DOS call WAIT4KY: MOV AH,1 ;read keyboard status function INT 16H ;BIOS call JZ WAIT4KY ;loop until any key is pressed MOV DX,WORD PTR CS:OLDIPCS ;old interrupt IP MOV DS,WORD PTR CS:OLDIPCS[2] ;old interrupt CS MOV AL,1CH ;timer interrupt number MOV AH,25H ;set interrupt vector function INT 21H ;DOS call MOV AH,1 ;read keyboard function INT 21H ;DOS call .EXIT The firs part of this program (until label WAIT4KY) installs the new ISR “SWAP”, adding it to the normal function of INTR 1CH (it hooks INTR 1CH). After installation, every time the hardware INTR 1CH is acknowledged, first the new task “SWAP” is performed, and after that the control is passed to the old task (initial ISR) of INTR 1CH (JMP CS:OLDIPCS ). The second part of this program (from label WAIT4KY until EXIT) uninstalls “SWAP”, restoring the old function (ISR) of INTR 1CH. set video mode read from IPT and save the old ISR pointer for INT 1CH as: OLDPICS. replace it with a pointer to SWAP. wait until any key is pressed which is the difference ? restore the old IPT. wait until any key is pressed

  3. 8.6 Multitasking Old ISR for INTR 1CH save the used registers on stack restore the used registers IRET: restores FLAGS and pick-up return address from stack SWAP: PUSHF ;NOT NEEDED!!! PUSH AX ;save registers used here PUSH BX PUSH CX PUSH DX NOT CS:TASK ;switch tasks CMP CS:TASK,0 ;task 2's turn now? JNZ ET2 CALL TASK1 ;perform task 1 JMP BYE ;go resume interrupt chain ET2: CALL TASK2 ;perform task 2 BYE: POP DX ;restore registers POP CX POP BX POP AX POPF ;NOT NEEDED!!! JMP CS:OLDIPCS ;go execute old ISR The requirements for “SWAP” are to: - restore the content of each used register (not FLAGS and return address, already saved by INTR acknowledge). - complete ALL the job it has to do in each session (no context to pass to next session => no own stack area needed). - finish quick each session (to let time for old ISR to complete until the next INTR 1CH acknowledgement). Main program, interrupted INTR 1CH saves FLAGS Three times: no action Task1 Fourth time: change digit (2.27/sec) 9.1times/sec. SWAP (18.2 times each second) Eleven times: no action Task2 9.1times/sec. Twelfth time: change digit (.75/sec)

  4. 8.6 Multitasking TASK1 PROC NEAR DEC CS:T1KNT ;decrement rate value JNZ ER1 ;exit if task is still asleep MOV CS:T1KNT,4 ;else, reload rate value MOV AH,2 ;set cursor position MOV BH,0 ;display page 0 MOV DH,12 ;row 12 MOV DL,37 ;column 37 INT 10H ;BIOS call MOV AL,CS:COUNT ;load count value MOV BL,2 ;color is green MOV CX,1 ;write one character MOV AH,9 ;write character/attribute INT 10H ;BIOS call INC CS:COUNT ;increment count value CMP CS:COUNT,'9'+1 ;wrap around? JNZ ER1 MOV CS:COUNT,'0' ;set initial count value ER1: RET TASK1 ENDP TASK2 PROC NEAR DEC CS:T2KNT ;decrement rate value JNZ ER2 ;exit if task is still asleep MOV CS:T2KNT,12 ;else, reload rate value MOV AH,2 ;set cursor position MOV BH,0 ;display page 0 MOV DH,12 ;row 12 MOV DL,43 ;column 43 INT 10H ;BIOS call MOV AL,CS:ALPHA ;load alpha value MOV BL,1 ;color is blue MOV CX,1 ;write one character MOV AH,9 ;write character/attribute INT 10H ;BIOS call INC CS:ALPHA ;increment alpha value CMP CS:ALPHA,'Z'+1 ; wrap around? JNZ ER2 MOV CS:ALPHA,'A' ;set initial alpha value ER2: RET TASK2 ENDP END Write a blue uppercase letter on row 12 column 43, increment ASCII code for next time. If incremented value is greater then “Z”, initializes it to “A”. Write a green decimal digit on row 12 column 37, increment digit for next time. If incremented value is greater then 9, initializes it to 0. All the needed data (T1KNT, T2KNT, COUNT, ALPHA) is in the common CODE SEGMENT => overriding the default (DATA) segment for MOVs, INCs, CMPs, etc. All the job is performed in a session => no need for stack context saving.

  5. 8.6 Multitasking ;Program TSLICE.ASM: Automatically switch between four tasks. ;Each task has it's own stack area. .MODEL SMALL 0000 .DATA 0000 0040 [0000] USER0 DW 64 DUP (?) ;user stack areas 0080 0040 [0000] USER1 DW 64 DUP (?) 0100 0040 [0000] USER2 DW 64 DUP (?) 0180 0040 [0000] USER3 DW 64 DUP (?) 0200 00 WHO DB 0 ;current user number(0...3) 0201 0080 STACK0 DW USER0+64*2 ;storage for user stack pointers 0203 0100 STACK1 DW USER1+64*2 0205 0180 STACK2 DW USER2+64*2 0207 0200 STACK3 DW USER3+64*2 0000 .CODE ;Installer (not shown) ;- hook or build a periodical INTR (set a counter to periodically request a hardware interrupt) ;- load DS and SS with SEG USER0 ;- initialize 13 words on stack areas for each of USER1, USER2 and USER3: ; - fictious values for FLAGS (will be loaded by first IRET) ; - the corresponding task address in format CS:IP (IP at lower address) ; (will be used as FAR RETURN ADDRESS to pass program control first time) ; - fictious values for all general purpose registers (AX to DI), DS and ES ; (will be loaded “back” first time the corresponding task will gain control) ; - the values in STACK1...STACK3 are updated to point the new STACK TOP for each user. ;- load SP with the content of STACK0 (top of stack area for USER0) ;- enable interrupts and jump to the USER0’s task. SP= STACK0 Stack area for user 1 (highest address up) Stack area for user 2 (highest address up) Stack area for user 3 (highest address up) ?? ?? ?? ?? ?? ?? ... FL high FL low CS h T1 CS l T1 IP h T1 IP l T1 AH AL ... ES low ... FL high FL low CS h T2 CS l T2 IP h T2 IP l T2 AH AL ... ES low ... FL high FL low CS h T3 CS l T3 IP h T3 IP l T3 AH AL ... ES low ... Stack area for user 0 (highest address up) STACK1 STACK1 STACK1 modify the corresponding memory locations in the IPT to specify the address (CS:IP) of the first instruction in the new ISR (TSLICE).

  6. 8.6 Multitasking Stack area for user 1 (highest address up) Stack area for user 2 (highest address up) Stack area for user 3 (highest address up) Stack area for user 0 (highest address up) FL high FL low CS h T0 CS l T0 IP h T0 IP l T0 AH AL ... ES low ... FL high FL low CS h T1 CS l T1 IP h T1 IP l T1 AH AL ... ES low ... FL high FL low CS h T2 CS l T2 IP h T2 IP l T2 AH AL ... ES low ... FL high FL low CS h T3 CS l T3 IP h T3 IP l T3 AH AL ... ES low ... SP= STACK0 STACK1 STACK1 STACK1 TSLICE: PUSHA ;save all registers TSLICE.ASM(16): error A2085: instruction or register not accepted in current CPU mode 0000 1E PUSH DS 0001 06 PUSH ES 0002 B8 ---- R MOV AX,seg WHO ;load system data segment 0005 8E D8 MOV DS,AX 0007 A0 0200 R MOV AL,WHO ;get user number 000A 98 CBW ;extend into 16 bits 000B 03 C0 ADD AX,AX ;double accumulator 000D 8B F8 MOV DI,AX ;load index 000F 8D 2E 0201 R LEA BP,STACK0 ;point to stack pointer table 0013 89 23 MOV [BP+DI],SP ;save user stack pointer 0015 FE 06 0200 R INC WHO ;increment user number 0019 83 C7 02 ADD DI,2 ;and index register 001C 83 FF 08 CMP DI,8 ;wrap around to user 0? 001F 75 08 JNZ GETSTACK 0021 C6 06 0200 R 00 MOV WHO,0 ;reset user number 0026 BF 0000 MOV DI,0 ;and index register 0029 8B 23 GETSTACK: MOV SP,[BP+DI] ;load new stack pointer 002B 07 POP ES ;restore new user's registers 002C 1F POP DS POPA TSLICE.ASM(36): error A2085: instruction or register not accepted in current CPU mode 002D CF IRET ;go service new user ... TSLICE is arrived as result of a hardware interrupt acknowledge => IE:automatically reset => no other maskable INTR is acknowledged until IE is set in the program or the FLAGS reg. is restored by IRET .286 assembler directive needed at program begin

More Related