Les 5: controletransfer-instructies en optimalisatie

100
ca5-1 Les 5: controletransfer- instructies en optimalisatie Software gets slower faster than hardware gets faster (Niklaus Wirth)

description

Les 5: controletransfer-instructies en optimalisatie. Software gets slower faster than hardware gets faster (Niklaus Wirth). i1 i2 i3 i4 i5 i6 i7 i8 i9 i10. Controletransferinstructies. Instructies die een nieuwe waarde toekennen aan de instructiewijzer. Overzicht. Sprongen - PowerPoint PPT Presentation

Transcript of Les 5: controletransfer-instructies en optimalisatie

Page 1: Les 5:  controletransfer-instructies en optimalisatie

ca5-1

Les 5: controletransfer-

instructies en optimalisatie

Software gets slower faster than hardware gets faster(Niklaus Wirth)

Page 2: Les 5:  controletransfer-instructies en optimalisatie

ca5-2

Controletransferinstructies

i1i2i3i4i5i6i7i8i9

i10

Instructies die een nieuwe waarde toekennen aan de instructiewijzer

Page 3: Les 5:  controletransfer-instructies en optimalisatie

ca5-3

Overzicht• Sprongen

• Lussen

• Procedure-oproep en -terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 4: Les 5:  controletransfer-instructies en optimalisatie

ca5-4

Sprongen

• Onvoorwaardelijke sprongen

• Voorwaardelijke sprongen

• Berekende sprongen

Page 5: Les 5:  controletransfer-instructies en optimalisatie

ca5-5

Onvoorwaardelijke sprongen

i1jmp 30

i3i4i5i6i7i8

jmp 26i10

jmp adresjmp adres

10141822263034384246

reg[eip] = adres

Sprong: onvoorwaardelijkeInstructie: jmp

Page 6: Les 5:  controletransfer-instructies en optimalisatie

ca5-6

Voorwaardelijke sprongen

i1jle 30

i3i4

jmp 38i6i7i8i9

i10

i1jle 30

i1jle 30

i3i4

jmp 38

i3i4

jmp 38

i6i7i6i7

i8i9

i10

i8i9

i10Basis-

blokkenBasis-

blokken

10141822263034384246

Sprong: voorwaardelijke

Page 7: Les 5:  controletransfer-instructies en optimalisatie

ca5-7

Sprongcondities (1)instructie sprongjz jump if zero

jc jump if carry

jo jump if overflow

jp jump if parity

js jump if sign

jnz jump if not zero

jnc jump if not carry

jno jump if not overflow

jnp jump if not parity

jns jump if not sign

Sprong: condities

Page 8: Les 5:  controletransfer-instructies en optimalisatie

ca5-8

Sprongcondities (2)instructie sprong

jg jnle jump if greater

jge jnl jump if greater or equal

jl jnge jump if less

jle jng jump if less or equal

je jump if equal

jne jump if not equal

ja jnbe jump if above

jae jnb jump if above or equal

jb jnae jump if below

jbe jna jump if below or equal

bina

ir2-

com

plem

ent

Page 9: Les 5:  controletransfer-instructies en optimalisatie

ca5-9

Statisch vs. berekend adres

jmp 100jmp 100

mov ebx,100

jmp ebx

mov ebx,100

jmp ebx

reg[eip] = 100 reg[eip] = reg[ebx]

Sprong: berekende

Page 10: Les 5:  controletransfer-instructies en optimalisatie

ca5-10

switch (a) { case 0: b += 3; break; case 2: b -= 4; break; case 3: b -= 3; break; default: b += 7;}

Switch: implementatie 1

if (a == 0) b += 3;else if (a == 2) b -= 4; else if (a == 3) b -= 3; else b += 7;

mov ebx,a mov edx,b cmp ebx,0 jne $23 add edx,3 jmp $20$23: cmp ebx,2 jne $24 sub edx,4 jmp $20$24: cmp ebx,3 jne $25 sub edx,3 jmp $20$25: add edx,7$20: mov b, edx

mov ebx,a mov edx,b cmp ebx,0 jne $23 add edx,3 jmp $20$23: cmp ebx,2 jne $24 sub edx,4 jmp $20$24: cmp ebx,3 jne $25 sub edx,3 jmp $20$25: add edx,7$20: mov b, edx

Page 11: Les 5:  controletransfer-instructies en optimalisatie

ca5-11

Switch: implementatie 1

$20: mov b, edx$20: mov b, edx

mov ebx,amov edx,bcmp ebx,0jne $23

mov ebx,amov edx,bcmp ebx,0jne $23

$23: cmp ebx,2 jne $24

$23: cmp ebx,2 jne $24

sub edx,4jmp $20

sub edx,4jmp $20

$24: cmp ebx,3 jne $25

$24: cmp ebx,3 jne $25

sub edx,3jmp $20

sub edx,3jmp $20

$25: add edx,7$25: add edx,7

add edx,3jmp $20

add edx,3jmp $20

Page 12: Les 5:  controletransfer-instructies en optimalisatie

ca5-12

mov ebx,a mov edx,b

cmp ebx,0jl $25cmp ebx,3jg $25mov eax,[tabel+ebx*4]jmp eax

$22: add edx,3jmp $20

$23: sub edx,4jmp $20

$24: sub edx,3jmp $20

$25: add edx,7$20: mov b, edx

mov ebx,a mov edx,b

cmp ebx,0jl $25cmp ebx,3jg $25mov eax,[tabel+ebx*4]jmp eax

$22: add edx,3jmp $20

$23: sub edx,4jmp $20

$24: sub edx,3jmp $20

$25: add edx,7$20: mov b, edx

tabel: .long $22.long $25.long $23.long $24

if (a < 0) b += 7;else if (a > 3) b += 7;else switch (a) { case 0: b += 3; break; case 1: b += 7; break; case 2: b -= 4; break; case 3: b -= 3; break; }

Switch: implementatie 2

Page 13: Les 5:  controletransfer-instructies en optimalisatie

ca5-13

$25: add edx,7

Switch: implementatie 2mov ebx,amov edx,bcmp ebx,0jl $25

cmp ebx,3jg $25

mov eax,[tabel+ebx*4]jmp eax

$22: add edx,3 jmp $20

$23: sub edx,4 jmp $20

$24: sub edx,3 jmp $20

$20: mov b, edx

Page 14: Les 5:  controletransfer-instructies en optimalisatie

ca5-14

mov ebx,a mov edx,b

cmp ebx,0jl $25cmp ebx,3jg $25add edx,[tabel+ebx*4]jmp $20

$25: add edx,7$20: mov b, edx

mov ebx,a mov edx,b

cmp ebx,0jl $25cmp ebx,3jg $25add edx,[tabel+ebx*4]jmp $20

$25: add edx,7$20: mov b, edx tabel: .long 3

.long 7

.long -4

.long -3

int tabel[4] = {3,7,-4,-3};

if (a < 0) b += 7;else if (a > 3) b += 7;else b += tabel[a];

Switch: implementatie 3

Page 15: Les 5:  controletransfer-instructies en optimalisatie

ca5-15

$25: add edx,7

Switch: implementatie 3

mov ebx,amov edx,bcmp ebx,0jl $25

cmp ebx,3jg $25

add edx,[tabel+ebx*4]jmp $20

$20: mov b, edx

Page 16: Les 5:  controletransfer-instructies en optimalisatie

ca5-16

Absoluut vs. Relatief adres

• Absoluutspring naar adres n

• Relatiefspring n bytes verder/terug

i1jmp 30

i3i4i5i6i7i8

jmp eip-20i10

10141822263034384246

reg[eip] = reg[eip] + n

Page 17: Les 5:  controletransfer-instructies en optimalisatie

ca5-17

Positie-onafhankelijke code

i1jmp 30

i3i4i5i6i7i8

jmp eip-20i10

10141822263034384246

i1jmp 30

i3i4i5i6i7i8

jmp eip-20i10

101418222630343842465054

38

Page 18: Les 5:  controletransfer-instructies en optimalisatie

ca5-18

Overzicht

• Sprongen• Lussen • Procedure-oproep en -terugkeer• Onderbrekingen• Instructiecodering• Compilers, linkers, en laders• Codeoptimalisatie

Page 19: Les 5:  controletransfer-instructies en optimalisatie

ca5-19

Lusinstructie

i1i2

mov ecx,5 i4i5i6i7i8

loop 22i10

10141822263034384246

loop adresloop adres

Decrementeer reg[ecx]

Spring naar adres indien reg[ecx] != 0

lus

Instructie: loop

Page 20: Les 5:  controletransfer-instructies en optimalisatie

ca5-20

Geprogrammeerde lus

i1i2

mov ecx,5 i4i5i6i7i8

sub ecx,1jnz 22

10141822263034384246

lus

Page 21: Les 5:  controletransfer-instructies en optimalisatie

ca5-21

lusimplementaties

jmp

cmpjnz

cmpjnz

cmpjz

jmp

jmpcmp

jz

jmp

while/forwhile/for do-while do-while

n

n-1

1

n-1

1

1

n-11

Page 22: Les 5:  controletransfer-instructies en optimalisatie

ca5-22

Overzicht• Sprongen

• Lussen

• Procedure-oproep en –terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 23: Les 5:  controletransfer-instructies en optimalisatie

ca5-23

Functieoproep

i1i2

call 34i4i5i6i7i8i9

i10ret

1014182226303438424650

call adrescall adres

retret

reg[esp] = reg[esp]-4mem[reg[esp]] = reg[eip] reg[eip] = adres

reg[eip] = mem[reg[esp]];reg[esp] = reg[esp] + 4

Page 24: Les 5:  controletransfer-instructies en optimalisatie

ca5-24

Controleverloopgraaf van functie-oproep

i1i2

call 34i4i5i6i7i8i9

i10ret

1014182226303438424650

i1i2

call 34

i1i2

call 34 i7i8i9

i10ret

i7i8i9

i10ret

i4i5i6

i4i5i6

Page 25: Les 5:  controletransfer-instructies en optimalisatie

ca5-25

Functie-oproep

i1i2

call r,34i4i5i6i7i8i9

i10jmp r

1014182226303438424650

call r,adrescall r,adres

jmp rjmp r

reg[r] = reg[eip] reg[eip] = adres

reg[eip] = reg[r]

bal r, adresbal r, adres

Instructie: call

Instructie: retInstructie: bal

Page 26: Les 5:  controletransfer-instructies en optimalisatie

ca5-26

int vijfvoud(int n) { if (n > 0) return n * 5; else return 0; } int g; main() { g = vijfvoud(6); }

int vijfvoud(int n) { if (n > 0) return n * 5; else return 0; } int g; main() { g = vijfvoud(6); }

Functie-oproep en -terugkeer

Page 27: Les 5:  controletransfer-instructies en optimalisatie

ca5-27

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

????????eax

????????ebx

????????edx

00000108esp

????????

????????

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000315eip

s=? z=?

cont

role

stap

el

Page 28: Les 5:  controletransfer-instructies en optimalisatie

ca5-28

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000006eax

????????ebx

????????edx

00000108esp

????????

????????

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000320eip

s=? z=?

push 325jmp 300

Page 29: Les 5:  controletransfer-instructies en optimalisatie

ca5-29

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000006eax

????????ebx

????????edx

00000104esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000300eip

s=? z=?

Page 30: Les 5:  controletransfer-instructies en optimalisatie

ca5-30

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000006eax

????????ebx

????????edx

00000104esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000302eip

s=0 z=0

Page 31: Les 5:  controletransfer-instructies en optimalisatie

ca5-31

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000006eax

????????ebx

????????edx

00000104esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000307eip

s=0 z=0

Page 32: Les 5:  controletransfer-instructies en optimalisatie

ca5-32

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000006eax

00000005ebx

????????edx

00000104esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000312eip

s=0 z=0

Page 33: Les 5:  controletransfer-instructies en optimalisatie

ca5-33

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000030eax

00000005ebx

00000000edx

00000104esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000314eip

s=0 z=0

Page 34: Les 5:  controletransfer-instructies en optimalisatie

ca5-34

Codevijfvoud: cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000030eax

00000005ebx

00000000edx

00000108esp

????????

00000325

????????

100

104

108

300:302:304:306:

307:312:314:

315:320:325:

00000325eip

s=0 z=0

Page 35: Les 5:  controletransfer-instructies en optimalisatie

ca5-35

Bewaren registersvijfvoud: push edx cmp eax,0 jg positief xor eax, eax pop edx retpositief: mov ebx, 5 imul ebx pop edx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000030eax

00000005ebx

????????edx

00000108esp

edx

00000328

????????

100

104

108

300:301:303:305:307:308

309:314:316:317:

318:323:328:

???????? 96

s=0 z=0

Registers: bewaren

Page 36: Les 5:  controletransfer-instructies en optimalisatie

ca5-36

Bew

aren

reg

iste

rsvijfvoud: push edx push ebx cmp eax,0 jg positief xor eax, eax pop ebx pop edx retpositief: mov ebx, 5 imul ebx pop ebx pop edx ret

main: mov eax, 6 call vijfvoud mov g, eax

00000030eax

????????ebx

????????edx

????????092

edx

t.k. adres

????????

100

104

108

ebx096

s=0 z=0

Page 37: Les 5:  controletransfer-instructies en optimalisatie

ca5-37

Controleverloopgraaf

vijfvoud: push edx push ebx cmp eax,0 jg positief

positief: mov ebx, 5 imul ebx pop ebx pop edx ret

xor eax, eax pop ebx pop edx ret

mov eax,6

call vijfvoud

mov g, eax

Page 38: Les 5:  controletransfer-instructies en optimalisatie

ca5-38

Controleverloopgraaf

positief: push edx push ebx mov ebx, 5 imul ebx pop ebx pop edx ret

xor eax, eax ret

vijfvoud: cmp eax,0 jg positief

mov eax,6

call vijfvoud

mov g, eax

Page 39: Les 5:  controletransfer-instructies en optimalisatie

ca5-39

Controleverloopgraaf

positief: push edx mov edx, 5 imul edx pop edx ret

xor eax, eax ret

vijfvoud: cmp eax,0 jg positief

mov eax,6

call vijfvoud

mov g, eax

Page 40: Les 5:  controletransfer-instructies en optimalisatie

ca5-40

Parameterdoorgave via stapel 1vijfvoud:

mov eax,[esp+4] cmp eax,0 jg positief xor eax, eax retpositief: mov ebx, 5 imul ebx ret

main: push 6 call vijfvoud add esp,4 mov g, eax

t.k. adres

00000006

????????

100

104

108

????????096

esp

Parameterdoorgave

Page 41: Les 5:  controletransfer-instructies en optimalisatie

ca5-41

Parameterdoorgave via stapel 2

t.k. adres

00000006

????????

100

104

108

????????096

esp

vijfvoud: mov eax,[esp+4] cmp eax,0 jg positief xor eax, eax ret 4positief: mov ebx, 5 imul ebx ret 4

main: push 6 call vijfvoud add esp,4 mov g, eax

Page 42: Les 5:  controletransfer-instructies en optimalisatie

ca5-42

Lokale veranderlijken int vijfvoud(int n) { int resultaat;

if (n > 0) resultaat = n * 5; else resultaat = 0; return resultaat; } int g; main() { g = vijfvoud(6); }

int vijfvoud(int n) { int resultaat;

if (n > 0) resultaat = n * 5; else resultaat = 0; return resultaat; } int g; main() { g = vijfvoud(6); }

Page 43: Les 5:  controletransfer-instructies en optimalisatie

ca5-43

Lokale veranderlijken

vijfvoud: sub esp,4 cmp eax,0 jg positief xor eax,eax mov [esp], eax jmp eindepositief: mov ebx, 5 imul ebx mov [esp], eaxeinde: mov eax,[esp] add esp,4 retprogramma: mov eax, 6 call vijfvoud mov g, eax

????????

t.k. adres

????????

100

104

108

????????096

esp

Page 44: Les 5:  controletransfer-instructies en optimalisatie

ca5-44

Controleverloopgraaf

vijfvoud: sub esp,4 cmp eax,0 jg positief

positief: mov ebx, 5 imul ebx mov [esp],eax

xor eax, eax mov [esp],eax jmp end

end: mov eax,[esp] add esp,4 ret

mov eax,6

call vijfvoud

mov g, eax

Page 45: Les 5:  controletransfer-instructies en optimalisatie

ca5-45

Compleet beeldvijfvoud: sub esp,4 push ebx push edx mov eax, [esp+16] cmp eax,0 jg positief xor eax,eax mov [esp+8], eax jmp eindepositief: mov ebx, 5 imul ebx mov [esp+8], eaxeinde: mov eax,[esp+8] pop edx pop ebx add esp, 4 ret

programma: push 6 call vijfvoud add esp,4 mov g, eax

resultaat

edxebx

t.k. adres00000006

espesp+4esp+8

esp+12

esp+16

stack frame

Page 46: Les 5:  controletransfer-instructies en optimalisatie

ca5-46

Overzicht• Sprongen

• Lussen

• Procedure-oproep en -terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 47: Les 5:  controletransfer-instructies en optimalisatie

ca5-47

Onderbrekingen

• Sprong naar een routine via een nummer i.p.v. via een adres

• Adressen van routines opgeslagen in een tabel van adressen (vectortabel)

• Gebruikt voor het opvangen van fouten, of als interface naar het besturingssysteem

Page 48: Les 5:  controletransfer-instructies en optimalisatie

ca5-48

Onderbrekingi1i2i3i4i5i6i7i8

int 2i10i11

int 3

t

01234

i1i2i3i4i5i6

Onderbrekings-routine 3

vectortabel

Page 49: Les 5:  controletransfer-instructies en optimalisatie

ca5-49

Systeemoperaties

• Controle van de machine: manipulatie van de processortoestand– onderbrekingen aan/uit– veranderen van privilegeniveau– halt-instructie– omschakeling big-endian naar little-endian– geheugenbeheer (caches, virtueel

geheugen, enz.)

Page 50: Les 5:  controletransfer-instructies en optimalisatie

ca5-50

Overzicht• Sprongen

• Lussen

• Procedure-oproep en -terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 51: Les 5:  controletransfer-instructies en optimalisatie

ca5-51

Instructiedecodering

05 02 00

0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

w word

1

data2

0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0

opcodeadd immediate to accumulator

0 0 0 0 0 1 0

add ax,2

Page 52: Les 5:  controletransfer-instructies en optimalisatie

ca5-52

Instructiebeschrijving

SUB subtract O D I T S Z A P C 001010dwoorrrmmm disp * * * * * *

ea=514 sub r,r sub r,m sub m,r8088 3 13+ea 24+ea8086 3 9+ea 16+ea80286 2 7 780386 2 7 680486 1 2 3Pentium 1 2 3

Page 53: Les 5:  controletransfer-instructies en optimalisatie

ca5-53

Page 54: Les 5:  controletransfer-instructies en optimalisatie

ca5-54

Page 55: Les 5:  controletransfer-instructies en optimalisatie

ca5-55

Overzicht• Sprongen

• Lussen

• Procedure-oproep en -terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 56: Les 5:  controletransfer-instructies en optimalisatie

ca5-56

Programma-ontwikkeling

broncode compilercompiler Object-bestand

linkerlinkerbibliotheken

uitvoerbaarbestand

Page 57: Les 5:  controletransfer-instructies en optimalisatie

ca5-57

Compiler

• Verschillende fasen– Lexicale analyse: a, 12, then, (, while– Syntactische analyse: if (a>b) then …– Semantische analyse (type-controle,…)– Optimalisatie – Codegeneratie– Scheduling

Page 58: Les 5:  controletransfer-instructies en optimalisatie

ca5-58

Objectbestand

object-bestand

instructies

extra informatie

globale veranderlijken

Page 59: Les 5:  controletransfer-instructies en optimalisatie

ca5-59

Linker

linkerlinker

Page 60: Les 5:  controletransfer-instructies en optimalisatie

ca5-60

Lader

laderlader

stapel

Grabbel-geheugen

geheugen

Dynamisch gealloceerd geheugen

Page 61: Les 5:  controletransfer-instructies en optimalisatie

ca5-61

Overzicht• Sprongen

• Lussen

• Procedure-oproep en -terugkeer

• Onderbrekingen

• Instructiecodering

• Compilers, linkers, en laders

• Codeoptimalisatie

Page 62: Les 5:  controletransfer-instructies en optimalisatie

ca5-62

Voorbeeld

int max(int n1, int n2) { return n1 > n2 ? n1 : n2;}

int max4(int n1, int n2, int n3, int n4) { return max(max(n1,n2),max(n3,n4));}

int g;

main () { g = max4(15,10,9,4);}

int max(int n1, int n2) { return n1 > n2 ? n1 : n2;}

int max4(int n1, int n2, int n3, int n4) { return max(max(n1,n2),max(n3,n4));}

int g;

main () { g = max4(15,10,9,4);}

Page 63: Les 5:  controletransfer-instructies en optimalisatie

ca5-63

Hoofdprogramma

main:push 4 ; n4push 9 ; n3push 10 ; n2push 15 ; n1call max4add esp,16mov g, eax

main:push 4 ; n4push 9 ; n3push 10 ; n2push 15 ; n1call max4add esp,16mov g, eax

return

n1

n2

n3

n4

Page 64: Les 5:  controletransfer-instructies en optimalisatie

ca5-64

max4:pushebpmov ebp,esppushesipushedipush[ebp+12]push[ebp+8]call maxadd esp,8mov edi,eax

n1

n2

edi

esi

ebp

return

n1

n2

n3

n4

ebp

Page 65: Les 5:  controletransfer-instructies en optimalisatie

ca5-65

max:pushebpmov ebp,esppushebxpushedimov edi,[ebp+12]cmp [ebp+8],edijle $14mov ebx,[ebp+8]jmp $15

$14: mov ebx,[ebp+12]$15: mov eax,ebx

pop edipop ebxpop ebpret

ediebxebp

returnn1n2ediesiebp

returnn1n2n3n4

ebp

Sta

ck fr

ames

Page 66: Les 5:  controletransfer-instructies en optimalisatie

ca5-66

Enter/Leave

enter nenter n push ebp

mov ebp,esp

sub esp,n

push ebp

mov ebp,esp

sub esp,n

leaveleave mov esp,ebp

pop ebp

mov esp,ebp

pop ebp

lok 2

lok 1

ebp

return

arg

ebp

Page 67: Les 5:  controletransfer-instructies en optimalisatie

ca5-67

Stack frames

p1

p2

p3

p4activatieboom

ParametersTerugkeeradres

Bewaarde registersLokale veranderlijken

Stack frame pointer

Page 68: Les 5:  controletransfer-instructies en optimalisatie

ca5-68

Oproepconventies

• Verzameling conventies die de oproep van procedures/functies regelt– Argumenten op stapel of in registers– Volgorde van de argumenten– Welk register is stapelwijzer– Registers bewaard door oproeper (caller saved) of

de oproepeling (callee saved)– Terugkeeradres op de stapel, of in een register (zgn.

link register)

Page 69: Les 5:  controletransfer-instructies en optimalisatie

ca5-69

max4:pushebpmov ebp,esppushesipushedipush[ebp+12]push[ebp+8]call maxadd esp,8mov edi,eax

push [ebp+20]push[ebp+16]

call maxadd esp,8mov esi,eaxpushesipushedicall maxadd esp,8pop edipop esipop ebpret

Page 70: Les 5:  controletransfer-instructies en optimalisatie

ca5-70

max:pushebpmov ebp,esppushebxpushedimov edi,[ebp+12]cmp [ebp+8],edijle $14mov ebx,[ebp+8]jmp $15

$14: mov ebx,[ebp+12]$15: mov eax,ebx

pop edipop ebxpop ebpret

eax

eax

Optimalisatie

Kopiepropagatie op eax en ebx.

Page 71: Les 5:  controletransfer-instructies en optimalisatie

ca5-71

max:pushebpmov ebp,esppushedimov edi,[ebp+12]cmp [ebp+8],edijle $14mov eax,[ebp+8]jmp $15

$14: mov eax,[ebp+12]$15: pop edi

pop ebpret

ebp = esp+4

esp+16esp+12

esp+12

esp+16

Optimalisatie

edi

ebp

ret

arg1

arg2

ebp

esp

constantenpropagatie

Page 72: Les 5:  controletransfer-instructies en optimalisatie

ca5-72

max:pushebpmov ebp,esppushedimov edi,[esp+16]cmp [esp+12],edijle $14mov eax,[esp+12]jmp $15

$14: mov eax,[esp+16]$15: pop edi

pop ebpret

dode waardeOptimalisatie

Page 73: Les 5:  controletransfer-instructies en optimalisatie

ca5-73

max:pushebppushedimov edi,[esp+16]cmp [esp+12],edijle $14mov eax,[esp+12]jmp $15

$14: mov eax,[esp+16]$15: pop edi

pop ebpret

Optimalisatie

Idempotente code

edi

ret

arg1

arg2

esp12

12

8

8

Page 74: Les 5:  controletransfer-instructies en optimalisatie

ca5-74

max:pushedimov edi,[esp+12]cmp [esp+8],edijle $14mov eax,[esp+8]jmp $15

$14: mov eax,[esp+12]$15: pop edi

ret

eaxeax

Optimalisatie

ret

arg1

arg2

esp

8

8

4 4

Register-herallocatie

Page 75: Les 5:  controletransfer-instructies en optimalisatie

ca5-75

max:mov eax,[esp+8]cmp [esp+4],eaxjle $14mov eax,[esp+4]jmp $15

$14: mov eax,[esp+8]$15: ret

Idempotente code

Optimalisatie

Page 76: Les 5:  controletransfer-instructies en optimalisatie

ca5-76

max:mov eax,[esp+8]cmp [esp+4],eaxjle $14mov eax,[esp+4]jmp $15

$14:$15: ret

Optimalisatie

Sprong naar doorvalpad

Page 77: Les 5:  controletransfer-instructies en optimalisatie

ca5-77

max:mov eax,[esp+8]cmp [esp+4],eaxjle $14mov eax,[esp+4]

$14: ret

Optimalisatie

Page 78: Les 5:  controletransfer-instructies en optimalisatie

ca5-78

max4:pushebpmov ebp,esppushesipushedipush[ebp+12]push[ebp+8]call maxadd esp,8mov edi,eax

push [ebp+20]push[ebp+16]

call maxadd esp,8mov esi,eaxpushesipushedicall maxadd esp,8pop edipop esipop ebpret

Optimalisatie

Page 79: Les 5:  controletransfer-instructies en optimalisatie

ca5-79

max:mov eax,[esp+8]cmp [esp+4],eaxjle $14mov eax,[esp+4]

$14: ret

Optimalisatie

max:cmp esi,eaxjle $14mov eax,esi

$14: ret

Page 80: Les 5:  controletransfer-instructies en optimalisatie

ca5-80

max4:pushebpmov ebp,esppushesipushedimov eax, [ebp+12]mov esi, [ebp+8]call maxmov edi,eax

mov eax, [ebp+20]mov esi, [ebp+16] call maxmov esi,edicall maxpop edipop esipop ebpret

Optimalisatie

Page 81: Les 5:  controletransfer-instructies en optimalisatie

ca5-81

max4:pushebpmov ebp,esppushesipushedimov eax, [ebp+12]mov esi, [ebp+8]cmp esi,eaxjle $14mov eax,esi

$14:mov edi,eax

mov eax, [ebp+20]mov esi, [ebp+16] cmp esi,eaxjle $15mov eax,esi

$15:mov esi,edicmp esi,eaxjle $16mov eax,esi

$16:pop edipop esipop ebpret

Substitutie

kopiepropagatie

kop

iep

ropa

gatie

Page 82: Les 5:  controletransfer-instructies en optimalisatie

ca5-82

max4:pushebpmov ebp,esppushesipushedimov edi, [ebp+12]mov esi, [ebp+8]cmp esi,edijle $14mov edi,esi

$14:mov edi,eax

Registerallocatie mov eax, [ebp+20]mov esi, [ebp+16] cmp esi,eaxjle $15mov eax,esi

$15:mov esi,edicmp edi,eaxjle $16mov eax,edi

$16:pop edipop esipop ebpret

Page 83: Les 5:  controletransfer-instructies en optimalisatie

ca5-83

max4:pushebpmov ebp,esppushesipushedimov edi, [ebp+12]mov esi, [ebp+8]cmp esi,edijle $14mov edi,esi

$14:

Registerallocatie mov eax, [ebp+20]mov esi, [ebp+16] cmp esi,eaxjle $15mov eax,esi

$15:cmp edi,eaxjle $16mov eax,edi

$16:pop edipop esipop ebpret

Page 84: Les 5:  controletransfer-instructies en optimalisatie

ca5-84

max4:pushebpmov ebp,esppushesipushedipushebxmov edi, [ebp+12]mov esi, [ebp+8]cmp esi,edijle $14mov edi,esi

$14:

Registerallocatie mov eax, [ebp+20]mov ebx, [ebp+16] cmp ebx,eaxjle $15mov eax,ebx

$15:cmp edi,eaxjle $16mov eax,edi

$16:pop ebxpop edipop esipop ebpret

Page 85: Les 5:  controletransfer-instructies en optimalisatie

ca5-85

Hoofdprogramma

main: push 9 ; n4 push 4 ; n3 push 10 ; n2 push 15 ; n1 call max4 add esp,16 mov g, eax

main: push 9 ; n4 push 4 ; n3 push 10 ; n2 push 15 ; n1 call max4 add esp,16 mov g, eax

main: mov eax,9 ; n4 mov ebx,4 ; n3 mov edi,10 ; n2 mov esi,15 ; n1 call max4

mov g, eax

main: mov eax,9 ; n4 mov ebx,4 ; n3 mov edi,10 ; n2 mov esi,15 ; n1 call max4

mov g, eax

Page 86: Les 5:  controletransfer-instructies en optimalisatie

ca5-86

max4:pushebpmov ebp,esppushesipushedipushebxmov edi, [ebp+12]mov esi, [ebp+8]cmp esi,edijle $14mov edi,esi

$14:

Optimalisatie mov eax, [ebp+20]mov ebx, [ebp+16] cmp ebx,eaxjle $15mov eax,ebx

$15:cmp edi,eaxjle $16mov eax,edi

$16:pop ebxpop edipop esipop ebpret

Page 87: Les 5:  controletransfer-instructies en optimalisatie

ca5-87

max4:pushedicmp esi,edijle $14mov edi,esi

$14:

Registerallocatie

cmp ebx,eaxjle $15mov eax,ebx

$15:cmp edi,eaxjle $16mov eax,edi

$16:pop ediret

Page 88: Les 5:  controletransfer-instructies en optimalisatie

ca5-88

Geoptimaliseerdprogramma

main: mov eax,9 ; n4 mov ebx,4 ; n3 mov edi,10 ; n2 mov esi,15 ; n1 call max4 mov g, eax

main: mov eax,9 ; n4 mov ebx,4 ; n3 mov edi,10 ; n2 mov esi,15 ; n1 call max4 mov g, eax

max4:cmp esi,edijle $14mov edi,esi

$14: cmp ebx,eaxjle $15mov eax,ebx

$15: cmp edi,eaxjle $16mov eax,edi

$16: ret

max4:cmp esi,edijle $14mov edi,esi

$14: cmp ebx,eaxjle $15mov eax,ebx

$15: cmp edi,eaxjle $16mov eax,edi

$16: ret

Page 89: Les 5:  controletransfer-instructies en optimalisatie

ca5-89

Geoptimaliseerdprogramma

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

mov eax,edimov eax,edi

mov g, eaxmov g, eax

max4: cmp esi,edijle $14

max4: cmp esi,edijle $14

mov edi,esimov edi,esi

$14: cmp ebx,eaxjle $15

$14: cmp ebx,eaxjle $15

mov eax,ebxmov eax,ebx

$15: cmp edi,eaxjle $16

$15: cmp edi,eaxjle $16

$16: ret$16: ret

eax=9ebx=4edi=10esi=15

eax=9ebx=4edi=15esi=15

eax=9ebx=4edi=15esi=15

eax=15ebx=4edi=15esi=15

Page 90: Les 5:  controletransfer-instructies en optimalisatie

ca5-90

Geoptimaliseerdprogramma

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

mov eax,edimov eax,edi

mov g, eaxmov g, eax

max4: cmp esi,edimax4: cmp esi,edi

mov edi,esimov edi,esi

$14: cmp ebx,eax$14: cmp ebx,eax

$15: cmp edi,eax$15: cmp edi,eax

$16: ret$16: ret

Page 91: Les 5:  controletransfer-instructies en optimalisatie

ca5-91

Geoptimaliseerdprogramma

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 call max4

mov g, eaxmov g, eax

mov edi,esimov eax,ediret

mov edi,esimov eax,ediret

Page 92: Les 5:  controletransfer-instructies en optimalisatie

ca5-92

Geoptimaliseerdprogramma

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 mov edi,esi mov eax,edi mov g, eax

main: mov eax,9 mov ebx,4 mov edi,10 mov esi,15 mov edi,esi mov eax,edi mov g, eax

Dode-waarde-eliminatieKopiepropagatie

mov g, 15mov g, 15

Page 93: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-93

int main(){ int i, j; int a[1000000]; float b[1000000]; float r = 0.0;

for (i=0; i<1000000; i++) for (j=0; j<1000000; j++) { unsigned int pattern = a[i] ^ a[j]; if (popcount(pattern) == 2) r += b[i] * b[j]; }}

i

j

a b

Page 94: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-94

int main(){ int i, j; int a[1000000]; float b[1000000]; float r = 0.0;

for (i=0; i<1000000; i++) for (j=j+1; j<1000000; j++) { unsigned int pattern = a[i] ^ a[j]; if (popcount(pattern) == 2) r += 2 * b[i] * b[j]; }}

i

j

a b

Page 95: Les 5:  controletransfer-instructies en optimalisatie

Popcount

ca5-95

int popcount(unsigned int x){ int c = 0; for (; x > 0; x &= x -1) c++; return c;}

for (i=0; i < (1<<16); i++) { _popcount[i] = popcount(i);

+

Page 96: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-96

int main(){ int i, j; int a[1000000]; float b[1000000]; float r = 0.0;

for (i=0; i<1000000; i++) for (j=j+1; j<1000000; j++) { unsigned int pattern = a[i] ^ a[j]; if (_popcount[pattern] == 2) r += 2 * b[i] * b[j]; }}

i

j

a b

Page 97: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-97

int main(){ for (i=0; i<N; i++) for (j=j+1; j<N; j++) { unsigned int pattern = a_pat[i] ^ a_pat[j]; if (_popcount[pattern] == 2) { for (x=0; x<a_pat[i].len; x++) for (y=0; y<a_pat[j].len; y++) r += 2 * a_pat[i].b[x] * a_pat[j].b[y]; } }}

i

j

a_patb

b

Page 98: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-98

int main(){ for (i=0; i<N; i++) for (j=j+1; j<N; j++) { unsigned int pattern = a_pat[i] ^ a_pat[j]; if (_popcount[pattern] == 2) { for (x=0; x<a_pat[i].len; x++) r += 2 * a_pat[i].b[x] * a_pat[j].som_b; } }}

i

j

a_patb

b

Page 99: Les 5:  controletransfer-instructies en optimalisatie

Algoritmische optimalisaties

ca5-99

int main(){ for (i=0; i<N; i++) for (p=0; p<120; p++) unsigned int pat2 = a_pat[i] ^ tweebits[p]; j = a_bitpat[pat2]; if (j >= 0) { for (x=0; x<a_pat[i].len; x++) r += 2 * a_pat[i].b[x] * a_pat[j].som_b; } }}

i

pat2

a_patb

b

a_bitpat

j

Iteraties: N * 120 * (1000000/N) = 120 x 106

Page 100: Les 5:  controletransfer-instructies en optimalisatie

ca5-100

Pauze