Machine-Level Representation of Programs III

Slides:



Advertisements
Similar presentations
Fabián E. Bustamante, Spring 2007 Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures.
Advertisements

Compiladores I- 1 - COMPARACION PROGRAMA FUENTE Y ENSAMBLADOR GENERADO.
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.
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
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
1 Machine-Level Representation of Programs Ⅱ. 2 Outline Data movement Data manipulation Control structure Suggested reading –Chap 3.4, 3.5, 3.6.
University of Washington x86 Programming III The Hardware/Software Interface CSE351 Winter 2013.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
1 Carnegie Mellon Machine-Level Programming II: Arithmetic and Control Lecture, Feb. 28, 2012 These slides are from website which.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
תרגול 5 תכנות באסמבלי, המשך
University of Washington x86 Programming II The Hardware/Software Interface CSE351 Winter 2013.
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 II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of loops Switch statements CS 105.
Carnegie Mellon Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012 Carnegie Mellon.
Assembly תרגול 7 תכנות באסמבלי, המשך. Condition Codes Single bit registers  CF – carry flag  ZF – zero flag  SF – sign flag  OF – overflow flag Relevant.
Machine-Level Programming 2 Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements.
Machine-Level Programming II: Control Flow Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch Statements class06.ppt.
CPSC 121: Models of Computation
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
X86 Assembly, and C-to-assembly
Conditional Branch Example
Homework Reading Labs PAL, pp
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
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
Machine-Level Programming II: Control
Chapter 3 Machine-Level Representation of Programs
Machine-Level Representation of Programs II
Computer Architecture adapted by Jason Fritts then by David Ferry
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Assembly Language: IA-32 Instructions
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming II: Control Flow
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Programming 2 Control Flow
Homework Reading Machine Projects Labs PAL, pp
Machine-Level Programming: Introduction
Machine-Level Representation of Programs II
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming II: Control Flow
X86 Assembly Review.
X86 Assembly - Control.
Machine-Level Programming II: Control Flow Sept. 12, 2007
CS201- Lecture 8 IA32 Flow Control
Presentation transcript:

Machine-Level Representation of Programs III

Translate Control constructs in C to assembly Outline Jump instructions Translate Control constructs in C to assembly goto branch loop Suggested reading Chap 3.6

Two of the most important parts of program execution Control Two of the most important parts of program execution Data flow (Accessing and operating data) Control flow (control the sequence of operations)

Sequential execution is default Control Sequential execution is default the instructions are executed in the order they appear in the program Chang the control flow Jump instructions

Jump Instructions 1 xorl %eax, %eax Set %eax to 0 2 jmp .L1 Goto .L1 3 movl (%eax), %edx Null pointer dereference 4 .L1: 5 popl %edx

Jumps unconditionally Direct jump: jmp label Unconditional jump Jumps unconditionally Direct jump: jmp label jmp .L Indirect jump: jmp *Operand jmp *%eax jmp *(%eax)

Conditional jump Either jump or continue executing at the next instruction in the code sequence Depending on some combination of the condition codes All direct jump

Jump Instructions jle .L2 .L5: movl %edx, %eax sarl $1, %eax subl %eax, %edx Leal (%edx, %edx, 2), %edx testl %edx, %edx jg .L5 9 .L2: 10 movl %edx, %eax

Example for Jump 8: 7e 0d jle 17<silly+0x17> a: 89 d0 mov %edx, %eax dest1: 3 c: c1 f8 sar %eax e: 29 c2 sub %eax, %edx 10: 8d 14 52 lea (%edx, %edx, 2), %edx 6 13: 85 d2 test %edx, %edx 15: 7f f3 jg a<silly+0x10> 17: 89 d0 movl %edx, %eax dest2: d+a = 17 17+f3(-d) =a

Example for Jump 804839c: 7e 0d jle 17<silly+0x17> 804839e: 89 d0 mov %edx, %eax dest1: 3 80483a0: c1 f8 sar %eax 80483a2: 29 c2 sub %eax, %edx 80483a4: 8d 14 52 lea (%edx, %edx, 2), %edx 6 80483a7: 85 d2 test %edx, %edx 80483a9: 7f f3 jg a<silly+0x10> 80483ab: 89 d0 movl %edx, %eax dest2: d+804849e = 80483ab 80483ab+f3(-d) = 804839e

Jump Instructions PC-relative Absolute address Jump target is an offset relative to the address of the instruction just followed jump (pointed by PC) Absolute address Jump target is an absolute address

Control Constructs in C Gotos goto L break continue Branch if () { } else { } switch () { }

Control Constructs in C Loop while () { } do { } while () for (init; test; incr) { }

Translating Conditional Branches t = test-expr ; if ( t ) goto true ; else-statement goto done true: then-statement done: if ( test-expr ) then-statement else else-statement

Translating Conditional Branches int absdiff(int x, int y) { if (x < y) return y – x; else return x – y; } int absdiff(int x, int y) { int rval ; if (x < y) goto less rval = x – y ; goto done; less: rval = y – x; done: return rval; }

Jump Instructions movl 8(%ebp), %edx get x movl 12(%ebp), %eax get y cmpl %eax, %edx cal x - y jl .L3 if x < y goto less subl %eax, %edx compute x - y movl %edx, %eax set return val jmp .L5 goto done .L3: less: subl %edx, %eax compute y – x .L5: done: Begin Completion code

Do-while Translation do body-statement while (test-expr) loop: t = test-expr; if ( t ) goto loop ;

Do-while Translation .L6: lea (%ebx, %edx), %eax movl %edx, %ebx movl %eax, %edx incl %ecx cmpl %esi, %ecx jl .L6 movl %ebx, %eax int fib_dw(int n) { int i = 0; int val = 0 ; int nval = 1 ; do { int t = val + nval ; val = nval ; nval = t ; i++; } while ( i<n) ; return val ; } register value initially %ecx i %esi n %ebx val %edx nval 1 %eax t -

While Loop Translation while (test-expr) body-statement loop: if ( !test-expr) t = test-expr goto done; if ( !t ) do goto done; body-statement body-statement while(test-expr) goto loop; done: done:

While Loop Translation int fib_w_goto(int n) { int val=1; int nval=1; int nmi, t ; if ( val >= n ) goto done ; nmi = n-1; loop: t=val+nval ; val = nval ; nval = t ; nmi--; if ( nmi ) goto loop done: return val } int fib_w(int n) { int i=1; int val=1; int nval=1; while ( i<n ) { int t=val+nval ; val = nval ; nval = t ; i++; } return val ;

While Loop Translation Register usage Register Variable Initially %edx nmi n-1 %ebx val 1 %ecx nval movl 8(%ebp), %eax movl $1, %ebx movl $1, %ecx cmpl %eax, ebx jge .L9 lea –1(%eax), %edx .L10: lea (%ecx, %ebx), %eax movl %ecx, %ebx movl %eax, %ecx decl %edx jnz .L10 .L9:

While Loop Translation /* strcpy: copy t to s; pointer version 2 */ void strcpy(char *s, char *t){ while ((*s = *t) != '\0') { s++ ; t++ ; }

While Loop Translation movl 12(%ebp), %eax movzbl (%eax), %edx movl 8(%ebp), %eax movb %dl, (%eax) movzbl (%eax), %eax testb %al, %al jne L3 popl %ebp ret _strcpy: pushl %ebp movl %esp, %ebp jmp L2 L3: addl $1, 8(%ebp) addl $1, 12(%ebp)

For Loop Translation for ( init-expr; test-expr; update-expr) body-statement init-expr while ( test-expr) { body-statement update-expr }

For Loop Translation /* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */ int strcmp(char *s, char *t) { for (; *s == *t ; s++, t++) if (*s == '\0') return 0; return *s - *t; }

For Loop Translation _strcmp: pushl %ebp movl %esp, %ebp subl $4, %esp jmp L2 L5: movl 8(%ebp), %eax movzbl (%eax), %eax testb %al, %al jne L3 movl $0, -4(%ebp) jmp L4 L3: addl $1, 8(%ebp) addl $1, 12(%ebp) L2: movl 8(%ebp), %eax movzbl (%eax), %edx movl 12(%ebp), %eax movzbl (%eax), %eax cmpb %al, %dl je L5

For Loop Translation movl 8(%ebp), %eax movzbl (%eax), %eax movsbl %al,%edx movl 12(%ebp), %eax movsbl %al,%eax movl %edx, %ecx subl %eax, %ecx movl %ecx, -4(%ebp) L4: movl -4(%ebp), %eax leave ret

Conditional Move Original C code 1 int absdiff(int x, int y) { 2 return x < y ? y-x : x-y; 3 } (b) Implementation using conditional assignment 1 int cmovdiff(int x, int y) { 2 int tval = y-x; 3 int rval = x-y; 4 int test = x < y; 5 /* Line below requires 6 single instruction: */ 7 if (test) rval = tval; 8 return rval; 9 } 28 28

Conditional Move (c) Generated assembly code %ecx x (x at %ebp+8, y at %ebp+12) movl 8(%ebp), %ecx Get x movl 12(%ebp), %edx Get y movl %edx, %ebx Copy y subl %ecx, %ebx Compute y-x movl %ecx, %eax Copy x subl %edx, %eax Compute x-y and set as return value cmpl %edx, %ecx Compare x:y cmovl %ebx, %eax If < , replace return value with y-x %ecx x %edx y %ebx y-x %eax x-y 29 29

Conditional Move instructions suppose that “there is no side effect” Invalid Situation Conditional Move instructions suppose that “there is no side effect” int cread(int *xp) { return (xp ? *xp : 0); } 30 30