Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Session 7.

Similar presentations


Presentation on theme: "Practical Session 7."— Presentation transcript:

1 Practical Session 7

2 Co-Routines

3 Co-routine state

4 Variable Decleration STKSZ equ 16*1024 section .bss CODEP equ 0
FLAGSP equ 4 SPP equ 8 section .rodata align 16 global numco numco: dd 3 CORS: dd CO1 dd CO2 dd CO3 section .data ; Structure for first co-routine CO1: dd Function1 Flags1: dd 0 SP1: dd STK1+STKSZ ; Structure for second co-routine CO2: dd Function1 Flags2: dd 0 SP2: dd STK2+STKSZ ; Structure for third co-routine CO3: dd Function2 Flags3: dd 0 SP3: dd STK3+STKSZ ; Counter COUNTER: dd 0 MAX_ITER: dd 3 section .bss align 16 CURR: resd 1 SPT: resd 1 SPMAIN: resd 1 STK1: resb STKSZ STK2: resb STKSZ STK3: resb STKSZ

5 Co-routine structure

6 Co-routine structure

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 extern printf global init_co_from_c global start_co_from_c global end_co ; Initalize a co-routine number given as an argument from C init_co_from_c: push EBP mov EBP, ESP push EBX mov EBX, [EBP+8] mov EBX, [EBX*4+CORS] call co_init pop EBX pop EBP ret ; EBX is pointer to co-routine structure to initialize co_init: pusha bts dword [EBX+FLAGSP],0 ; test if already initialized jc init_done mov EAX,[EBX+CODEP] ; Get initial PC mov [SPT], ESP mov ESP,[EBX+SPP] ; Get initial SP mov EBP, ESP ; Also use as EBP push EAX ; Push initial "return" address pushf ; and flags pusha ; and all other regs mov [EBX+SPP],ESP ; Save new SP in structure mov ESP, [SPT] ; Restore original SP init_done: popa ret

9 Co-routine initialization

10 Co-routine initialization

11 Co-routine initialization

12 Function1 ; This function used as code for co-routines 0 and 1
push dword 0 push dword 1 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 2 add ESP, 20 add dword [COUNTER], 1 mov eax, [MAX_ITER] cmp [COUNTER], eax jne func1_loop jmp end_co

13 Function2 ; This function used as code for co-routine 2
push dword 0 push dword 1 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 dword 2 push dword [CORS+4] 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 dword 3 push dword [CORS] push dword [CURR] push dword FMT2 call printf add ESP, 16 mov EBX, [CORS] push dword 4 push dword [CORS+4] jmp end_co

14 Co-routine code (function)

15 Co-routine code (function)

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 mov EDX, [CURR] mov [EDX+SPP],ESP ; Save current SP do_resume: mov ESP, [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 Co-routine resume

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

20 Start Co-routine scheduler

21 Example CURR SPT SPMAIN STK1 STK2 STK3 numco CORS Function1 CO1 Flags1
Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER

22 After Init CURR SPT SPMAIN STK1 Registers Flags Function1 STK2 STK3
numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER

23 Resuming CO2 CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 ESP ……….
numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret

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

25 Resuming CO2 CURR SPT SPMAIN STK1 Registers Flags Addr1 ESP STK2
………. STK3 Addr3 numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret

26 Resuming CO2 CURR SPT SPMAIN STK1 Registers Flags Addr1 ESP STK2
………. STK3 Addr3 numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret

27 Resuming CO2 CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 Addr2
………. STK3 ESP Addr3 numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret

28 Resuming CO3 CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 Addr2
………. STK3 ESP Addr3 numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret

29 Resuming CO3 CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 Addr2
………. STK3 ESP Addr3 numco CORS Function1 CO1 1 Flags1 SP1 CO2 Flags2 SP2 Function2 CO3 Flags3 SP3 COUNTER 3 MAX_ITER resume: pushf pusha mov EDX, [CURR] mov [EDX+SPP],ESP do_resume: mov ESP, [EBX+SPP] mov [CURR], EBX popa popf ret


Download ppt "Practical Session 7."

Similar presentations


Ads by Google