+ All Categories

Today

Date post: 02-Jan-2016
Category:
Upload: unity-palmer
View: 20 times
Download: 0 times
Share this document with a friend
Description:
Today. How’s Lab2 going? Fun?. • • •. Offset. 12. yp. 8. xp. 4. Rtn adr. % ebp. 0. Old % ebp. -4. Old % ebx. Swap Ints in 32-bit Mode. void swap( int * xp , int * yp ) { int t0 = * xp ; int t1 = * yp ; * xp = t1; * yp = t0; }. swap: pushl %ebp - PowerPoint PPT Presentation
59
University of Washington Today How’s Lab2 going? Fun? 1
Transcript
Page 1: Today

University of Washington

1

Today How’s Lab2 going? Fun?

Page 2: Today

University of Washington

2

Swap Ints in 32-bit Modevoid swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:pushl %ebpmovl %esp,%ebppushl %ebx

movl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)

movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret

Body

Setup

Finishyp

xp

Rtn adrOld %ebp %ebp 0

4

8

12

Offset

•••

Old %ebx-4

Page 3: Today

University of Washington

3

Swap Ints in 64-bit Mode

Arguments passed in registers (why useful?) First (xp) in %rdi, second (yp) in %rsi 64-bit pointers

No stack operations required 32-bit data

Data held in registers %eax and %edx movl operation (the l refers to data width, not address width)

void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}

swap:movl (%rdi), %edxmovl (%rsi), %eaxmovl %eax, (%rdi)movl %edx, (%rsi)retq

Page 4: Today

University of Washington

4

Swap Long Ints in 64-bit Mode

64-bit data Data held in registers %rax and %rdx movq operation “q” stands for quad-word

void swap_l (long int *xp, long int *yp) { long int t0 = *xp; long int t1 = *yp; *xp = t1; *yp = t0;}

swap_l:movq (%rdi), %rdxmovq (%rsi), %raxmovq %rax, (%rdi)movq %rdx, (%rsi)retq

Page 5: Today

University of Washington

5

Complete Memory Addressing Modes Remember, the addresses used for accessing memory in mov (and

other) instructions can be computed in several different ways Most General Form:

D(Rb,Ri,S) Mem[Reg[Rb] + S*Reg[Ri] + D] D: Constant “displacement” 1, 2, or 4 bytes Rb: Base register: Any of the 8/16 integer registers Ri: Index register: Any, except for %esp or %rsp

Unlikely you’d use %ebp, either S: Scale: 1, 2, 4, or 8 (why these numbers?)

Special Cases: can use any combination of D, Rb, Ri and S(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D](Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

Page 6: Today

University of Washington

6

Address Computation Examples

%edx

%ecx

0xf000

0x100

Expression Address Computation Address

0x8(%edx)

(%edx,%ecx)

(%edx,%ecx,4)

0x80(,%edx,2)

(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]D(,Ri,S) Mem[S*Reg[Ri]+D](Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]D(Rb) Mem[Reg[Rb] +D]

Page 7: Today

University of Washington

7

Address Computation Examples

%edx

%ecx

0xf000

0x100

Expression Address Computation Address

0x8(%edx)

(%edx,%ecx)

(%edx,%ecx,4)

0x80(,%edx,2)

(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]]D(,Ri,S) Mem[S*Reg[Ri]+D](Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]D(Rb) Mem[Reg[Rb] +D]

0xf000 + 0x8 0xf008

0xf000 + 0x100 0xf100

0xf000 + 4*0x100 0xf400

2*0xf000 + 0x80 0x1e080

Page 8: Today

University of Washington

8

Address Computation Instruction leal Src,Dest

Src is address mode expression Set Dest to address computed by expression

(lea stands for load effective address) Example: leal (%edx,%ecx,4), %eax

Uses Computing addresses without a memory reference

E.g., translation of p = &x[i]; Computing arithmetic expressions of the form x + k*i

k = 1, 2, 4, or 8

Page 9: Today

University of Washington

9

Some Arithmetic Operations Two Operand (Binary) Instructions:

Format Computationaddl Src,Dest Dest = Dest + Srcsubl Src,Dest Dest = Dest - Srcimull Src,Dest Dest = Dest * Srcsall Src,Dest Dest = Dest << Src Also called shllsarl Src,Dest Dest = Dest >> Src Arithmeticshrl Src,Dest Dest = Dest >> Src Logicalxorl Src,Dest Dest = Dest ^ Srcandl Src,Dest Dest = Dest & Srcorl Src,Dest Dest = Dest | Src

Page 10: Today

University of Washington

10

Some Arithmetic Operations One Operand (Unary) Instructions

incl Dest Dest = Dest + 1decl Dest Dest = Dest - 1negl Dest Dest = -Destnotl Dest Dest = ~Dest

Page 11: Today

University of Washington

11

Using leal for Arithmetic Expressions

int arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

arith:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxmovl 12(%ebp),%edxleal (%edx,%eax),%ecxleal (%edx,%edx,2),%edxsall $4,%edxaddl 16(%ebp),%ecxleal 4(%edx,%eax),%eaximull %ecx,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

Page 12: Today

University of Washington

12

Understanding arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

z16

Page 13: Today

University of Washington

13

Understanding arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

z16

Page 14: Today

University of Washington

14

Understanding arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

z16

Page 15: Today

University of Washington

15

Understanding arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

z16

Page 16: Today

University of Washington

16

Observations about arithint arith (int x, int y, int z){ int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval;}

movl 8(%ebp),%eax # eax = xmovl 12(%ebp),%edx # edx = yleal (%edx,%eax),%ecx # ecx = x+y (t1)leal (%edx,%edx,2),%edx # edx = y + 2*y = 3*ysall $4,%edx # edx = 48*y (t4)addl 16(%ebp),%ecx # ecx = z+t1 (t2)leal 4(%edx,%eax),%eax # eax = 4+t4+x (t5)imull %ecx,%eax # eax = t5*t2 (rval)

Instructions in different order from C code

Some expressions require multiple instructions

Some instructions cover multiple expressions

Get exact same code when compile:

(x+y+z)*(x+4+48*y)

Page 17: Today

University of Washington

17

Another Example

int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}

logical:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax # eax = xxorl 12(%ebp),%eax # eax = x^ysarl $17,%eax # eax = t1>>17andl $8185,%eax # eax = t2 & 8185

y

x

Rtn adr

Old %ebp %ebp 0

4

8

12

OffsetStack

•••

Page 18: Today

University of Washington

18

Another Example

int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}

logical:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185

Page 19: Today

University of Washington

19

Another Example

int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}

logical:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185

Page 20: Today

University of Washington

20

Another Example

int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}

logical:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185

213 = 8192, 213 – 7 = 8185…0010000000000000, …0001111111111001

Page 21: Today

University of Washington

21

Another Example

int logical(int x, int y){ int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval;}

logical:pushl %ebpmovl %esp,%ebp

movl 8(%ebp),%eaxxorl 12(%ebp),%eaxsarl $17,%eaxandl $8185,%eax

movl %ebp,%esppopl %ebpret

Body

SetUp

Finish

movl 8(%ebp),%eax eax = xxorl 12(%ebp),%eax eax = x^y (t1)sarl $17,%eax eax = t1>>17 (t2)andl $8185,%eax eax = t2 & 8185

??

Page 22: Today

University of Washington

22

Topics: control flow Condition codes Conditional and unconditional branches Loops

Page 23: Today

University of Washington

23

Conditionals and Control Flow A conditional branch is sufficient to implement most control

flow constructs offered in higher level languages if (condition) then {...} else {…} while (condition) {…} do {…} while (condition) for (initialization; condition; iterative) {...}

Unconditional branches implement some related control flow constructs

break, continue

In x86, we’ll refer to branches as “jumps” (either conditional or unconditional)

Page 24: Today

University of Washington

24

Jumping jX Instructions

Jump to different part of code depending on condition codes

jX Condition Descriptionjmp 1 Unconditional

je ZF Equal / Zero

jne ~ZF Not Equal / Not Zero

js SF Negative

jns ~SF Nonnegative

jg ~(SF^OF)&~ZF Greater (Signed)

jge ~(SF^OF) Greater or Equal (Signed)

jl (SF^OF) Less (Signed)

jle (SF^OF)|ZF Less or Equal (Signed)

ja ~CF&~ZF Above (unsigned)

jb CF Below (unsigned)

Page 25: Today

University of Washington

25

Processor State (IA32, Partial) Information about

currently executing program Temporary data

( %eax, …)

Location of runtime stack( %ebp,%esp)

Location of current code control point( %eip )

Status of recent tests( CF,ZF,SF,OF )

%eip

General purposeregisters

Current stack top

Current stack frame

Instruction pointer

CF ZF SF OF Condition codes

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

Page 26: Today

University of Washington

26

Condition Codes (Implicit Setting) Single-bit registers

CF Carry Flag (for unsigned) SF Sign Flag (for signed)ZF Zero Flag OF Overflow Flag (for signed)

Implicitly set (think of it as side effect) by arithmetic operationsExample: addl/addq Src,Dest ↔ t = a+b

CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s complement (signed) overflow(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

Not set by lea instruction (beware!) Full documentation (IA32): http://www.jegerlehner.ch/intel/IntelCodeTable.pdf

Page 27: Today

University of Washington

27

Condition Codes (Explicit Setting: Compare) Single-bit registers

CF Carry Flag (for unsigned) SF Sign Flag (for signed)ZF Zero Flag OF Overflow Flag (for signed)

Explicit Setting by Compare Instructioncmpl/cmpq Src2,Src1

cmpl b,a like computing a-b without setting destination CF set if carry out from most significant bit (used for unsigned comparisons) ZF set if a == b SF set if (a-b) < 0 (as signed) OF set if two’s complement (signed) overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

Page 28: Today

University of Washington

28

Condition Codes (Explicit Setting: Test) Single-bit registers

CF Carry Flag (for unsigned) SF Sign Flag (for signed)ZF Zero Flag OF Overflow Flag (for signed)

Explicit Setting by Test instructiontestl/testq Src2,Src1

testl b,a like computing a & b without setting destination Sets condition codes based on value of Src1 & Src2 Useful to have one of the operands be a mask

ZF set if a&b == 0 SF set if a&b < 0

testl %eax, %eax Sets SF and ZF, check if eax is +,0,-

Page 29: Today

University of Washington

29

Reading Condition Codes SetX Instructions

Set a single byte to 0 or 1 based on combinations of condition codes

SetX Condition Descriptionsete ZF Equal / Zerosetne ~ZF Not Equal / Not Zerosets SF Negativesetns ~SF Nonnegativesetg ~(SF^OF)&~ZF Greater (Signed)setge ~(SF^OF) Greater or Equal (Signed)setl (SF^OF) Less (Signed)setle (SF^OF)|ZF Less or Equal (Signed)seta ~CF&~ZF Above (unsigned)setb CF Below (unsigned)

Page 30: Today

University of Washington

30

Reading Condition Codes (Cont.) SetX Instructions:

Set single byte to 0 or 1 based on combination of condition codes

One of 8 addressable byte registers Does not alter remaining 3 bytes Typically use movzbl to finish job

int gt (int x, int y){ return x > y;}

movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eax

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

%al%ah

%cl%ch

%dl%dh

%bl%bh

What does each of these instructions do?

Body: y at 12(%ebp), x at 8(%ebp)

Page 31: Today

University of Washington

31

Reading Condition Codes (Cont.) SetX Instructions:

Set single byte to 0 or 1 based on combination of condition codes

One of 8 addressable byte registers Does not alter remaining 3 bytes Typically use movzbl to finish job

int gt (int x, int y){ return x > y;}

movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x and ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eax

(x – y)

Body: y at 12(%ebp), x at 8(%ebp)

%eax

%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

%al%ah

%cl%ch

%dl%dh

%bl%bh

Page 32: Today

University of Washington

32

Jumping jX Instructions

Jump to different part of code depending on condition codes

jX Condition Descriptionjmp 1 Unconditional

je ZF Equal / Zero

jne ~ZF Not Equal / Not Zero

js SF Negative

jns ~SF Nonnegative

jg ~(SF^OF)&~ZF Greater (Signed)

jge ~(SF^OF) Greater or Equal (Signed)

jl (SF^OF) Less (Signed)

jle (SF^OF)|ZF Less or Equal (Signed)

ja ~CF&~ZF Above (unsigned)

jb CF Below (unsigned)

Page 33: Today

University of Washington

33

Conditional Branch Example

int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8

Body1

Setup

Finish

Body2

Page 34: Today

University of Washington

34

Conditional Branch Example (Cont.)

int absdiff(int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

C allows “goto” as means of transferring control Closer to machine-level

programming style Generally considered bad coding

style

Page 35: Today

University of Washington

35

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8int x %edx

int y %eax

Page 36: Today

University of Washington

36

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8int x %edx

int y %eax

Page 37: Today

University of Washington

37

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8int x %edx

int y %eax

Page 38: Today

University of Washington

38

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8int x %edx

int y %eax

Page 39: Today

University of Washington

39

Conditional Branch Example (Cont.)int goto_ad(int x, int y){ int result; if (x <= y) goto Else; result = x-y;Exit: return result;Else: result = y-x; goto Exit;}

absdiff:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %edxmovl 12(%ebp), %eaxcmpl %eax, %edxjle .L7subl %eax, %edxmovl %edx, %eax

.L8:leaveret

.L7:subl %edx, %eaxjmp .L8int x %edx

int y %eax

Page 40: Today

University of Washington

40

C Codeval = Test ? Then-Expr : Else-Expr;

Goto Versionnt = !Test;if (nt) goto Else;val = Then-Expr;

Done:. . .

Else: val = Else-Expr; goto Done;

General Conditional Expression Translation

Test is expression returning integer= 0 interpreted as false0 interpreted as true

Create separate code regions for then & else expressions

Execute appropriate one

How might you make this more efficient?

result = x>y ? x-y : y-x;

if (Test) val = Then-Expr;else val = Else-Expr;

Page 41: Today

University of Washington

41

Conditionals: x86-64

Conditional move instruction cmovC src, dest Move value from src to dest if condition C holds Why is this good?

absdiff: # x in %edi, y in %esimovl %edi, %eax # eax = xmovl %esi, %edx # edx = ysubl %esi, %eax # eax = x-ysubl %edi, %edx # edx = y-xcmpl %esi, %edi # x:ycmovle %edx, %eax # eax=edx if <=ret

int absdiff( int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

Page 42: Today

University of Washington

42

Conditionals: x86-64

Conditional move instruction cmovC src, dest Move value from src to dest if condition C holds More efficient than conditional branching (simple control flow) But overhead: both branches are evaluated

absdiff: # x in %edi, y in %esimovl %edi, %eax # eax = xmovl %esi, %edx # edx = ysubl %esi, %eax # eax = x-ysubl %edi, %edx # edx = y-xcmpl %esi, %edi # x:ycmovle %edx, %eax # eax=edx if <=ret

int absdiff( int x, int y){ int result; if (x > y) { result = x-y; } else { result = y-x; } return result;}

Page 43: Today

University of Washington

43

PC Relative Addressing

0x100 cmp r2, r3 0x10000x102 je 0x70 0x10020x104 … 0x1004… … … 0x172 add r3, r4 0x1072

PC relative branches are relocatable Absolute branches are not

Page 44: Today

University of Washington

44

Compiling Loops

How to compile other loops should be straightforward The only slightly tricky part is to be sure where the conditional branch

occurs: top or bottom of the loop How would for(i=0; i<100; i++) be implemented?

while ( sum != 0 ) { <loop body>}

loopTop: cmpl $0, %eax je loopDone <loop body code> jmp loopToploopDone:

Machine code:C/Java code:

Page 45: Today

University of Washington

45

C Codeint fact_do(int x){ int result = 1; do { result *= x; x = x-1; } while (x > 1); return result;}

Goto Versionint fact_goto(int x){ int result = 1;loop: result *= x; x = x-1; if (x > 1) goto loop; return result;}

“Do-While” Loop Example

Use backward branch to continue looping Only take branch when “while” condition holds

Page 46: Today

University of Washington

46

Goto Versionintfact_goto(int x){ int result = 1;

loop: result *= x; x = x-1; if (x > 1) goto loop;

return result;}

“Do-While” Loop CompilationRegisters:%edx x%eax result

fact_goto:pushl %ebp # Setupmovl %esp,%ebp # Setupmovl $1,%eax # eax = 1movl 8(%ebp),%edx # edx = x

.L11:imull %edx,%eax # result *= xdecl %edx # x--cmpl $1,%edx # Compare x : 1jg .L11 # if > goto loop

movl %ebp,%esp # Finishpopl %ebp # Finishret # Finish

Assembly

Translation?

Page 47: Today

University of Washington

47

Goto Versionintfact_goto(int x){ int result = 1;

loop: result *= x; x = x-1; if (x > 1) goto loop;

return result;}

“Do-While” Loop CompilationRegisters:%edx x%eax result

fact_goto:pushl %ebp # Setupmovl %esp,%ebp # Setupmovl $1,%eax # eax = 1movl 8(%ebp),%edx # edx = x

.L11:imull %edx,%eax # result *= xdecl %edx # x--cmpl $1,%edx # Compare x : 1jg .L11 # if > goto loop

movl %ebp,%esp # Finishpopl %ebp # Finishret # Finish

Assembly

Page 48: Today

University of Washington

48

C Codedo Body while (Test);

Goto Versionloop: Body if (Test) goto loop

General “Do-While” Translation

Body:

Test returns integer= 0 interpreted as false 0 interpreted as true

{ Statement1; Statement2; … Statementn;}

Page 49: Today

University of Washington

49

C Codeint fact_while(int x){ int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;}

Goto Versionint fact_while_goto(int x){ int result = 1; goto middle; loop: result *= x; x = x-1;middle: if (x > 1) goto loop; return result;}

“While” Loop Translation

Used by GCC for both IA32 & x86-64First iteration jumps over body computation within loop straight to test

Page 50: Today

University of Washington

50

int fact_while(int x){ int result = 1; while (x > 1) { result *= x; x--; }; return result;}

# x in %edx, result in %eax jmp .L34 # goto Middle.L35: # Loop: imull %edx, %eax # result *= x decl %edx # x--.L34: # Middle: cmpl $1, %edx # x:1 jg .L35 # if >, goto # Loop

“While” Loop Example

Page 51: Today

University of Washington

51

“For” Loop Example: Square-and-Multiply

Algorithm Exploit bit representation: p = p0 + 2p1 + 22p2 + … 2n–1pn–1

Gives: xp = z0 · z1 2 · (z2 2) 2 · … · (…((zn –12) 2 )…) 2

zi = 1 when pi = 0

zi = x when pi = 1

Complexity O(log p)

/* Compute x raised to nonnegative power p */int ipwr_for(int x, unsigned int p){

int result;for (result = 1; p != 0; p = p>>1) {

if (p & 0x1)result *= x;

x = x*x; }

return result;}

n–1 times

Example

310 = 32 * 38

= 32 * ((32)2)2

Page 52: Today

University of Washington

52

ipwr Computation/* Compute x raised to nonnegative power p */int ipwr_for(int x, unsigned int p){

int result;for (result = 1; p != 0; p = p>>1) {

if (p & 0x1)result *= x;

x = x*x;}return result;

}

before iteration result x=3 p=101 1 3 10=10102

2 1 9 5= 1012

3 9 81 2= 102

4 9 6561 1= 12

5 59049 43046721 02

Page 53: Today

University of Washington

53

“For” Loop Example

for (Init; Test; Update) Body

int result;for (result = 1; p != 0; p = p>>1){

if (p & 0x1)result *= x;

x = x*x;}

General Form

Init

result = 1

Test

p != 0

Update

p = p >> 1

Body

{ if (p & 0x1) result *= x; x = x*x; }

Page 54: Today

University of Washington

54

“For” “While”

for (Init; Test; Update )

Body

Init;while (Test ) { Body Update ;}

Init; goto middle;loop: Body Update ;middle: if (Test) goto loop;done:

While Version

For Version

Goto Version

Page 55: Today

University of Washington

55

For-Loop: Compilation

for (Init; Test; Update )

Body

Init; goto middle;loop: Body Update ;middle: if (Test) goto loop;done:

For Version

Goto Version

for (result = 1; p != 0; p = p>>1){ if (p & 0x1) result *= x; x = x*x;}

result = 1;goto middle;loop: if (p & 0x1) result *= x; x = x*x; p = p >> 1;middle: if (p != 0) goto loop;done:

Page 56: Today

University of Washington

56

Quick Review Complete memory addressing mode

(%eax), 17(%eax), 2(%ebx, %ecx, 8), …

Arithmetic operations that do set condition codes subl %eax, %ecx # ecx = ecx + eax sall $4,%edx # edx = edx << 4 addl 16(%ebp),%ecx # ecx = ecx + Mem[16+ebp]

imull %ecx,%eax # eax = eax * ecx

Arithmetic operations that do NOT set condition codes leal 4(%edx,%eax),%eax # eax = 4 + edx + eax

Page 57: Today

University of Washington

57

Quick Review x86-64 vs. IA32

Integer registers: 16 x 64-bit vs. 8 x 32-bit movq, addq, … vs. movl, addl, …

movq -> “move quad word” or 4*16-bits x86-64: better support for passing

function arguments in registers

Control Condition code registers Set as side effect or by cmp, test Used:

Read out by setx instructions (setg, setle, …) Or by conditional jumps (jle .L4, je .L10, …) Or by conditional moves (cmovle %edx, %eax)

%rax

%rbx

%rcx

%rdx

%rsi

%rdi

%rsp

%rbp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

%r8

%r9

%r10

%r11

%r12

%r13

%r14

%r15

%r8d

%r9d

%r10d

%r11d

%r12d

%r13d

%r14d

%r15d

CF ZF SF OF

Page 58: Today

University of Washington

58

Quick Review Do-While loop

While-Do loop

C Codedo Body while (Test);

Goto Versionloop: Body if (Test) goto loop

While versionwhile (Test) Body

Do-While Version if (!Test) goto done; do Body while(Test);done:

Goto Version if (!Test) goto done;loop: Body if (Test) goto loop;done:

goto middle;loop: Bodymiddle: if (Test) goto loop;

or

Page 59: Today

University of Washington

59

Summarizing C Control

if-then-else do-while while, for switch

Assembler Control Conditional jump Conditional move Indirect jump Compiler Must generate assembly code

to implement more complex control

Standard Techniques Loops converted to do-while form Large switch statements use jump tables Sparse switch statements may use

decision trees (see text)

Conditions in CISC CISC machines generally have condition

code registers


Recommended