Computer Architecture and Assembly Language Practical Session 13 שאלות חזרה למבחן
שאלה 1 ADD dword [x], 0x10001 כמה גישות לזיכרון, במקרה הגרוע, אורך ה- op-code הוא 2 בתים? דרושות לביצוע הפקודה אם ידוע כי פתרון: Fetch אורך הקוד הבינארי 10 בתים 2 עבור,opcode 4 עבור הכתובת,x ועוד 4 עבור מהספר המיידי. קריאתם מהזיכרון דורשת 4 גישות לזיכרון לכל היותר Read יש לקרוא 4 בתים החל מהכתובת --< 2 גישות לזיכרון לכל היותר Write יש לכתוב 4 בתים, וזה דורש 2 גישות לזכרון במקרה הגרוע. בסה"כ 8 גישות לזיכרון
שאלה 2 נתונה הגדרת macro הבאה, וכן נתונים בזכרון: %macro print 3 pusha mov eax, 4 ; write mov, %1 ; file descriptor mov ecx, %2 ; address mov edx, %3 ; byte count int 0x80 popa %endmacro section.rodata File: dd 1 MJ: db Beat it, 10, 0 איזה מהשימושים הבאים במקרו יגרום לפעולה לא נכונה של התוכנית: a) mov, MJ print 1,, 9 b) print 1, MJ, 9 c) print dword [File], MJ, 9 d) mov edx, 9 print 1, MJ, edx
שאלה 2 נתונה הגדרת macro הבאה, וכן נתונים בזכרון: %macro print 3 pusha mov eax, 4 ; write mov, %1 ; file descriptor mov ecx, %2 ; address mov edx, %3 ; byte count int 0x80 popa %endmacro section.rodata File: dd 1 MJ: db Beat it, 10, 0 איזה מהשימושים הבאים במקרו יגרום לפעולה לא נכונה של התוכנית: a) mov, MJ print 1,, 9 b) print 1, MJ, 9 c) print dword [File], MJ, 9 d) mov edx, 9 print 1, MJ, edx
שאלה 3? 0 ל- )3 מה תחזיר הפונקציה Gloat ברגיסטר )עבור בין 2 2 בחזקת 2 ב( א( 0 בחזקת ג( כפול ד( Gloat: shl, 2 jmp [+Tab] Tab: dd F4 dd F3 dd F2 dd F1 F1: add, 4 F2: add, 4 F3: add, 4 F4: shr, 2 ret
שאלה 3? 0 ל- )3 מה תחזיר הפונקציה Gloat ברגיסטר )עבור בין 2 2 בחזקת 2 ב( א( 0 בחזקת ג( כפול ד( Gloat: shl, 2 jmp [+Tab] Tab: dd F4 dd F3 dd F2 dd F1 F1: add, 4 F2: add, 4 F3: add, 4 F4: shr, 2 ret = 0 shl 0,2 = 0 jmp [0+Tab] = jmp F4 shr 0, 2 = 0 = 0 = 2 shl 2,2 = 2*2*2 = 8 jmp [8+Tab] = jmp F2 F2: add 8, 4 = 12 F3: add 12, 4 = 16 shr 16, 2 = 16/2/2 = 4 = 4 = 1 shl 1,2 = 1*2*2 = 4 jmp [4+Tab] = jmp F3 F3: add 4, 4 = 8 shr 8, 2 = 8/2/2 = 2 = 2 = 3 shl 3,2 = 3*2*2 = 12 jmp [12+Tab] = jmp F1 F1: add 12, 4 = 16 F2: add 16, 4 = 20 F3: add 20, 4 = 24 shr 24, 2 = 24/2/2 = 6 = 6
שאלה 3? 0 ל- )3 מה תחזיר הפונקציה Gloat ברגיסטר )עבור בין 2 2 בחזקת 2 ב( א( 0 בחזקת ג( כפול ד( Gloat: shl, 2 jmp [+Tab] Tab: dd F4 dd F3 dd F2 dd F1 F1: add, 4 F2: add, 4 F3: add, 4 F4: shr, 2 ret = 0 shl 0,2 = 0 jmp [0+Tab] = jmp F4 shr 0, 2 = 0 = 0 = 2 shl 2,2 = 2*2*2 = 8 jmp [8+Tab] = jmp F2 F2: add 8, 4 = 12 F3: add 12, 4 = 16 shr 16, 2 = 16/2/2 = 4 = 4 = 1 shl 1,2 = 1*2*2 = 4 jmp [4+Tab] = jmp F3 F3: add 4, 4 = 8 shr 8, 2 = 8/2/2 = 2 = 2 = 3 shl 3,2 = 3*2*2 = 12 jmp [12+Tab] = jmp F1 F1: add 12, 4 = 16 F2: add 16, 4 = 20 F3: add 20, 4 = 24 shr 24, 2 = 24/2/2 = 6 = 6
L: dw 7 L1: dw 0xF7AC a) mov ax, [L] c) mov eax, [L1] mov bx, [L1] rol eax, 16 mov [L1], ax mov [L1], eax mov [L], bx שאלה 4 עלינו להחליף בין ערכי המשתנים L ו- L1 המוגדרים בצורה הבאה: אילו מקטעי הקוד הבאים לא יבצע את זאת כנדרש? RAM b) mov eax, [L] d) mov eax, [L] rol eax, 16 xor ax, [L1] mov [L], eax xor [L], ax xor [L1], ax a) mov ax, [L] ax = 0x00 0x07 mov bx, [L1] bx = 0xF7 0xAC mov [L1], ax [L1] = 0x07, [L1+1] = 0x00 mov [L], bx [L] = 0xAC, [L+1] = 0xF7 0x?? 0x?? 0xF7 0xAC 0x00 0x07 L1 L a) mov eax, [L] eax = 0xF7 0xAC 0x00 0x07 rol eax, 16 eax = 0x00 0x07 0xF7 0xAC mov [L], eax [L] = 0xAC, [L+1] = 0xF7, [L1] = 0x07, [L1+1] = 0x00 a) mov eax, [L1] eax = 0x?? 0x?? 0xF7 0xAC rol eax, 16 eax = 0xF7 0xAC 0x?? 0x?? mov [L1], eax [L1] = 0x??, [L1+1] = 0x?? a) mov eax, [L] eax = 0xF7 0xAC 0x00 0x07 xor ax, [L1] ax = 0x0007 xor 0xF7AC = 0xF7AB xor [L], ax [L] = 0x0007 xor 0xF7AB = 0xF7AC xor [L1], ax [L1] = 0xF7AC xor 0xF7AB = 0x0007
L: dw 7 L1: dw 0xF7AC a) mov ax, [L] c) mov eax, [L1] mov bx, [L1] rol eax, 16 mov [L1], ax mov [L1], eax mov [L], bx שאלה 4 עלינו להחליף בין ערכי המשתנים L ו- L1 המוגדרים בצורה הבאה: אילו מקטעי הקוד הבאים לא יבצע את זאת כנדרש? RAM b) mov eax, [L] d) mov eax, [L] rol eax, 16 xor ax, [L1] mov [L], eax xor [L], ax xor [L1], ax a) mov ax, [L] ax = 0x00 0x07 mov bx, [L1] bx = 0xF7 0xAC mov [L1], ax [L1] = 0x07, [L1+1] = 0x00 mov [L], bx [L] = 0xAC, [L+1] = 0xF7 0x?? 0x?? 0xF7 0xAC 0x00 0x07 L1 L a) mov eax, [L] eax = 0xF7 0xAC 0x00 0x07 rol eax, 16 eax = 0x00 0x07 0xF7 0xAC mov [L], eax [L] = 0xAC, [L+1] = 0xF7, [L1] = 0x07, [L1+1] = 0x00 a) mov eax, [L1] eax = 0x?? 0x?? 0xF7 0xAC rol eax, 16 eax = 0xF7 0xAC 0x?? 0x?? mov [L1], eax [L1] = 0x??, [L1+1] = 0x?? a) mov eax, [L] eax = 0xF7 0xAC 0x00 0x07 xor ax, [L1] ax = 0x0007 xor 0xF7AC = 0xF7AB xor [L], ax [L] = 0x0007 xor 0xF7AB = 0xF7AC xor [L1], ax [L1] = 0xF7AC xor 0xF7AB = 0x0007
שאלה 5 דרוש לבצע חיבור של שני מספרים של BIT 32 על מעבד 68040 )רוחב BUS של.)BIT 32 פקודת חיבור שני אופרנדים בזכרון מותרת במעבד זה. ייצוג המספרים הוא,BIG ENDIAN וכל אופרנד באורך N חייב להתחיל בכתובת המתחלקת ב N.)alignment( המספרים נמצאים בזכרון בכתובת X ו Y, והתוצאה צריכה להיות ב )X Y. הוא משתנה שמשתרע על איזור X_HIGH וגם.)X_LOW איזו מהתוכניות הבאות תוכל לבצע את הנדרש? align 4 A: dw 0 X: X_HIGH: dw 5 ; dw reserves space for 2 byte variable X_LOW: dw 10 JUNK: dw 0 Y: dl 700 ; dl reserves space for 4 byte variable a) ADD.W X, Y ; add word at address X in ; memory into word at address Y ADDX.W X+2, Y+2 ; add word at X+2 in memory into ; word at Y+2 in memory with extend flag (like CF) b) ADD.W X+2, Y+2 ADDX.W X, Y c) MOVE.L X, D0 ; move longword (32 bits) at X to register D0 ADD.L D0, Y ; add D0 to Y d) ADD.L X, Y ; add longword X to Y
a) ADD.W X, Y ; add word at address X in ; memory into word at address Y שאלה 5 דרוש לבצע חיבור של שני מספרים של BIT 32 על מעבד 68040 )רוחב BUS של.)BIT 32 פקודת חיבור שני אופרנדים בזכרון מותרת במעבד זה. ייצוג המספרים הוא,BIG ENDIAN וכל אופרנד באורך N חייב להתחיל בכתובת המתחלקת ב N.)alignment( המספרים נמצאים בזכרון בכתובת X ו Y, והתוצאה צריכה להיות ב )X Y. הוא משתנה שמשתרע על איזור X_HIGH וגם.)X_LOW איזו מהתוכניות הבאות תוכל לבצע את הנדרש? align 4 a) Y_HIGH += X_HIGH b) Y_LOW + = X_LOW d) ADD.L is not legal by the A: dw 0 Y_LOW + = X_LOW + CF Y_HIGH += X_HIGH+ CF same reason as MOVE.L X: X_HIGH: dw 5 ; dw reserves space for 2 byte variable RAM 12 X_LOW: dw 10 0xBC JUNK: dw 0 700 d = 0x2BC 11 0x02 Y: dl 700 ; dl reserves space for 4 byte variable 10 0x00 ADDX.W X+2, Y+2 ; add word at X+2 in memory into ; word at Y+2 in memory with extend flag (like CF) b) ADD.W X+2, Y+2 ADDX.W X, Y c) MOVE.L X, D0 ; move longword (32 bits) at X to register D0 ADD.L D0, Y ; add D0 to Y d) ADD.L X, Y ; add longword X to Y c) MOVE.L is not legal since its X operand is in memory and of size L(longword = 4 bytes), so X should begin in memory at address that %4 = 0. X begins in memory at address with offset 2 from some address that is divided by 4, so X s address is not divided by 4. align 4 move to a closest address in appropriate memory section which is %4 = 0 9 8 7 6 5 4 3 2 1 0 0x00 0 0 0x0A 0x00 0x05 0x00 0 0 y A JUNK X_LOW X X_HIGH
שאלה 6 נתון מבנה נתונים המוגדר באופן הבא: bla: dd bla + 8, bla + 16, bla + 24, bla + 32, bla + 24, 0, 0, 0, 0, 0 כמו כן נתון הקוד של פונקציה :Foo Foo: cmp, 0 jz End inc ecx push mov, dword [ + 4] call Foo pop mov, dword [] call Foo End: ret bla bla+4 מה יהיה ערכו של ecx וערכו של ecx היה 0? מה יהיה ערכו של ecx וערכו של ecx היה 0? לאחר קריאה ל- Foo לאחר קריאה ל- Foo כאשר ערכו של כאשר ערכו של לפני הקריאה היה לפני הקריאה היה.1.2
שאלה 6 תשובה נספור את פעולות ה- 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=5 מכאן שעבור סעיף ראשון ecx=6 ועבור סעיף שני
שאלה 7 ברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך eax פי 3. מוצעות 2 אפשרויות: שימוש במקרו triple או קריאה לפונקציה :Triple %macro triple 0 mov, eax add eax, eax add eax, %endmacro Triple: mov, eax add eax, eax add eax, ret א( בזמן ריצה ל- 2 האפשרויות אותו זמן ביצוע. ב( השימוש ב- macro מהיר יותר, אבל דורש יותר זיכרון לקוד. ג( השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד. ד( הפונקציה Triple לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית
שאלה 7 ברצוננו לכתוב קוד לשימוש רב-פעמי, שמכפיל את ערך eax פי 3. מוצעות 2 אפשרויות: שימוש במקרו triple או קריאה לפונקציה :Triple %macro triple 0 mov, eax add eax, eax add eax, %endmacro Triple: mov, eax add eax, eax add eax, ret א( בזמן ריצה ל- 2 האפשרויות אותו זמן ביצוע. ב( השימוש ב- macro מהיר יותר, אבל דורש יותר זיכרון לקוד. ג( השימוש בפונקציה מהיר יותר, אבל דורש יותר זיכרון לקוד. ד( הפונקציה Triple לא יכולה לעבוד, כי היא לא מוציאה משתנים מהמחסנית