8.6 Multitasking

6
8.6 Multitasking User 1 P User 2 User 3 User 4 User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4 User 1 User 2 User 3 User 4 ... time the time slice to work for user 3 Switching context from one user to the next one User 1 waits while other users are serviced 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.

description

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. - PowerPoint PPT Presentation

Transcript of 8.6 Multitasking

Page 1: 8.6 Multitasking

8.6 Multitasking

User1

P

User2

User3

User4

User1

User2

User3

User4

User1

User2

User3

User4

User1

User2

User3

User4 ...

time

the time slice to work for user 3

Switching context from one user to the next oneUser 1 waits while other

users are serviced

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.

Page 2: 8.6 Multitasking

;Program SWITCHER.ASM: Automatically ;switch between two simple tasks. .MODEL SMALL .CODETASK 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 callWAIT4KY: 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 ).

8.6 Multitasking

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

restore the old IPT.

wait until any key is pressed

which is the difference ?

set video mode

The second part of this program (from label WAIT4KY until EXIT) uninstalls “SWAP”, restoring the old function (ISR) of INTR 1CH.

Page 3: 8.6 Multitasking

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 chainET2: CALL TASK2 ;perform task 2BYE: POP DX ;restore registers POP CX POP BX POP AX POPF ;NOT NEEDED!!! JMP CS:OLDIPCS ;go execute old ISR

8.6 Multitasking

SWAP

(18.2 times each second)

Task1

Task2

9.1times/sec.

9.1times/sec.

Three times: no action

Fourth time: change digit (2.27/sec)

Eleven times: no action

Twelfth time: change digit (.75/sec)

Main program, interrupted

INTR 1CH saves FLAGS

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).

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

Page 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 valueER1: RETTASK1 ENDP

8.6 MultitaskingTASK2 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 valueER2: RETTASK2 ENDP END

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.

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.

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”.

Page 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.

8.6 Multitasking

STACK1

FL highFL lowCS h T1CS l T1IP h T1IP l T1AHAL...ES low...

Stack area for user 1 (highest address up)

SP=STACK0

????????????...

Stack area for user 0 (highest address up)

STACK1

FL highFL lowCS h T2CS l T2IP h T2IP l T2AHAL...ES low...

Stack area for user 2 (highest address up)

STACK1

FL highFL lowCS h T3CS l T3IP h T3IP l T3AHAL...ES low...

Stack area for user 3 (highest address up)

modify the corresponding memory locations in the IPT to specify the address (CS:IP) of the first instruction in the new ISR (TSLICE).

Page 6: 8.6 Multitasking

TSLICE: PUSHA ;save all registersTSLICE.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

POPATSLICE.ASM(36): error A2085: instruction or register not accepted in current CPU mode 002D CF IRET ;go service new user...

8.6 Multitasking

STACK1

FL highFL lowCS h T1CS l T1IP h T1IP l T1AHAL...ES low...

Stack area for user 1 (highest address up)

SP=STACK0

FL highFL lowCS h T0CS l T0IP h T0IP l T0AHAL...ES low...

Stack area for user 0 (highest address up)

STACK1

FL highFL lowCS h T2CS l T2IP h T2IP l T2AHAL...ES low...

Stack area for user 2 (highest address up)

STACK1

FL highFL lowCS h T3CS l T3IP h T3IP l T3AHAL...ES low...

Stack area for user 3 (highest address up)

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