1 / 53

Practical session 9

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

jaguar
Download Presentation

Practical session 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Practical session 9 Assignment 3

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

  3. A Cell • Just to be perfectly clear: Each cell (organism) has 8 neighbors.

  4. Border cells • Every cell has 8 neighbors even 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

  5. Game rules • If the cell iscurrentlyalive, then it will remain alive in the next generation if and only if exactly 2 or 3 of its neighbors are currentlyalive. Otherwise it dies. • A deadcell remains dead in the next generation, unless it has exactly 3 living neighbors. • Organism age is the number of generation it was alive in a row. Maximum age is 9*.*A cell does not die after age 9, it continues its life according to the game rules, but its age stays unchanged.

  6. Our version • Each cell is given an initial state you read from an input file on program startup to a global array. • At each generation, a cell will determine its next state according to its former state and its neighbors’ former states, using the following rules. alive cells are denoted by ‘1’ in input file empty (dead) cells are denoted by space in input file

  7. Implementation • Using the co-routine mechanism, we’ll write a small simulator for the game. • Running co-routines: • n cell instances (2-dimensional matrix) • a printer • a scheduler

  8. A Cell • Can be alive(‘1’-’9’) or dead(‘ ‘) • Cell executes a simple infinite loop: • Calculate next state using current state (of “me” and my neighbors). • Update current state of “me” to new state • After each of these two stages, the cellco-routine must resume the scheduler. In other words, it loops (forever) over: (1) resume  (2) resume

  9. The printer • Simply printsthe entire “world” (the global array), whenever it gets “time”.

  10. The scheduler • You are to implement a simple round-robin scheduler • void scheduler (int cycles) • iterate cycles times over all cells • after each K resumes of cell co-routines, resume the printer (1) resume  (2) resume … • at the end of all cycles, resume the printer once more, and then terminate the process

  11. Program’s flow row=0, column=0 while (numberOfGenerations< maximalNumberOfGenerations) resume(cell (row, column)) if (time to resume printer) resume (printer) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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) row=(row+1)%maximalNumberOfRows if(row==0) column=(column+1)%maximalNumberOfColumns 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 • Invoked using: ass3 <filename> <length> <width> <t> <K> • where: • <filename> - name of a file contain the initial state of the game board. A series of size <width> of ‘ ‘ and ‘1’ in each line. The file contain <length> lines. • <t> - number of generations, i.e. amount of scheduler cycles through each cell • <K> - 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, K • Initiate all co-routines: • each cell gets parameters i,j - its indices in the global array • a scheduler needs to get number of generation (t) as a parameter • initiate the printer • Initiate an array CORSof pointers to: cell0,0,…,celllength-1,width-1, scheduler, printer  • Transfer control to scheduler

  35. Program’s flow In your implementation of co-routines each cell in CORS array will point directly to the top of the stack of the corresponded co-routine. You don’t have to implement the co-routine structure as presented in the practical session. CORS: SP(0,0) SP(0,1) … SP(n-1, n-1) CORS: CO(0,0) CO(0,1) … CO(n-1, n-1) CO(i,j): Function Flags SP(i,j)

  36. Example Let’s run an example using your simple scheduler: ass3 inputFile 10 10 99 7 alive cells are denoted by ‘1’ in input file empty (dead) cells are denoted by space in input file

  37. Example code • Try to run life.c or lifeG.c that simulate game of life with the same parameters as your assembly program. • For lifeG.c you have to include openGL libraries: gcc lifeG.c –lGL –lGLU –lglut –o life

  38. Technicalities • Submit a single .zip file, using the same directory structure as in previous assignments. • src folder will include: • ass3.s (with most of the code) • printer.s (printer function code) • scheduler.s (scheduler function code)

  39. שאלות חזרה למבחן

  40. שאלה 1 עלינו לממש את קטע הקוד הבא: int a, b, x; x = blah(a,&b) מהו קטע הקוד שיבצע זאת נכון ? a) push a c) push dword b push b push dword [a] call blah call blah add esp, 8 add esp, 8 mov [x], eax mov [x], eax b) push dword [b] d) push dword [b] push dword a push dword a call blah call blah add esp, 8 add esp, 8 mov [x], eax pop dword [x]

  41. שאלה 1 עלינו לממש את קטע הקוד הבא: int a, b, x; x = blah(a,&b) מהו קטע הקוד שיבצע זאת נכון ? a) push a c) push dword b push b push dword [a] call blah call blah add esp, 8 add esp, 8 mov [x], eax mov [x], eax b) push dword [b] d) push dword [b] push dword a push dword a call blah call blah add esp, 8 add esp, 8 mov [x], eax pop dword [x]

  42. שאלה 2 איזה מקטעי הקוד הבאים יעבוד כ-Position Independent Code : bla: db ‘tab\0’push blacall printf mov eax, 4mov ebx, 1mov ecx, dword [bla]int 0x80 moveax, ‘bla\0’call printf None of the above

  43. שאלה 3 איזה מקטעי הקוד הבאים יעבוד כ-Position Independent Code : bla: db ‘tab\0’push blacall printf mov eax, 4mov ebx, 1mov ecx, dword [bla]int 0x80 moveax, ‘bla\0’call printf None of the above

  44. שאלה 4 עלינו לפנות מהזיכרון רשימה מקושרת ע"י שחרור כל האיברים שלה (בעזרת פונקציית free של C). ה-dword הראשון בכל רשומה הוא מצביע לרשומה הבאה. מצביע שערכו 0 הוא null pointer. נניח כי אוגר ecx מצביע לאיבר הראשון ברשימה. יש לממש את הקוד הנדרש.

  45. שאלה 4 עלינו לפנות מהזיכרון רשימה מקושרת ע"י שחרור כל האיברים שלה (בעזרת פונקציית free של C). ה-dword הראשון בכל רשומה הוא מצביע לרשומה הבאה. מצביע שערכו 0 הוא null pointer. נניח כי אוגר ecx מצביע לאיבר הראשון ברשימה. יש לממש את הקוד הנדרש. תשובה free_list: cmp ecx, 0 jz end push [ecx] ; backup for the next pointer push ecx call free add esp, 4 pop ecx ; restore for the next pointer jmp free_list end: ret

  46. שאלה 5 נתונות ההגדרות הבאות בייצוג Little Endian: x: db 10y: db 3 מה יהיה ערכו (בייצוג הקסא-דצימלי) של הרגיסטר BX לאחר הפקודה: mov bx, [x]

  47. שאלה 5 נתונות ההגדרות הבאות בייצוג Little Endian: x: db 10y: db 3 מה יהיה ערכו (בייצוג הקסא-דצימלי) של הרגיסטר BX לאחר הפקודה: mov bx, [x] תשובה bx = 0x30A 0000 0011 0000 1010 0x0 0x3 0x0 0xA 0x30A

  48. שאלה 6 • ברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך eax פי 3. מוצעות 2 אפשרויות: שימוש במקרו triple או קריאה לפונקציה Triple: • %macro triple 0 mov ebx, eax add eax, eax add eax, ebx %endmacro • Triple: mov ebx, eax add eax, eax add eax, ebx ret א) בזמן ריצה ל-2 האפשרויות אותו זמן ביצוע. ב) השימוש ב- macro מהיר יותר, אבל דורש יותר זיכרון לקוד. ג) השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד. ד) הפונקציה Triple לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית

  49. שאלה 6 • ברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך eax פי 3. מוצעות 2 אפשרויות: שימוש במקרו triple או קריאה לפונקציה Triple: • %macro triple 0 mov ebx, eax add eax, eax add eax, ebx %endmacro • Triple: mov ebx, eax add eax, eax add eax, ebx ret א) בזמן ריצה ל-2 האפשרויות אותו זמן ביצוע. ב) השימוש ב- macro מהיר יותר, אבל דורש יותר זיכרון לקוד. ג) השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד. ד) הפונקציה Triple לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית

  50. שאלה 7 עלינו להחליף בין ערכי המשתנים L ו- L1 המוגדרים בצורה הבאה: L: dw 7 L1: dw 0xF7AC אילו מקטעי הקוד הבאים לא יבצע את זאת כנדרש? • mov ax, [L] c) mov eax, [L1] mov bx, [L1] rol eax, 16 mov [L1], ax mov [L1], eax mov [L], bx • mov eax, [L] d) mov eax, [L] rol eax, 16 xor ax, [L1] mov [L], eax xor [L], ax xor [L1], ax

More Related