Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Architecture and Assembly Language

Similar presentations


Presentation on theme: "Computer Architecture and Assembly Language"— Presentation transcript:

1 Computer Architecture and Assembly Language
Practical Session 9

2 Game of life You can see the simulator here.

3 Game of life Simulates Evolution of two-dimensional hexagonal matrix’s cells. Each cell can be alive or dead. A cell’s state in each iteration (generation) is set with accordance to its state and its neighbors’ states.

4 Game rules If the cell is currently alive, then it will remain alive in the next generation if and only if exactly 3 or 4 of its neighbors are currently alive. Otherwise it dies. Examples: me stays alive me stays alive me me me me A dead cell remains dead in the next generation, unless it has exactly 2 living neighbors. Example: me comes alive me me

5 Cell neighbors At the board edges
me 1 2 3 4 5 6 At the board edges cells of the first row are neighbors of the cells in the last row cells of the first column are neighbors of the cells in the last column 4 6 5 2 3 1

6 Input file Each cell is given an initial state from input file.
1 alive cells are denoted by ‘1’ in input file dead cells are denoted by ‘0’ in input file At each generation, a cell will determine its next state according to its former state and its neighbors’ former states, using the game rules. After calculation of the next state, each cell updates its state. 1 calculate next state 1 update next state 1 v v x x v x x v x x v v

7 i,j i,j i,j i,j Cell neighbors or i-1, j-1 i-1,j i-1,j i-1,j+1 i, j-1
2 1 2 or me 6 3 6 me 3 5 4 5 4 i,j i,j i-1, j-1 i-1,j i-1,j i-1,j+1 i, j-1 i,j i, j+1 i, j-1 i,j i, j+1 i+1, j-1 i+1,j i+1,j i+1, j+1

8 n cell instances (2-dimensional matrix) a printer a scheduler
Implementation Using the co-routine mechanism, with the following co-routines: n cell instances (2-dimensional matrix) a printer a scheduler

9 (stage 1) resume  (stage 2) resume
A Cell Cell can be alive(‘1’) or dead(‘0‘) Cell executes a simple infinite loop: Calculate next state using current state (of a cell and its neighbors) Update current state of to a new state After each of these two stages, the cell co-routine must resume the scheduler. In other words, it loops (forever) over: (stage 1) resume  (stage 2) resume

10 A scheduler The printer implements simple round-robin algorithm
void scheduler (int cycles) iterate cycles times over all cells after each P resumes of cell co-routines, resume the printer (1) resume  (2) resume … right before exit the program, resume the printer once more, and then terminate the process What is the definition of K?? The printer Prints the entire “world” (the global array), whenever it gets “time”.

11 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

12 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

13 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

14 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

15 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

16 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

17 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

18 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

19 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

20 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

21 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

22 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

23 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

24 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

25 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

26 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

27 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

28 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

29 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

30 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

31 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) print matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

32 Program’s flow row=0, column=0
while (numberOfGenerations < maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) column=(column+1)%maximalNumberOfColumns if(column ==0) row=(row+1)%maximalNumberOfRows if (row==0 && column==0) numberOfGenerations+=0.5 Printer Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1) calculate stage calculate stage calculate stage update matrix update matrix update matrix calculate stage calculate stage calculate stage update matrix update matrix update matrix

33 Program’s flow > ass3 <filename> <length> <width> <g> <P> <filename> - name of a file contain the initial state of the game board. A series of size <2*width> of ‘0 ‘ and ‘1’ in each line, separated by space. The file contain <length> lines. <g> - number of generations <P> - printing frequency your array dimensions will be <width>*<length>

34 Program’s flow When invoked, and using the co-routines mechanism from class, your application will: Set up: a state array, Length, Width, P, g Initiate all co-routines: each cell gets parameters i,j - its indices in the global array a scheduler gets g, P, Length, and Width as parameters a printer gets Length and Width as parameters Initiate an array CORS of pointers to: cell0,0,…,celllength-1,width-1, scheduler, printer  Transfer control to scheduler

35 Program’s flow You may build cell in CORS array will point directly to the top of the stack of the corresponded co-routine. CORS: CoPrinter CoScheduler CO(0,0) CO(0,1) CO(i, j) … CO(i,j): Function Flags SP(i,j) CORS: SP_CoPrinter SP_CoScheduler SP(0,0) SP(0,1) SP(i, j)


Download ppt "Computer Architecture and Assembly Language"

Similar presentations


Ads by Google