Presentation is loading. Please wait.

Presentation is loading. Please wait.

תרגול 4 Assembly. – 2 – text binary Compiler ( gcc -S ) Assembler ( gcc or as ) Linker ( gcc or ld ) C program ( p1.c p2.c ) Asm program ( p1.s p2.s )

Similar presentations


Presentation on theme: "תרגול 4 Assembly. – 2 – text binary Compiler ( gcc -S ) Assembler ( gcc or as ) Linker ( gcc or ld ) C program ( p1.c p2.c ) Asm program ( p1.s p2.s )"— Presentation transcript:

1 תרגול 4 Assembly

2 – 2 – text binary Compiler ( gcc -S ) Assembler ( gcc or as ) Linker ( gcc or ld ) C program ( p1.c p2.c ) Asm program ( p1.s p2.s ) Object program ( p1.o p2.o ) Executable program ( p ) Static libraries (.a ) תזכורת זה: מתוכנית ב C לתוכנית ברת הרצה נניח שהתוכנית נמצאת בקבצים p1.c p2.c נהדר עם : gcc -O p1.c p2.c -o p (-O אופטימיזציות בסיסיות, -o בקובץ p בינארי )

3 – 3 – שיטות מיעון מורכבות

4 – 4 – תרגיל 1 כתובתערך 0x1000xFF 0x1040xAB 0x1080x13 0x10C0x14 רגיסטרערך R10x100 R20x1 R30x3 זכרוןCPU כתובתערך R10x100 0x1040xAB $0x108 (R1)0xFF 4(R1)0xAB 9(R1,R3)0x14 260(R2,R3)0x13 0xFC(,R2,4)0xFF (R1,R3,4)0x14

5 – 5 – שילובים אפשריים ב move move $0x4,R1 move $-147,(R1) move R1,R2 move R1,(R2) move (R1),R2 איך זה נראה ב C temp = 0x4; *p = -147; temp2 = temp1; *p = temp; temp = *p; move constant register move constant memory move register register move register memory move memory register תבניתדוגמה  movb (move byte)  movw (move word)  movl (move double word)

6 – 6 – תרגיל 2 פקודות הזזה כתובתערך 0x1000xFF 0x1040xAB 0x1080x13 0x10C0x14 רגיסטרערך R10x100 R20x4 R60x100 R80x120 R70x10C זכרוןCPU מה יהיה הערך ביעד אחרי כל פקודה? R1=0x4050 R7=0x120 R1=0xAB M[0x120]=0xFFFFFFEF M[0x114]=0x100 R1 R8R7 R6R2R1 R7 R6R8 move

7 – 7 – תרגיל 3 כתובתערך 0x1000xFF 0x1040xAB 0x1080x13 0x10C0x14 רגיסטרערך R10x100 R20x1 R30x3 זכרוןCPU פקודה ערך יעד לפניערך אחרי addaddl R2,(R1)0xFF0x100 subtractsubl R3,4(R1)0xAB0xA8 multiply imull $16,(R1,R3,4)0x140x140 incrementincl 8(R1)0x130x14 decrementdecl R20x10x0 subtractsubl R3,R10x1000xFD

8 – 8 – תרגיל 4  מימוש bubble sort (לא מימוש אופטימלי): void BubbleSort(int* arr, int size) { for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) { for (int j = 0; j < size – i - 1;++j) { for (int j = 0; j < size – i - 1;++j) { if (arr[j] > arr[j + 1]) { if (arr[j] > arr[j + 1]) { int tmp = arr[j]; int tmp = arr[j]; arr[j] = arr[j + 1]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; arr[j + 1] = tmp; } } }}

9 – 9 – תרגיל 4 – Assembly code.LCFI3: movl 8(%ebp), %esi pushl %ebx.LCFI4: movl 12(%ebp), %ebx testl %ebx, %ebx jle.L11.L4: leal -1(%ebx), %edi movl $1, %eax testl %edi, %edi jle.L9.p2align 4,,7.L6: movl -4(%esi,%eax,4), %ecx movl (%esi,%eax,4), %edx cmpl %edx, %ecx jle.L7 movl %edx, -4(%esi,%eax,4) movl %ecx, (%esi,%eax,4).L7: addl $1, %eax cmpl %eax, %ebx jne.L6.L9: testl %edi, %edi je.L11 movl %edi, %ebx jmp.L4.L11: popl %ebx … וכולי … ret

10 – 10 – תרגיל 4 – Assembly code.LCFI3: movl 8(%ebp), R4 pushl R2.LCFI4: movl 12(%ebp), R2 testl R2, R2 jle.L11.L4: leal -1(R2), R3 movl $1, R1 testl R3, R3 jle.L9.p2align 4,,7.L6: movl -4(R4,R1,4), R5 movl (R4,R1,4), R6 cmpl R6, R5 jle.L7 movl R6, -4(R4,R1,4) movl R5, (R4,R1,4).L7: addl $1, R1 cmpl R1, R2 jne.L6.L9: testl R3, R3 je.L11 movl R3, R2 jmp.L4.L11: popl R2 … וכולי … ret

11 – 11 – תרגיל 4 – Assembly code.LCFI3: movl 8(%ebp), R4 pushl R2.LCFI4: movl 12(%ebp), R2 testl R2, R2 jle.L11.L4: leal -1(R2), R3 movl $1, R1 testl R3, R3 jle.L9.p2align 4,,7.L6: movl -4(R4,R1,4), R5 movl (R4,R1,4), R6 cmpl R6, R5 jle.L7 movl R6, -4(R4,R1,4) movl R5, (R4,R1,4).L7: addl $1, R1 cmpl R1, R2 jne.L6.L9: testl R3, R3 je.L11 movl R3, R2 jmp.L4.L11: popl R2 … וכולי … ret קח מהמחסנית את גודל המערך R2 = size cmp R2,0 if (size >= 0) קח מהמחסנית את כתובת המערך R3 = R2 - 1 R1 (j) = 1 cmp R3,0 (size – 1 == 0) R5 == arr[j] R6 == arr[j+1] if (arr[j] > arr[j+1]) arr[j+1] = arr[j] arr[j] = (old)arr[j+1] ++j if (j < size – i – 1)


Download ppt "תרגול 4 Assembly. – 2 – text binary Compiler ( gcc -S ) Assembler ( gcc or as ) Linker ( gcc or ld ) C program ( p1.c p2.c ) Asm program ( p1.s p2.s )"

Similar presentations


Ads by Google