Practical Session 7 Computer Architecture and Assembly Language.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Practical Session 3 Computer Architecture and Assembly Language.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Practical Session 9 Co-Routines. Co-Routines Co-routine state.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Position Independent Code self sufficiency of combining program.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
IA32 (Pentium) Processor Architecture. Processor modes: 1.Protected (mode we will study) – 32-bit mode – 32-bit (4GB) address space 2.Virtual 8086 modes.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
Compiler Construction Code Generation Activation Records
Practical Session 7. Co-Routines co-routines are assembly implementation of threads each co-routine decides to which co- routine to pass a control - unlike.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Assembly Language Co-Routines
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Section 5: Procedures & Stacks
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Assembly language.
C function call conventions and the stack
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Introduction to Compilers Tim Teitelbaum
Practical Session 7.
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Stack Frames and Advanced Procedures
Practical Session 9.
MIPS Procedure Calls CSE 378 – Section 3.
Practical Session 4.
Multi-modules programming
Miscellaneous Topics.
Low-Level Thread Dispatching on the x86
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Implementing Functions: Overview
Presentation transcript:

Practical Session 7 Computer Architecture and Assembly Language

Co-Routines co-routines are assembly implementation of threads each co-routine decides to which co-routine to pass a control (unlike “usual” threads) We would implement “silly” round robin scheduling algorithm: main()  scheduler  co-routine1  scheduler  co-routine2  scheduler   co-routine1  scheduler  co-routine2  scheduler  main()

Co-routine state co-routine is denoted by Coi (i is co-routine’s id) scheduler is also a co-routine co-routine suspends itself after some “time slice” co-routine resumes a scheduler co-routine should save its current state before it suspends itself (in order to continue its execution later) stack state registers flags stack pointer (ESP) instructions pointer (EIP)

Co-routine structure STKSIZE equ 16*1024 ;(16 Kb) STKi:resb STKSIZE COi:dd Functioni ; pointer to co-routine function Flags:dd 0 ; 0 if co-routine is not initialized, 1 otherwise SPi:dd STKi + STKSIZE ; pointer to the beginning of co-routine stack Co-routine structure: We define an array of co-routines’ structures: why SPi points to the end of stack ? to be able to use push and pop stack functionality with ESP SPi0Funci co-routine structure co-routine stack

STKSZequ16*1024; co-routine stack size CODEPequ0; offset of pointer to co-routine function in co-routine structure FLAGSPequ4; offset of pointer to flags co-routine structure SPPequ8; offset of pointer to co-routine stack in co-routine structure section.rodata align 16 globalnumco numco:dd3 CORS:ddCO1 ddCO2 ddCO3 section.data align 16 CO1:ddFunction1 ; structure for first co-routine Flags1:dd0 SP1:ddSTK1+STKSZ CO2:ddFunction1 ; structure for second co-routine Flags2:dd0 SP2:ddSTK2+STKSZ CO3:ddFunction2 ; structure for third co-routine Flags3:dd0 SP3:ddSTK3+STKSZ section.bss align 16 CURR:resd1 SPT:resd1 ; temporary stack pointer variable SPMAIN:resd1 ; stack pointer of main STK1:resbSTKSZ STK2:resbSTKSZ STK3:resbSTKSZ Variables Declaration

FunciEFlagsregistersSPi0Funci Co-routine initialization - save initial co-routine state co_init: … mov eax, [ebx+CODEP] ; get initial PC … mov esp, [EBX+SPP] ; get initial SP … push eax ; push initial “return” address pushfd ; push flags pushad ; push all other registers mov [ebx+SPP], esp ; save new SPi value (after all the pushes) … co-routine structure co-routine stack init_co_from_c: … mov ebx, [ebp+8] ; get co-routine ID number mov ebx, [4*ebx + CORS] ; get COi pointer call co_init … main() { /* initialize co-routines*/ for(i = 0; i < numco; i++) init_co_from_c(i); /* start a scheduler co-routine*/ start_co_from_c (2); }

Co-routine initialization section.text align 16 externprintf globalinit_co_from_c globalstart_co_from_c globalend_co init_co_from_c: pushEBP movEBP, ESP pushEBX movEBX, [EBP+8] ; EBX contains a number of the co-routine to be initialized movEBX, [EBX*4+CORS]; EBX contains a pointer to co-routine structure to be initialized callco_init popEBX popEBP ret co_init: pushad btsdword [EBX+FLAGSP],0 ; test if already initialized jcinit_done movEAX,[EBX+CODEP]; get initial PC mov[SPT], ESP; save original SP movESP,[EBX+SPP] ; get initial SP movEBP, ESP ; also use as EBP pushEAX ; push initial "return" address (initial PC) pushfd ; push flags pushad ; push all other registers mov[EBX+SPP],ESP ; save new SP in structure movESP, [SPT] ; restore original SP init_done: popad ret ‘bts’ instruction tests one bit of its first operand, whose index is given by the second operand, and stores the value of that bit in the carry flag (CF). In addition, ‘bts’ sets the bit to be 1.

Start co-routine scheduler We start scheduling by suspending main() and resuming a scheduler co-routine. start_co_from_c: pushEBP mov EBP, ESP pushad mov [SPMAIN], ESP; save ESP of main () mov EBX, [EBP+8]; gets ID number of a scheduler mov EBX, [EBX*4 + CORS]; gets a pointer to a scheduler structure jmp do_resume; resume a scheduler co-routine We end scheduling by going back to main(). end_co: movESP, [SPMAIN] ; restore state of main code popad popEBP ret main() { /* initialize co-routines*/ for(i = 0; i < numco; i++) init_co_from_c(i); /* start a scheduler co-routine*/ start_co_from_c (2); }

Scheduler co-routine function Scheduler function is started by main. scheduler_Function: pick up some thread ID i mov EBX, [CORS + i*4] ; resumes Coi call resume pick up some other thread ID j mov EBX, [CORS + j*4] ; resumes Coj call resume … jmp end_co ; resume main ‘call resume’ save a state of the current co-routine resume a state of the next co-routine (EBX should contain a pointer to it) EBX is pointer to co-init structure of the co-routine to be resumed. CURR holds a pointer to co-init structure of the current co-routine. resume: ; save state of caller pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP ; save current SP do_resume: ; load SP for resumed co-routine movESP, [EBX+SPP] mov[CURR], EBX popad ; restore resumed co-routine state popfd ret ; "return" to resumed co-routine! after ‘call resume’ return address (i.e. EIP) is pushed automatically into (co-routine) stack we only have to save EFLAGS, ESP, and registers

Function1 This function used as code for co-routines 1 and 2 FMT1: db "Function1, co %lx, called by %lx, pass %ld", 10, 0 Function1: pushdword 1 pushdword [CORS+8] pushdword [CURR] pushdword FMT1 callprintf addESP, 20 movEBX, [CORS+8]; resume a scheduler callresume push dword 2 pushdword [CORS+8] pushdword [CURR] pushdword FMT1 callprintf add ESP, 20 movEBX, [CORS+8] ; resume a scheduler callresume

Function2 This function used as code for co-routine 3 FMT2: db "Function2, co %lx, called by %lx, pass %ld", 10, 0 Function2: pushdword1 pushdword [CORS] pushdword [CURR] pushdword FMT2 callprintf addESP, 16 movEBX, [CORS] ; resume CO1 callresume pushdword2 pushdword [CORS+4] pushdword [CURR] pushdword FMT2 callprintf addESP, 20 movEBX, [CORS+4] ; resume CO2 callresume pushdword3 pushdword [CORS] pushdword [CURR] pushdword FMT2 callprintf addESP, 16 movEBX, [CORS] ; resume CO1 callresume pushdword4 pushdword [CORS+4] pushdword [CURR] pushdword FMT2 callprintf addESP, 16 movEBX, [CORS+4] ; resume CO2 callresume jmp end_co ; resume main

Run example – data declaration CURR SPT SPMAIN STK1 STK2 STK3 3numco CO1CORS CO2 CO3 Function1CO1 0Flags1 SP1 Function1CO2 0Flags2 SP2 Function2CO3 0Flags3 SP3 0COUNTER 3MAX_ITER.bss.data

Afterco-routine initialization After co-routine initialization CURR SPT SPMAIN STK1 Registers Flags Function1 STK2 Registers Flags Function1 STK3 Registers Flags Function2 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data

Resuming - right before CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret.bss.data ESP 

Resuming – resume is called CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 STK2 Addr2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming – backup registers CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming – backup stack pointer CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming - load stack pointer of resume co-routine CO2CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming – set current co-routine variable CO3CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 Registers Flags Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming – restore a state of loaded co-routine CO3CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 Addr3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 

Resuming – go to execute the loaded co-routine CO3CURR SPT SPMAIN STK1 Registers Flags Addr1 RegistersSTK2 Flags Addr2 ………. STK3 3numco CO1CORS CO2 CO3 Function1CO1 1Flags1 SP1 Function1CO2 1Flags2 SP2 Function2CO3 1Flags3 SP3 0COUNTER 3MAX_ITER.bss.data resume: pushfd pushad movEDX, [CURR] mov[EDX+SPP],ESP do_resume: movESP, [EBX+SPP] mov[CURR], EBX popad popfd ret ESP 