Practical Session 7 CoRoutines Coroutine state Variable Decleration
Practical Session 7
Co-Routines
Co-routine state
Variable Decleration STKSZ equ CODEP equ FLAGSP equ SPP equ section. rodata align 16 global numco: dd 3 CORS: dd CO 1 dd CO 2 dd CO 3 section. data align 16 ; Structure for first co-routine CO 1: dd Function 1 Flags 1: dd 0 SP 1: dd STK 1+STKSZ ; Structure for second co-routine CO 2: dd Function 1 Flags 2: dd 0 SP 2: dd STK 2+STKSZ ; Structure for third co-routine CO 3: dd Function 2 Flags 3: dd 0 SP 3: dd STK 3+STKSZ ; Counter COUNTER: dd 0 MAX_ITER: dd 3 16*1024 0 4 8 section. bss CURR: SPT: SPMAIN: STK 1: STK 2: STK 3: align 16 resd resb 1 1 1 STKSZ
Co-routine structure
Co-routine structure
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
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
Co-routine initialization
Co-routine initialization
Co-routine initialization
Function 1 ; This function used as code for co-routines 0 and 1 Function 1: push dword 0 push dword 1 push dword [CORS+8] push dword [CURR] push dword FMT 1 call printf add ESP, 20 mov EBX, [CORS+8] call dword resume mov dword [COUNTER], 0 func 1_loop: push dword [COUNTER] push 2 push dword [CORS+8] push dword [CURR] push dword FMT 1 call printf add ESP, 20 add dword [COUNTER], 1 mov eax, [MAX_ITER] cmp [COUNTER], eax jne func 1_loop mov EBX, [CORS+8] call dword resume jmp end_co
Function 2 ; This function used as code for co-routine 2 Function 2: push dword 0 push dword 1 push dword [CORS] push dword [CURR] push dword FMT 2 call printf add ESP, 16 mov EBX, [CORS] call dword resume mov dword [COUNTER], 0 func 2_loop: push dword [COUNTER] push dword 2 push dword [CORS+4] push dword [CURR] push dword FMT 2 call printf add ESP, 20 add dword [COUNTER], 1 mov eax, [MAX_ITER] cmp [COUNTER], eax jne func 2_loop mov EBX, [CORS+4] call dword resume push dword 3 push dword [CORS] push dword [CURR] push dword FMT 2 call printf add ESP, 16 mov EBX, [CORS] call dword resume push dword 4 push dword [CORS+4] push dword [CURR] push dword FMT 2 call printf add ESP, 16 mov EBX, [CORS+4] call dword resume jmp end_co
Co-routine code (function)
Co-routine code (function)
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!
Co-routine resume
Co-routine resume
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] popa pop EBP ret ; Restore state of main code
Start Co-routine scheduler
Example CURR numco SPT CORS SPMAIN STK 1 Function 1 0 CO 1 Flags 1 SP 1 STK 2 Function 1 0 CO 2 Flags 2 SP 2 Function 2 0 CO 3 Flags 3 SP 3 STK 3 0 COUNTER 3 MAX_ITER
After Init CURR numco SPT CORS SPMAIN STK 1 Function 1 Registers 1 Flags STK 2 Function 1 1 Function 2 Flags 1 Function 1 Flags 2 CO 3 Flags 3 SP 3 STK 3 Function 2 CO 2 SP 2 Registers Flags 1 SP 1 Function 1 Registers CO 1 0 COUNTER 3 MAX_ITER
Resuming resume: CO 2 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags CO 1 Flags 1 SP 1 Addr 1 STK 2 Function 1 1 CO 2 Flags 2 SP 2 ESP Function 2 ………. 1 ………. Flags Addr 3 Flags 3 SP 3 STK 3 Registers CO 3 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming – Resume is Called resume: CO 2 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags STK 2 Function 1 1 Function 2 ………. 1 ………. Addr 3 Flags 2 CO 3 Flags 3 SP 3 STK 3 Flags CO 2 SP 2 Addr 2 Registers Flags 1 SP 1 Addr 1 ESP CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming resume: CO 2 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags Registers STK 2 Flags Function 1 1 Function 2 ………. 1 ………. Addr 3 Flags 2 CO 3 Flags 3 SP 3 STK 3 Flags CO 2 SP 2 Addr 2 Registers Flags 1 SP 1 Addr 1 ESP CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming resume: CO 2 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags Registers STK 2 Flags Function 1 1 Function 2 ………. 1 ………. Addr 3 Flags 2 CO 3 Flags 3 SP 3 STK 3 Flags CO 2 SP 2 Addr 2 Registers Flags 1 SP 1 Addr 1 ESP CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming resume: CO 2 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags STK 2 Flags Function 1 1 Function 2 ………. 1 ………. Flags Addr 3 Flags 2 CO 3 Flags 3 SP 3 STK 3 Registers CO 2 SP 2 Addr 2 ESP Flags 1 SP 1 Addr 1 Registers CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming resume: CO 3 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags STK 2 Flags Function 1 1 Function 2 ………. 1 ………. Flags Addr 3 Flags 2 CO 3 Flags 3 SP 3 STK 3 Registers CO 2 SP 2 Addr 2 ESP Flags 1 SP 1 Addr 1 Registers CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
Resuming resume: CO 3 CURR numco SPT CORS SPMAIN do_resume: STK 1 Function 1 Registers 1 Flags STK 2 Flags Function 1 1 Function 2 ………. 1 ………. Flags 2 CO 3 Flags 3 SP 3 STK 3 Addr 3 CO 2 SP 2 Addr 2 ESP Flags 1 SP 1 Addr 1 Registers CO 1 0 COUNTER 3 MAX_ITER pushf pusha mov mov popa popf ret EDX, [CURR] [EDX+SPP], ESP, [EBX+SPP] [CURR], EBX
- Slides: 29