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 Simulates Evolution of 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 state and its neighbors’ states.

3 Input file Each cell is given an initial state from input file.
1 alive cells are denoted by ‘1’ in input file empty (dead) cells are denoted by space 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 1 2 1 calculate next state update next state

4 Cell neighbors Every cell has 8 neighbors Even at the board edges me
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 is currently alive, then it will remain alive in the next generation if and only if exactly 2 or 3 of its neighbors are currently alive. Otherwise it dies. Examples: N3 N2 N1 N5 me N4 N8 N7 N6 N3 N2 N1 N5 me N4 N8 N7 N6 me dies me stays alive N3 N2 N1 N5 me N4 N8 N7 N6 A dead cell remains dead in the next generation, unless it has exactly 3 living neighbors. Examples: N3 N2 N1 N5 me N4 N8 N7 N6 N3 N2 N1 N5 me N4 N8 N7 N6 me comes alive me stays dead 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 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

7 (stage 1) resume  (stage 2) resume
A Cell Cell can be alive(‘1’-’9’) or dead(‘ ‘) 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

8 A scheduler The printer implements simple round-robin algorithm
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 … 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”.

9 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

10 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

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) 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

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 > ass3 <filename> <length> <width> <t> <K> <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 <K> - printing frequency your array dimensions will be <width>*<length>

32 Program’s flow When invoked, and using the co-routines mechanism from class, your application will: Set up: a state array, Length, Width, K, t Initiate all co-routines: each cell gets parameters i,j - its indices in the global array a scheduler gets t, K, 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

33 Program’s flow 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: CO(0,0) CO(0,1) CO(n-1, n-1) CoPrinter CoScheduler CO(i,j): Function Flags SP(i,j) CORS: SP(0,0) SP(0,1) SP(n-1, n-1) SP_CoPrinter SP_CoScheduler For lifeG.c you have to include openGL libraries: gcc lifeG.c –lGL –lGLU –lglut –o life

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

35 שאלה 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 mov [x], eax pop dword [x]

36 שאלה 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 mov [x], eax pop dword [x]

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

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

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

40 שאלה 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

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

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

43 שאלה 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 לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית

44 שאלה 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 לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית

45 שאלה 7 עלינו להחליף בין ערכי המשתנים L ו- L1 המוגדרים בצורה הבאה:
שאלה 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

46 שאלה 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

47 שאלה 8 נתון מבנה נתונים המוגדר באופן הבא:
bla: dd bla + 8, bla + 16, bla + 24, bla + 32, bla + 24, 0, 0, 0, 0, 0 כמו כן נתון הקוד של פונקציה Foo: Foo: cmp ebx, 0 jz End inc ecx push ebx mov ebx, dword [ebx + 4] call Foo pop ebx mov ebx, dword [ebx] call Foo End: ret מה יהיה ערכו של ecx לאחר קריאה ל-Foo כאשר ערכו של ebx לפני הקריאה היה bla וערכו של ecx היה 0? מה יהיה ערכו של ecx לאחר קריאה ל-Foo כאשר ערכו של ebx לפני הקריאה היה bla+4 וערכו של ecx היה 0?

48 תשובה נספור את פעולות ה- inc עבור ערכי EBX שונים
תשובה נספור את פעולות ה- inc עבור ערכי EBX שונים. נשים לב כי ישנם 2 קריאות רקורסיביות. לכן, נדאג לכך שמספר פעולות ה- inc יחושב עבורם מוקדם יותר (כלומר נבצע את הסכימה מ"למטה למעלה") ונוסיף לכך פעולת inc אחת המתבצעת בפונקציה, נמשיך באופן זה עד שנגיע לבעיה המקורית: foo(EBX=0) = 0 ; stop condition foo(EBX=bla+X) = 1 where X>=20 foo(EBX=bla+16) = foo(EBX=0) + foo(EBX=bla+24) + 1 = 2 foo(EBX=bla+12) = foo(EBX=bla+24) + foo(EBX=bla+32) + 1 = 3 foo(EBX=bla+8) = foo(EBX=bla+32) + foo(EBX=bla+24) + 1 = 3 foo(EBX=bla+4) = foo(EBX=bla+24) + foo(EBX=bla+16) + 1 = 5 foo(EBX=bla) = foo(EBX=bla+16) + foo(EBX=bla+8) + 1 = 6 מכאן שעבור סעיף ראשון ecx=6 ועבור סעיף שני ecx=5.


Download ppt "Computer Architecture and Assembly Language"

Similar presentations


Ads by Google