Presentation is loading. Please wait.

Presentation is loading. Please wait.

8.6 Multitasking User 1 P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.

Similar presentations


Presentation on theme: "8.6 Multitasking User 1 P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."— Presentation transcript:

1 8.6 Multitasking User 1 P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.

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

3 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 8.6 Multitasking SWAP (18.2 times each second) Task1 Task2 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

4 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 8.6 Multitasking 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 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”.

5 ;Program TSLICE.ASM: Automatically switch between four tasks. ;Each task has it's own stack area..MODEL SMALL 0000.DATA 0000 0040 [0000]USER0DW 64 DUP (?);user stack areas 0080 0040 [0000]USER1DW 64 DUP (?) 0100 0040 [0000]USER2DW 64 DUP (?) 0180 0040 [0000]USER3DW 64 DUP (?) 0200 00WHO DB 0 ;current user number(0...3) 0201 0080STACK0DW USER0+64*2;storage for user stack pointers 0203 0100STACK1DW USER1+64*2 0205 0180STACK2DW USER2+64*2 0207 0200STACK3DW 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 high FL low CS h T1 CS l T1 IP h T1 IP l T1 AH AL... ES low... Stack area for user 1 (highest address up) SP= STACK0  ??... Stack area for user 0 (highest address up) STACK1  FL high FL low CS h T2 CS l T2 IP h T2 IP l T2 AH AL... ES low... Stack area for user 2 (highest address up) STACK1  FL high FL low CS h T3 CS l T3 IP h T3 IP l T3 AH AL... 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).

6 TSLICE:PUSHA;save all registers TSLICE.ASM(16): error A2085: instruction or register not accepted in current CPU mode 0000 1EPUSH DS 0001 06PUSH ES 0002 B8 ---- RMOV AX,seg WHO;load system data segment 0005 8E D8MOV DS,AX 0007 A0 0200 RMOV AL,WHO;get user number 000A 98CBW;extend into 16 bits 000B 03 C0ADD AX,AX;double accumulator 000D 8B F8MOV DI,AX;load index 000F 8D 2E 0201 RLEA BP,STACK0;point to stack pointer table 0013 89 23MOV [BP+DI],SP;save user stack pointer 0015 FE 06 0200 RINC WHO;increment user number 0019 83 C7 02ADD DI,2;and index register 001C 83 FF 08CMP DI,8;wrap around to user 0? 001F 75 08JNZ GETSTACK 0021 C6 06 0200 R 00MOV WHO,0;reset user number 0026 BF 0000MOV DI,0;and index register 0029 8B 23GETSTACK: MOV SP,[BP+DI] ;load new stack pointer 002B 07POP ES;restore new user's registers 002C 1FPOP DS POPA TSLICE.ASM(36): error A2085: instruction or register not accepted in current CPU mode 002D CFIRET;go service new user... 8.6 Multitasking STACK1  FL high FL low CS h T1 CS l T1 IP h T1 IP l T1 AH AL... ES low... Stack area for user 1 (highest address up) SP= STACK0  FL high FL low CS h T0 CS l T0 IP h T0 IP l T0 AH AL... ES low... Stack area for user 0 (highest address up) STACK1  FL high FL low CS h T2 CS l T2 IP h T2 IP l T2 AH AL... ES low... Stack area for user 2 (highest address up) STACK1  FL high FL low CS h T3 CS l T3 IP h T3 IP l T3 AH AL... 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


Download ppt "8.6 Multitasking User 1 P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."

Similar presentations


Ads by Google