Practical Session 9
-
Upload
sacha-vazquez -
Category
Documents
-
view
29 -
download
2
description
Transcript of Practical Session 9
Variable DeclerationSTKSZ equ 16*1024CODEP equ 0FLAGSP equ 4SPP equ 8
section .rodataalign 16global numco
numco: dd 3CORS: dd CO1
dd CO2dd CO3
section .dataalign 16
; Structure for first co-routineCO1: dd Function1Flags1: dd 0SP1: dd STK1+STKSZ; Structure for second co-routineCO2: dd Function1Flags2: dd 0SP2: dd STK2+STKSZ; Structure for third co-routineCO3: dd Function2Flags3: dd 0SP3: dd STK3+STKSZ; CounterCOUNTER: dd 0MAX_ITER: dd 3
section .bssalign 16
CURR: resd 1SPT: resd 1SPMAIN: resd 1STK1: resb STKSZSTK2: resb STKSZSTK3: resb STKSZ
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 .textalign 16extern printfglobal init_co_from_cglobal start_co_from_cglobal end_co
; Initalize a co-routine number given as an argument from Cinit_co_from_c:
push EBPmov EBP, ESPpush EBXmov EBX, [EBP+8]mov EBX, [EBX*4+CORS]call co_initpop EBXpop EBPret
; EBX is pointer to co-routine structure to initializeco_init:
pushabts dword [EBX+FLAGSP],0 ; test if already initializedjc init_donemov EAX,[EBX+CODEP] ; Get initial PCmov [SPT], ESPmov ESP,[EBX+SPP] ; Get initial SPmov EBP, ESP ; Also use as EBPpush EAX ; Push initial "return" addresspushf ; and flagspusha ; and all other regsmov [EBX+SPP],ESP ; Save new SP in structuremov ESP, [SPT] ; Restore original SP
init_done:poparet
Function1; This function used as code for co-routines 0 and 1Function1:
push dword 0push dword 1push dword [CORS+8]push dword [CURR]push dword FMT1call printfadd ESP, 20mov EBX, [CORS+8]call dword resumemov dword [COUNTER], 0
func1_loop:push dword [COUNTER]push 2push dword [CORS+8]push dword [CURR]push dword FMT1call printfadd ESP, 20add dword [COUNTER], 1mov eax, [MAX_ITER]cmp [COUNTER], eaxjne func1_loop mov EBX, [CORS+8]call dword resumejmp end_co
Function2; This function used as code for co-routine 2Function2:
push dword 0push dword 1push dword [CORS]push dword [CURR]push dword FMT2call printfadd ESP, 16mov EBX, [CORS]call dword resumemov dword [COUNTER], 0
func2_loop:push dword [COUNTER]push dword 2push dword [CORS+4]push dword [CURR]push dword FMT2call printfadd ESP, 20
add dword [COUNTER], 1mov eax, [MAX_ITER]cmp [COUNTER], eaxjne func2_loopmov EBX, [CORS+4]call dword resumepush dword 3push dword [CORS]push dword [CURR]push dword FMT2call printfadd ESP, 16mov EBX, [CORS]call dword resumepush dword 4push dword [CORS+4]push dword [CURR]push dword FMT2call printfadd ESP, 16mov EBX, [CORS+4]call dword resumejmp end_co
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-routineresume:
pushf ; Save state of callerpushamov EDX, [CURR]mov [EDX+SPP],ESP; Save current SP
do_resume:mov ESP, [EBX+SPP] ; Load SP for resumed co-routinemov [CURR], EBXpopa ; Restore resumed co-routine statepopfret ; "return" to resumed co-routine!
Start and End; C-callable start of the first co-routinestart_co_from_c:
push EBPmov EBP, ESPpushamov [SPMAIN], ESP ; Save SP of main codemov EBX, [EBP+8] ; Get number of co-routinemov EBX, [EBX*4+CORS] ; and pointer to co-routine structurejmp do_resume
; End co-routine mechanism, back to C mainend_co:
mov ESP, [SPMAIN] ; Restore state of main codepopapop EBPret
ExampleCURR
SPT
SPMAIN
STK1
STK2
STK3
numco
CORS
Function1 CO1
0 Flags1
SP1
Function1 CO2
0 Flags2
SP2
Function2 CO3
0 Flags3
SP3
0 COUNTER
3 MAX_ITER
After InitCURR
SPT
SPMAIN
STK1
Registers
Flags
Function1
STK2
Registers
Flags
Function1
STK3
Registers
Flags
Function2
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
ResumingCO2 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
STK2
ESP ……….
……….
STK3
Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret
Resuming – Resume is CalledCO2 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
STK2
ESP Addr2
……….
……….
STK3
Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret
ResumingCO2 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
ESP Registers STK2
Flags
Addr2
……….
……….
STK3
Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret
ResumingCO2 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
ESP Registers STK2
Flags
Addr2
……….
……….
STK3
Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret
ResumingCO2 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
Registers STK2
Flags
Addr2
……….
……….
STK3
ESP Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP,
[EBX+SPP] mov [CURR], EBXpopapopfret
ResumingCO3 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
Registers STK2
Flags
Addr2
……….
……….
STK3
ESP Registers
Flags
Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret
ResumingCO3 CURR
SPT
SPMAIN
STK1
Registers
Flags
Addr1
Registers STK2
Flags
Addr2
……….
……….
STK3
ESP Addr3
numco
CORS
Function1 CO1
1 Flags1
SP1
Function1 CO2
1 Flags2
SP2
Function2 CO3
1 Flags3
SP3
0 COUNTER
3 MAX_ITER
resume:pushf
pushamov EDX, [CURR]mov [EDX+SPP],ESP
do_resume:mov ESP, [EBX+SPP] mov [CURR], EBXpopapopfret