Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Session 9 Co-Routines. Co-Routines Co-routine state.

Similar presentations


Presentation on theme: "Practical Session 9 Co-Routines. Co-Routines Co-routine state."— Presentation transcript:

1 Practical Session 9 Co-Routines

2 Co-Routines

3 Co-routine state

4 Variable Decleration STKSZequ16*1024 CODEPequ0 FLAGSPequ4 SPPequ8 section.rodata align 16 globalnumco numco:dd3 CORS:ddCO1 ddCO2 ddCO3 section.data align 16 ; Structure for first co-routine CO1:ddFunction1 Flags1:dd0 SP1:ddSTK1+STKSZ ; Structure for second co-routine CO2:ddFunction1 Flags2:dd0 SP2:ddSTK2+STKSZ ; Structure for third co-routine CO3:ddFunction2 Flags3:dd0 SP3:ddSTK3+STKSZ ; Counter COUNTER: dd 0 MAX_ITER: dd 3 section.bss align 16 CURR:resd1 SPT:resd1 SPMAIN:resd1 STK1:resbSTKSZ STK2:resbSTKSZ STK3:resbSTKSZ

5 Co-routine structure

6

7 Variables STKSZ – Stack Size CODEP – Code Position: Offset to code FLAGP – Flag Position: Offset to flags SPP – Stack Pointer Position: Offset to stack pointer numco – Number of Co-Routines: Global CORS –Co-Routines Array COi – Function Address of Co-Routine i Flagsi – Flags of Co-Routine i SPi – Stack Pointer of Co-Routine i STKi – The Stack of Co-Routine i SPMain – Stack Pointer of Main Function CURR – Current Co-Routine SPT – Temporary Storage for Stack Pointer

8 Initializtion section.text align 16 externprintf globalinit_co_from_c globalstart_co_from_c globalend_co ; Initalize a co-routine number given as an argument from C init_co_from_c: pushEBP movEBP, ESP pushEBX movEBX, [EBP+8] movEBX, [EBX*4+CORS] callco_init popEBX popEBP ret ; EBX is pointer to co-routine structure to initialize co_init: pusha btsdword [EBX+FLAGSP],0 ; test if already initialized jcinit_done movEAX,[EBX+CODEP] ; Get initial PC mov[SPT], ESP movESP,[EBX+SPP] ; Get initial SP movEBP, ESP ; Also use as EBP pushEAX ; Push initial "return" address pushf ; and flags pusha ; and all other regs mov[EBX+SPP],ESP ; Save new SP in structure movESP, [SPT] ; Restore original SP init_done: popa ret

9 Co-routine initialization

10

11

12 Function1 ; This function used as code for co-routines 0 and 1 Function1: push dword 0 push dword1 push dword [CORS+8] push dword [CURR] push dword FMT1 call printf add ESP, 20 mov EBX, [CORS+8] call dword resume mov dword [COUNTER], 0 func1_loop: push dword [COUNTER] push dword 2 push dword [CORS+8] push dword [CURR] push dword FMT1 call printf add ESP, 20 add dword [COUNTER], 1 mov eax, [MAX_ITER] cmp [COUNTER], eax jne func1_loop mov EBX, [CORS+8] call dword resume jmp end_co

13 Function2 ; This function used as code for co-routine 2 Function2: push dword 0 push dword1 push dword [CORS] push dword [CURR] push dword FMT2 call printf add ESP, 16 mov EBX, [CORS] call dword resume mov dword [COUNTER], 0 func2_loop: push dword [COUNTER] push dword2 push dword [CORS+4] push dword [CURR] push dword FMT2 call printf add ESP, 20 add dword [COUNTER], 1 mov eax, [MAX_ITER] cmp [COUNTER], eax jne func2_loop mov EBX, [CORS+4] call dword resume push dword3 push dword [CORS] push dword [CURR] push dword FMT2 call printf add ESP, 16 mov EBX, [CORS] call dword resume push dword4 push dword [CORS+4] push dword [CURR] push dword FMT2 call printf add ESP, 16 mov EBX, [CORS+4] call dword resume jmp end_co

14 Co-routine code (function)

15

16 Resume ; EBX is pointer to co-init structure of co-routine to be resumed ; CURR holds a pointer to co-init structure of the curent co-routine resume: pushf ; Save state of caller pusha movEDX, [CURR] mov[EDX+SPP],ESP; Save current SP do_resume: movESP, [EBX+SPP] ; Load SP for resumed co-routine mov[CURR], EBX popa; Restore resumed co-routine state popf ret; "return" to resumed co-routine!

17 Co-routine resume

18

19 Start and End ; C-callable start of the first co-routine start_co_from_c: pushEBP movEBP, ESP pusha mov[SPMAIN], ESP ; Save SP of main code movEBX, [EBP+8]; Get number of co-routine movEBX, [EBX*4+CORS] ; and pointer to co-routine structure jmpdo_resume ; End co-routine mechanism, back to C main end_co: movESP, [SPMAIN] ; Restore state of main code popa popEBP ret

20 Start Co-routine scheduler

21 Example CURR SPT SPMAIN STK1 STK2 STK3 numco CORS Function1CO1 0Flags1 SP1 Function1CO2 0Flags2 SP2 Function2CO3 0Flags3 SP3 0COUNTER 3MAX_ITER

22 After Init CURR SPT SPMAIN STK1 Registers Flags Function1 STK2 Registers Flags Function1 STK3 Registers Flags Function2 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER

23 Resuming CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 ESP  ………. STK3 Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

24 Resuming – Resume is Called CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 ESP  Addr2 ………. STK3 Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

25 Resuming CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 ESP  RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

26 Resuming CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 ESP  RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

27 Resuming CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 ESP  Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

28 Resuming CO3CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 ESP  Registers Flags Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret

29 Resuming CO3CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 ESP  Addr3 numco CORS Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushf pusha movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popa popf ret


Download ppt "Practical Session 9 Co-Routines. Co-Routines Co-routine state."

Similar presentations


Ads by Google