תרגול 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 )

Slides:



Advertisements
Similar presentations
Code Optimization and Performance Chapter 5 CS 105 Tour of the Black Holes of Computing.
Advertisements

Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Smashing the Stack for Fun and Profit
Fabián E. Bustamante, Spring 2007 Machine-Level Programming – Introduction Today Assembly programmer’s exec model Accessing information Arithmetic operations.
Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Accessing parameters from the stack and calling functions.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Y86 Processor State Program Registers
Assembly Questions תרגול 12.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Practical Session 4 Computer Architecture and Assembly Language.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Assembly Language: Function Calls Jennifer Rexford.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Precept 7: Introduction to IA-32 Assembly Language Programming
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
Instruction Set Architecture
Assembly Language Programming IV: shift, struct, recursion
Credits and Disclaimers
C function call conventions and the stack
Conditional Branch Example
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
Homework In-line Assembly Code Machine Language
Computer Architecture
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming II: Arithmetic & Control
Machine-Level Programming II Control Flow Sept. 9, 1999
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Machine-Level Programming 1 Introduction
Computer Architecture adapted by Jason Fritts then by David Ferry
asum.ys A Y86 Programming Example
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Condition Codes Single Bit Registers
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Machine-Level Programming 2 Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
Machine-Level Programming I: Introduction Feb. 1, 2000
Machine-Level Programming I: Introduction
X86 Assembly - Data.
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
C structures and Compilation to IA32
X86 Assembly - Control.
02/02/10 20:53 Assembly Questions תרגול 12 1.
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

תרגול 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 ) 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 – שיטות מיעון מורכבות

– 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 – שילובים אפשריים ב 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 – תרגיל 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 – תרגיל 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 – תרגיל 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 – תרגיל 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 – תרגיל 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 – תרגיל 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)