53
Practical session 9 Assignment 3

Practical session 9

  • Upload
    jaguar

  • View
    25

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Practical session 9

Practical session 9

Assignment 3

Page 2: Practical session 9

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.

Page 3: Practical session 9

A Cell• Just to be perfectly clear:

Each cell (organism) has 8 neighbors.

N1 N2 N3

N4 meme N5

N6 N7 N8

Page 4: Practical session 9

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

Page 5: Practical session 9

Game rules1. If the cell is currently alivealive, then it will remain

alive in the next generation if and only if if and only if exactly 2 or 3 of its neighbors are exactly 2 or 3 of its neighbors are currently alivealive. Otherwise it dies.

2. A deaddead cell remains dead in the next generation, unless it has exactly 3 living unless it has exactly 3 living neighborsneighbors.

3. Organism age is the number of generation it was alive in a row. Maximum age is 9*.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.

Page 6: Practical session 9

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.

1 11 1

1 1 11 1 1

1 1 11 1

empty (dead) cells are denoted by space in input file

alive cells are denoted by ‘1’ in input file

Page 7: Practical session 9

Implementation• Using the co-routine mechanism, we’ll write a

small simulator for the game.

• Running co-routines:–n n cell instances cell instances (2-dimensional matrix)

–a printera printer–a schedulera scheduler

Page 8: Practical session 9

A Cell

• Can be alive(‘1’-’9’) or dead(‘ ‘)• Cell executes a simple infinite loopinfinite loop:

1. Calculate next state using current state (of “me” and my neighbors).

2. Update current state of “me” to new state

• After each of these two stages, the cellcell co-routine must resume the schedulermust resume the scheduler.

In other words, it loops (forever) over:(1)(1) resume resume (2) (2) resumeresume

Page 9: Practical session 9

The printer

• Simply printsprints the entire “world” (the global the global arrayarray), whenever it gets “time”.

Page 10: Practical session 9

The scheduler• You are to implement a simple round-robin round-robin

schedulerscheduler• void scheduler (int cycles)

– iterate cycles times over all cells– after each after each KK resumes of cell co-routines, resume the resumes of cell co-routines, resume the

printer printer (1) resume (2) resume …– at the end of all cycles, resume the printer once

more, and then terminate the process

Page 11: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 12: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 13: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 14: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 15: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 16: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 17: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 18: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 19: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 20: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 21: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 22: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 23: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 24: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 25: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 26: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 27: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 28: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 29: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 30: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Cell (0,1) Cell (n-1, n-1)…

Page 31: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0)Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

print matrix

Cell (0,1) Cell (n-1, n-1)…

Page 32: Practical session 9

Program’s flowProgram’s flowrow=0, column=0while (numberOfGenerations < maximalNumberOfGenerations)

resume(cell (row, column))resume(cell (row, column))if (time to resume printer)

resume (printer)resume (printer)row=(row+1)%maximalNumberOfRowsif(row==0)

column=(column+1)%maximalNumberOfColumnsif (row==0 && column==0)

numberOfGenerations+=0.5

Scheduler Cell (0,0) Cell (0,1) Cell (n-1, n-1)…Printer

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

calculate stage

update matrix

Page 33: Practical session 9

Program’s flow• Invoked using:

ass3 <filename> ass3 <filename> <length> <width><length> <width> <t> <K> <t> <K>• where:

–<filenamefilename> - name of a file contain the initial state initial state of the game board. A series of size <width> of ‘ ‘ and ‘1’ in each line. The file contain <length> lines. –<tt> - number of generationsnumber of generations, i.e. amount of scheduler cycles through each cell–<KK> - printing frequencyprinting frequency–your array dimensions array dimensions will be <width>*<length><width>*<length>

Page 34: Practical session 9

Program’s flow• When invoked, and using the co-routines mechanism from class,

your application will:

– Set up: a Set up: a state arraystate array, , Length, WidthLength, Width, , KK

– Initiate all co-routinesInitiate 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 Initiate an array CORSCORS of pointers to: : cellcell0,00,0,…,cell,…,celllength-1,width-1length-1,width-1, scheduler, printer , scheduler, printer

– Transfer control to schedulerTransfer control to scheduler

Page 35: Practical session 9

Program’s flowIn 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): FunctionFlagsSP(i,j)

Page 36: Practical session 9

Example

Let’s run an example using your simple scheduler:

ass3 inputFile 10 10 99 7

1 11 1

1 1 11 1 1

1 1 11 1

empty (dead) cells are denoted by space in input file

alive cells are denoted by ‘1’ in input file

Page 37: Practical session 9

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

Page 38: Practical session 9

Technicalities• Submit a single .zip .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)

Page 39: Practical session 9

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

Page 40: Practical session 9

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]

Page 41: Practical session 9

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]

Page 42: Practical session 9

2שאלה

:Position Independent Codeאיזה מקטעי הקוד הבאים יעבוד כ-

a)bla: db ‘tab\0’push blacall printfb)mov eax, 4mov ebx, 1mov ecx, dword [bla]int 0x80c)mov eax, ‘bla\0’call printfd)None of the above

Page 43: Practical session 9

3שאלה

: Position Independent Code-איזה מקטעי הקוד הבאים יעבוד כ

a)bla: db ‘tab\0’push blacall printfb)mov eax, 4mov ebx, 1mov ecx, dword [bla]int 0x80c)mov eax, ‘bla\0’call printfd)None of the above

Page 44: Practical session 9

4שאלה ע"י שחרור כל האיברים רשימה מקושרת עלינו לפנות מהזיכרון

בכל הראשון dword(. ה-C של freeשלה )בעזרת פונקציית null הוא 0רשומה הוא מצביע לרשומה הבאה. מצביע שערכו

pointer נניח כי אוגר .ecx ברשימה. יש מצביע לאיבר הראשוןלממש את הקוד הנדרש.

Page 45: Practical session 9

4שאלה ע"י שחרור כל האיברים רשימה מקושרת עלינו לפנות מהזיכרון

בכל הראשון dword(. ה-C של freeשלה )בעזרת פונקציית null הוא 0רשומה הוא מצביע לרשומה הבאה. מצביע שערכו

pointer נניח כי אוגר .ecx ברשימה. יש מצביע לאיבר הראשוןלממש את הקוד הנדרש.

תשובהfree_list:

cmp ecx, 0jz endpush [ecx] ; backup for the next pointerpush ecxcall freeadd esp, 4 pop ecx ; restore for the next pointerjmp free_list

end:ret

Page 46: Practical session 9

5שאלה

:Little Endianנתונות ההגדרות הבאות בייצוג x: db 10y: db 3

מה יהיה ערכו )בייצוג הקסא-דצימלי( של לאחר הפקודה:BXהרגיסטר

mov bx, [x]

Page 47: Practical session 9

5שאלה

:Little Endian נתונות ההגדרות הבאות בייצוגx: db 10y: db 3

מה יהיה ערכו )בייצוג הקסא-דצימלי( של:לאחר הפקודה BX הרגיסטרmov bx, [x]

תשובהbx = 0x30A

0000 0011 0000 1010

0x0 0x3 0x0 0xA

0x30A

Page 48: Practical session 9

6שאלה 2. מוצעות 3 פי eaxברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך •

: Triple או קריאה לפונקציה tripleאפשרויות: שימוש במקרו

• %macro triple 0mov ebx, eaxadd eax, eaxadd eax, ebx

%endmacro• Triple: mov ebx, eax

add eax, eax add eax, ebx

ret האפשרויות אותו זמן ביצוע.2א( בזמן ריצה ל-

מהיר יותר, אבל דורש יותר זיכרון לקוד.macroב( השימוש ב-

ג( השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד.

לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסניתTripleד( הפונקציה

Page 49: Practical session 9

6שאלה 2. מוצעות 3 פי eaxברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך •

: Triple או קריאה לפונקציה tripleאפשרויות: שימוש במקרו

• %macro triple 0mov ebx, eaxadd eax, eaxadd eax, ebx

%endmacro• Triple: mov ebx, eax

add eax, eax add eax, ebx

ret האפשרויות אותו זמן ביצוע.2א( בזמן ריצה ל-

מהיר יותר, אבל דורש יותר זיכרון לקוד.macroב( השימוש ב-

ג( השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד.

לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסניתTripleד( הפונקציה

Page 50: Practical session 9

7שאלה

המוגדרים בצורה הבאה:L1 ו- Lעלינו להחליף בין ערכי המשתנים

L: dw 7L1: dw 0xF7AC

יבצע את זאת כנדרש?לאאילו מקטעי הקוד הבאים

a) mov ax, [L] c) mov eax, [L1] mov bx, [L1] rol eax, 16

mov [L1], ax mov [L1], eaxmov [L], bx

b) mov eax, [L] d) mov eax, [L]rol eax, 16 xor ax, [L1]mov [L], eax xor [L], ax

xor [L1], ax

Page 51: Practical session 9

7שאלה

המוגדרים בצורה הבאה:L1 ו- Lינו להחליף בין ערכי המשתנים על

L: dw 7L1: dw 0xF7AC

יבצע את זאת כנדרשלאאילו מקטעי הקוד הבאים

a) mov ax, [L] c) mov eax, [L1] mov bx, [L1] rol eax, 16

mov [L1], ax mov [L1], eaxmov [L], bx

b) mov eax, [L] d) mov eax, [L]rol eax, 16 xor ax, [L1]mov [L], eax xor [L], ax

xor [L1], ax

Page 52: Practical session 9

8שאלה נתון מבנה נתונים המוגדר באופן הבא:

bla: dd bla + 8, bla + 16, bla + 24, bla + 32, bla + 24, 0, 0, 0, 0, 0:Fooכמו כן נתון הקוד של פונקציה

Foo: cmp ebx, 0jz Endinc ecxpush ebxmov ebx, dword [ebx + 4]call Foopop ebxmov ebx, dword [ebx]call Foo

End: ret bla לפני הקריאה היה ebx כאשר ערכו של Foo לאחר קריאה ל-ecxמה יהיה ערכו של 1.

?0 היה ecxוערכו של לפני הקריאה היה ebx כאשר ערכו של Foo לאחר קריאה ל-ecxמה יהיה ערכו של 2.

bla+4 וערכו של ecx 0 היה?

Page 53: Practical session 9

2 שונים. נשים לב כי ישנם EBX עבור ערכי incנספור את פעולות ה- תשובה יחושב עבורם incקריאות רקורסיביות. לכן, נדאג לכך שמספר פעולות ה-

מוקדם יותר )כלומר נבצע את הסכימה מ"למטה למעלה"( ונוסיף לכך פעולת inc:אחת המתבצעת בפונקציה, נמשיך באופן זה עד שנגיע לבעיה המקורית

foo(EBX=0) = 0 ; stop conditionfoo(EBX=bla+X) = 1 where X>=20foo(EBX=bla+16) = foo(EBX=0) + foo(EBX=bla+24) + 1 = 2foo(EBX=bla+12) = foo(EBX=bla+24) + foo(EBX=bla+32) + 1 = 3foo(EBX=bla+8) = foo(EBX=bla+32) + foo(EBX=bla+24) + 1 = 3foo(EBX=bla+4) = foo(EBX=bla+24) + foo(EBX=bla+16) + 1 = 5foo(EBX=bla) = foo(EBX=bla+16) + foo(EBX=bla+8) + 1 = 6

.ecx=5 ועבור סעיף שני ecx=6 מכאן שעבור סעיף ראשון