Control Flow

Preview:

DESCRIPTION

Flow Chart

Citation preview

Organisasi Sistem Komputer

Bagian 6Bagian 6Control Flow

Sekolah Teknik Elektro dan InformatikaSekolah Teknik Elektro dan Informatika Institut Teknologi Bandung2009

1

Pembahasan

Condition CodeCo d t o CodePengesetan (setting)Pengujian (testing)

Aliran kontrolPercabangan if-then-elseBerbagai jenis loopPernyataan switch

2

Condition Code

Register satu bitC Fl Si FlCF Carry Flag SF Sign Flag

ZF Zero Flag OF Overflow FlagDi-set secara implisit dalam operasi aritmatika

S D taddl Src,DestKode C : t = a + b

CF di-set jika terdapat carry out pada most significant bitDigunakan untuk mendeteksi unsigned overflowDigunakan untuk mendeteksi unsigned overflow

ZF di-set jika t == 0SF di-set jika t < 0OF di-set jika two’s complement overflowOF di set jika two s complement overflow(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

Instruksi leal tidak men-set condition code

3

Pengesetan Condition Code

Pengesetan secara eksplisit dengan instruksi Compareg p g pcmpl Src2,Src1cmpl b,a seperti menghitung a-b tanpa men-set tujuannya CF di-set jika terdapat carry out pada most significant bitCF di-set jika terdapat carry out pada most significant bit

Digunakan untuk membandingkan unsigned ZF di-set jika a == bSF di set jika ( b) < 0SF di-set jika (a-b) < 0OF di-set jika terjadi two’s complement overflow(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

4

Pengesetan Condition Code

Pengesetan secara eksplisit dengan instruksi Testg p gtestl Src2,Src1

Men-set condition code berdasarkan nilai dari Src1 & Src2Biasanya digunakan pada operasi maskingBiasanya digunakan pada operasi masking

testl b,a seperti menghitung a&b tanpa men-set tujuannyaZF di-set jika a&b == 0ZF di-set jika a&b == 0SF di-set jika a&b < 0

5

Membaca Condition CodeInstruksi SetX

Men-set satu byte berdasarkan kombinasi dari condition codes

SetX Kondisi Deskripsi sete ZF Equal / Zero

t ZF N t E l / N t Zsetne ~ZF Not Equal / Not Zerosets SF Negative setns ~SF Nonnegative setg ~(SF^OF)&~ZF Greater (Signed) setge ~(SF^OF) Greater or Equal (Signed) setl (SF^OF) Less (Signed)( ) Less (Signed)setle (SF^OF)|ZF Less or Equal (Signed) seta ~CF&~ZF Above (unsigned) setb CF B l ( i d)setb CF Below (unsigned)

6

Membaca Condition CodeInstruksi SetX

Men-set satu byte berdasarkan kombinasi dari %eax %al%ahcondition codesMenggunakan satu dari delapan register

Digabung dengan 4 register integer pertama

%edx

%ecx

%dl%dh

%cl%chTidak mempengaruhi 3 byte lainnyaUmumnya menggunakan movzbl pada akhir baris

%ebx

%esi

%bl%bh

int gt (int x, int y)%edi

%esp

g ( , y){return x > y;

}Body %ebpmovl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : ysetg %al # al = x > y

perhatikanurutan

Body

setg %al # al x > ymovzbl %al,%eax # Zero rest of %eax

urutanterbalik!

7

Condition Code movl 8(%ebp),%ecxmovl 12(%ebp),%esi

char ctest(int a,int b,int c) {

cmpl %esi,%ecxsetl %alcmpl %ecx,%esisetb -1(%ebp){

char t1= a …… b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;

setb -1(%ebp)cmpw %cx,16(%ebp)setge -2(%ebp)movb %cl,%dl

char t4= ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

cmpb 16(%ebp),%dlsetne %blcmpl %esi,16(%ebp)setg -3(%ebp)return t1+t2+t3+t4+t5+t6;

}setg 3(%ebp)testl %ecx,%ecxsetg %dladdb -1(%ebp),%al

2( )Isilah titik-titik kosong pada program C di atas

addb -2(%ebp),%aladdb %bl,%aladdb -3(%ebp),%aladdb %dl,%al

dengan operator dan tipe data yang sesuai kode assembler sebelah kanan

,movsbl %al,%eax

8

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a …… b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; -1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

movl 8(%ebp),%ecx #get amovl 12(%ebp) %esi #get b

} -4

%edx

%ecx

%esi

movl 12(%ebp),%esi #get bcmpl %esi,%ecx #compare a:bsetl %al #compute t1cmpl %ecx %esi #compare b:a%esi

%ebp 0x110

cmpl %ecx,%esi #compare b:asetb -1(%ebp) #compute t2

9

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a …… b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; -1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

} -4

movl 8(%ebp),%ecx #get amovl 12(%ebp) %esi #get b%edx

%ecx

%esi

a

movl 12(%ebp),%esi #get bcmpl %esi,%ecx #compare a:bsetl %al #compute t1cmpl %ecx %esi #compare b:a%esi

%ebp 0x110

cmpl %ecx,%esi #compare b:asetb -1(%ebp) #compute t2

10

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a …… b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; -1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

} -4

movl 8(%ebp),%ecx #get amovl 12(%ebp) %esi #get b%edx

%ecx

%esi

a

b

movl 12(%ebp),%esi #get bcmpl %esi,%ecx #compare a:bsetl %al #compute t1cmpl %ecx %esi #compare b:a%esi

%ebp

b

0x110

cmpl %ecx,%esi #compare b:asetb -1(%ebp) #compute t2

11

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a …… b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; -1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1

} -4

movl 8(%ebp),%ecx #get amovl 12(%ebp) %esi #get b%edx

%ecx

%esi

a

b

movl 12(%ebp),%esi #get bcmpl %esi,%ecx #compare a:bsetl %al #compute t1cmpl %ecx %esi #compare b:a%esi

%ebp

b

0x110

cmpl %ecx,%esi #compare b:asetb -1(%ebp) #compute t2

12

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a < b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; -1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1

cmpl %esi %ecx #compare a:b

} -4

Penjelasan rinci :

%edx

%ecx

%esi

a

b

cmpl %esi,%ecx #compare a:bcompare double words : (a – b) = (%ecx - %esi)

setl %al #hitung t1set condition : less ( signed < )%esi

%ebp

b

0x110

set condition : less ( signed < )jika a<b, set %al

13

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

t2%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a < b;char t2= b …… ( …… )a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; t2-1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1

} -4

movl 8(%ebp),%ecx #get amovl 12(%ebp) %esi #get b%edx

%ecx

%esi

a

b

movl 12(%ebp),%esi #get bcmpl %esi,%ecx #compare a:bsetl %al #compute t1cmpl %ecx %esi #compare b:a%esi

%ebp

b

0x110

cmpl %ecx,%esi #compare b:asetb -1(%ebp) #compute t2

14

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

t2%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a < b;char t2= b < (unsigned)a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; t2-1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1

cmpl %ecx %esi #compare b:a

} -4

Penjelasan rinci :

%edx

%ecx

%esi

a

b

cmpl %ecx,%esi #compare b:acompare double words : (b – a) = (%esi - %ecx)

setb -1(%ebp) #hitung t2set condition : below ( unsigned < )%esi

%ebp

b

0x110

set condition : below ( unsigned < )jika b<a, set -1(%ebp)

15

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

t2%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a < b;char t2= b < (unsigned)a;char t3= ( …… )c …… ( …… )a;char t4= ( )a ( )c; t2

t3-1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1 cmpw %cx,16(%ebp) #compare c:asetge -2(%ebp) #hitung t3

} -4

%edx

%ecx

%esi

a

b

g ( p) # gmovb %cl,%dlcmpb 16(%ebp),%dl #compare a:csetne %bl #hitung t4

l % i 16(% b ) # b%esi

%ebp

b

0x110

cmpl %esi,16(%ebp) #compare c:bsetg -3(%ebp) #hitung t5

16

Condition Code Ofset

Alamat

0x124

0 12016

8

12

c

b

a

0x120

0x11c

0x118 char ctest(int a,int b,int c) {

16

t2%ebp 0

4

1

Rtn adr 0x114

0x110

0 10f

char t1= a < b;char t2= b < (unsigned)a;char t3= (short)c >= (short)a;char t4= ( )a ( )c; t2

t3-1 0x10f

0x10e

0x10d

char t4 ( …… )a …… ( …… )c;char t5= c …… b;char t6= a …… 0;return t1+t2+t3+t4+t5+t6;

}

-2

-3

0x10c %eax

% d

t1

cmpw %cx 16(%ebp) #compare c:a

} -4

Penjelasan rinci :

%edx

%ecx

%esi

a

b

cmpw %cx,16(%ebp) #compare c:acompare word : (c – a) = (16(%ebp) - %cx)

setge -2(%ebp) #hitung t3set condition : greater or equal ( signed >= )%esi

%ebp

b

0x110

set condition : greater or equal ( signed >= )jika c>=a, set -2(%ebp)

17

Program movl 8(%ebp),%ecxmovl 12(%ebp),%esi

char ctest(int a,int b,int c) {

cmpl %esi,%ecxsetl %alcmpl %ecx,%esisetb -1(%ebp){

char t1= a < b;char t2= b < (unsigned)a;char t3= (short)c >= (short)a;

setb -1(%ebp)cmpw %cx,16(%ebp)setge -2(%ebp)movb %cl,%dl

char t4= (char) a != (char) c;char t5= c > b;char t6= a > 0;return t1+t2+t3+t4+t5+t6;

cmpb 16(%ebp),%dlsetne %blcmpl %esi,16(%ebp)setg -3(%ebp)return t1+t2+t3+t4+t5+t6;

}setg 3(%ebp)testl %ecx,%ecxsetg %dladdb -1(%ebp),%al

2( )addb -2(%ebp),%aladdb %bl,%aladdb -3(%ebp),%aladdb %dl,%al,movsbl %al,%eax

18

Jumping

Instruksi jXMelompat ke bagian kode berbeda berdasarkan nilai condition codesMelompat ke bagian kode berbeda berdasarkan nilai condition codes

jX Kondisi Deskripsi jmp 1 Unconditional j ZF E l / Zje ZF Equal / Zerojne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegativejg ~(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)

19

Contoh Percabangan

_max:pushl %ebp Set

int max(int x, int y){if ( )

movl %esp,%ebp

movl 8(%ebp),%edxmovl 12(%ebp) %eax

SetUp

if (x > y)return x;

elsereturn y;

movl 12(%ebp),%eaxcmpl %eax,%edxjle L9movl %edx,%eax

Body

y;} L9:

movl %ebp,%esppopl %ebp Finishpopl %ebpret

Finish

20

Contoh Percabanganint goto_max(int x, int y){int rval = y;

Pada C terdapat instruksi “goto” untuk melakukanint ok = (x <= y);

if (ok)goto done;

rval = x;

goto untuk melakukan kontrol

Mendekati style pemrograman level mesinrval x;

done:return rval;

}

p g

Umumnya dianggap sebagai gaya pengkodean yang kurang baik

movl 8(%ebp),%edx # edx = xmovl 12(%ebp),%eax # eax = ycmpl %eax %edx # x : y

g

cmpl %eax,%edx # x : yjle L9 # if <= goto L9movl %edx,%eax # eax = x

L9: # Done:Diabaikan jika x ≤ y

21

Contoh Loop - Do-While

Kode Ci t f t d

Versi Gotoi t f t t (i t )int fact_do

(int x){int result = 1;

int fact_goto(int x){int result = 1;

loop:do {result *= x;x = x-1;

} while (x > 1);

presult *= x;x = x-1;if (x > 1)goto loop;} while (x > 1);

return result;}

goto loop;return result;

}

Menggunakan percabangan mundur dalam melakukan loop.Cabang akan dipilih jika kondisi “while” dipenuhi.Cabang akan dipilih jika kondisi while dipenuhi.

22

Kompilasi Loop - Do-WhileVersi Goto

int fact goto fact goto:

Bahasa Assemblyint fact_goto(int x)

{int result = 1;

_fact_goto:pushl %ebp # Setupmovl %esp,%ebp # Setupmovl $1,%eax # eax = 1

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

movl 8(%ebp),%edx # edx = x

L11:imull %edx,%eax # result *= xif (x > 1)

goto loop;return result;

}

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

Register%edx x

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

%eax result

23

Translasi Umum Do-WhileKode Cdo

Versi Gotoloop:

Bodywhile (Test);

Bodyif (Test)goto loop

Body berisi pernyataan-pernyataan CUmumnya berisi kumpulan pernyataan :

{{Statement1;Statement2;

T t d l h k i t k k b li d l i t

Statementn;}

Test adalah ekspresi untuk kembali dalam integer=0 diintepretasikan salah ≠0 diitepretasikan benar

24

Contoh Loop - While

Kode Cint fact while

Versi Goto Pertamaint fact while gotoint fact_while

(int x){int result = 1;

int fact_while_goto(int x)

{int result = 1;

while (x > 1) {result *= x;x = x-1;

};

loop:if (!(x > 1))goto done;

result *= x;};return result;

}

result x;x = x-1;goto loop;

done:t ltreturn result;

}

Apakah kode ekivalen dengan versi do-while ?p gHarus melompat keluar loop jika tes gagal.

25

Translasi Loop While Sebenarnya

Kode Ci t f t hil (i t )

Versi Goto Keduai t f t hil t 2int fact_while(int x)

{int result = 1;while (x > 1) {

int fact_while_goto2(int x)

{int result = 1;

result *= x;x = x-1;

};return result;

if (!(x > 1))goto done;

loop:result *= x;return result;

}result *= x;x = x-1;if (x > 1)goto loop;Menggunakan loop bagian

done:return result;

}

gg p gdalam sama seperti versi do-whileMenjaga bagian masuk j g gloop dengan tes tambahan

26

Translasi Umum - While

Kode Chil (T t)while (Test)

Body

Versi Do-While Versi GotoVersi Do-Whileif (!Test) goto done;

d

if (!Test)goto done;

loop:do

Bodywhile(Test);

done:

pBodyif (Test)goto loop;

done:done:

27

Contoh Loop - For

for (Init; Test; Update )int result;for (result = 1;

Bentuk Umumfor (Init; Test; Update )

Body

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

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

x = x*x;}

I i i li iInisialisasiresult = 1

Tesp != 0

Updatep = p >> 1

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

x = x*x;x = x*x;}

28

Translasi Umum - For

for (Init; Test; Update ) Init;

Versi WhileVersi For

Body while (Test ) {BodyUpdate ;

}}

Versi GotoI it

Versi Do-WhileInit;if (!Test) Init;

if (!Test)goto done;

loop:

if (!Test)goto done;

do {Body p

BodyUpdate ;if (Test)goto loop;

Update ;} while (Test)

done:goto loop;

done:

29

Kompilasi Loop ForVersi Goto

Init;result = 1;if (p == 0)Init;

if (!Test)goto done;

loop:

pgoto done;

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

Update ;if (Test)goto loop;

result *= x;x = x*x;p = p >> 1;if (p != 0)

Init TesBody

goto loop;done: goto loop;

done:

result = 1 p != 0

Update

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

p = p >> 1result = x;

x = x*x;}

30

Switch typedef enum{ADD, MULT, MINUS, DIV, MOD, BAD}

Alternatif lain pe cabangan

op_type;

char unparse_symbol(op_type op){percabangan

Banyak kondisiBaik jika kasus sedikit

{switch (op) {case ADD :return '+';

Baik jika kasus sedikitLambat jika banyak

Jump TableLookup branch target

case MULT:return '*';

case MINUS:return '-';

Menghindari kondisiDimungkinkan bila kasus merupakan konstanta integer

return ;case DIV:return '/';

case MOD:gGCC

Mengambil satu pilihan berdasarkan struktur case

return '%';case BAD:return '?';

}a }}

31

Struktur Jump Table

Code BlockTarg0:Targ0jtab:switch(op) {

Bentuk Switch Jump Table Jump Target

Code Block0

g

Code BlockTarg1:

Targ0

Targ1

Targ2

jtab:switch(op) {case val_0:

Block 0case val_1:

Code Block1

a g :

C d Bl kTarg2:

•••

Block 1• • •

case val_n-1:Block n–1 Code Block

2Targ2:

Targn-1Block n 1

}

P ki T l i ••target = JTab[op];

goto *target;

Perkiraan Translasi

Code Blockn–1

Targn-1:

32

Contoh Pernyataan Switch

Nilai Enumerasi :ADD 0

typedef enum{ADD, MULT, MINUS, DIV, MOD, BAD} ADD 0

MULT 1MINUS 2DIV 3MOD 4

{ADD, MULT, MINUS, DIV, MOD, BAD}op_type;

char unparse_symbol(op_type op){ MOD 4

BAD 5{switch (op) {• • •

}

unparse_symbol:pushl %ebp # Setup

}

Setup:

pushl %ebp # Setupmovl %esp,%ebp # Setupmovl 8(%ebp),%eax # eax = opcmpl $5,%eax # Compare op : 5

#ja .L49 # If > goto donejmp *.L57(,%eax,4) # goto Table[op]

33

Penjelasan ‘Setup’

Label simbolikL b l d l b t k dit j hk j di l t l h blLabel dalam bentuk .LXX diterjemahkan menjadi alamat oleh assembler

Struktur TabelSetiap tabel memerlukan 4 byteBase address at L57Base address at .L57

Jumpingjmp .L49

Jump target diperlihatkan oleh label L49Jump target diperlihatkan oleh label .L49jmp *.L57(,%eax,4)

Awal jump table diperlihatkan oleh label .L57Register %eax menyimpan opRegister %eax menyimpan opUntuk memperoleh offset ke dalam tabel harus dikalikan dengan faktor 4Fetch target dari alamat efektif .L57 + op*4

34

Jump Table

section rodata

Isi Tabel.L51:

Target dan Penyelesaian Fungsi.section .rodata

.align 4.L57:.long .L51 #Op = 0

movl $43,%eax # ’+’jmp .L49

.L52:movl $42 %eax # ’*’

.long .L52 #Op = 1

.long .L53 #Op = 2

.long .L54 #Op = 3

.long .L55 #Op = 4

movl $42,%eax # jmp .L49

.L53:movl $45,%eax # ’-’

Nilai Enumerasi ADD 0

.long .L55 #Op 4

.long .L56 #Op = 5jmp .L49

.L54:movl $47,%eax # ’/’jmp .L49ADD 0

MULT 1MINUS 2DIV 3

j p . 9.L55:

movl $37,%eax # ’%’jmp .L49

L56MOD 4BAD 5

.L56:movl $63,%eax # ’?’# Fall Through to .L49 35

Akhir Pernyataan Switch

.L49: # Done:movl %ebp,%esp # Finishp, ppopl %ebp # Finishret # Finish

PuzzleNilai apa yang dikembalikan (return value) jika op invalid?

JawabRegister %eax men-set op pada awal prosedurNilai ini merupakan return valueNilai ini merupakan return value

Keuntungan Jump TableDapat melakukan percabangan k-way dalam O(1) operasiDapat melakukan percabangan k way dalam O(1) operasi

36

Kode Obyek

SetuppLabel .L49 menjadi alamat 0x804875cLabel .L57 menjadi alamat 0x8048bc0

08048718 <unparse_symbol>:8048718:55 pushl %ebp8048719:89 e5 movl %esp %ebp8048719:89 e5 movl %esp,%ebp804871b:8b 45 08 movl 0x8(%ebp),%eax804871e:83 f8 05 cmpl $0x5,%eax8048721:77 39 ja 804875c <unparse_symbol+0x44>_8048723:ff 24 85 c0 8b jmp *0x8048bc0(,%eax,4)

37

Kode Obyek - Lanjutan

Jump TableTidak terlihat dalam kode disasemblerDapat diinspeksi menggunakan GDB

gdb code-examples(gdb) x/6xw 0x8048bc0(gdb) x/6xw 0x8048bc0

Memeriksa 6 hexadecimal format “words” (masing-masing 4-bytes)Untuk memperoleh dokumentasi format gunakan perintah “help x”

0x8048bc0 <_fini+32>: 0x08048730 0x08048737 0 080487400x08048740 0x080487470x08048750 0x080487570x08048757

38

Ekstraksi Jump Table

Jump Table disimpan dalam segmen data read only (.rodata)Berbagai nilai tetap diperlukan oleh kode

Dapat diperiksa dengan objdumpobjdump code-examples –s –-section=.rodata

M lih tk h l h l d l dii dik iMemperlihatkan hal-hal dalam segmen yang diindikasiSulit dibaca

Entri pada jump table diperlihatkan dalam urutan byte terbalik

Contents of section .rodata:8048bc0 30870408 37870408 40870408 47870408 0...7...@...G...8048bd0 50870408 57870408 46616374 28256429 P...W...Fact(%d)8048b 0 203d2025 6 640 00 43686172 203d2025 %ld Ch %

Mis., 30870408 berarti 0x08048730

8048be0 203d2025 6c640a00 43686172 203d2025 = %ld..Char = %…

39

Target Disassemble8048730:b8 2b 00 00 00 movl $0x2b,%eax8048735:eb 25 jmp 804875c <unparse_symbol+0x44>8048737:b8 2a 00 00 00 movl $0x2a %eax8048737:b8 2a 00 00 00 movl $0x2a,%eax804873c:eb 1e jmp 804875c <unparse_symbol+0x44>804873e:89 f6 movl %esi,%esi8048740:b8 2d 00 00 00 movl $0x2d,%eax8048745:eb 15 jmp 804875c <unparse_symbol+0x44>8048747:b8 2f 00 00 00 movl $0x2f,%eax804874c:eb 0e jmp 804875c <unparse_symbol+0x44>804874e:89 f6 movl %esi,%esi804874e:89 f6 movl %esi,%esi8048750:b8 25 00 00 00 movl $0x25,%eax8048755:eb 05 jmp 804875c <unparse_symbol+0x44>8048757:b8 3f 00 00 00 movl $0x3f,%eax

movl %esi,%esi tidak melakukan apapunDisisipkan untuk melakukan align terhadap instruksi agarDisisipkan untuk melakukan align terhadap instruksi agar diperoleh kinerja cache yang lebih baik

40

Mencocokan Target Disassemble

8048730 b8 2b 00 00 00 l8048730:b8 2b 00 00 00 movl8048735:eb 25 jmp8048737:b8 2a 00 00 00 movl804873c:eb 1e jmp

Entry0x08048730 j p

804873e:89 f6 movl8048740:b8 2d 00 00 00 movl8048745:eb 15 jmp8048747:b8 2f 00 00 00 movl

0x08048730

0x08048737 0x08048740 0x08048747 8048747:b8 2f 00 00 00 movl

804874c:eb 0e jmp804874e:89 f6 movl8048750:b8 25 00 00 00 movl

0x080487470x08048750 0x08048757

8048755:eb 05 jmp8048757:b8 3f 00 00 00 movl

41

Contoh Spare Switch

Penggunaan jump tabel tidak praktis

/* Return x/111 if x is multiple&& <= 999. -1 otherwise */ tidak praktis

dapat memerlukan lebih dari 1000 entri

Translasi menjadi if-the-

int div111(int x){switch(x) {case 0: return 0; Translasi menjadi if the

else dapat direduksi menjadi maksimum 9 kali pengujian

case 0: return 0;case 111: return 1;case 222: return 2;case 333: return 3;

444 4p g j

case 444: return 4;case 555: return 5;case 666: return 6;case 777: return 7;;case 888: return 8;case 999: return 9;default: return -1;}}

}

42

Code Spare Switchmovl 8(%ebp),%eax # get xcmpl $444,%eax # x:444je L8jg L16cmpl $111,%eax # x:111je L5

. . .L5:

l $1 %je L5jg L17testl %eax,%eax # x:0je L4j 14

movl $1,%eaxjmp L19

L6:movl $2,%eax

jmp L14

. . .

$ ,jmp L19

L7:movl $3,%eaxjmp L19

Membandingkan x terhadap setiap kasus yang mungkinMelompat ke tempat lain bergantung

jmp L19L8:

movl $4,%eaxjmp L19Melompat ke tempat lain bergantung

pada keluaran . . .

43

Struktur Kode Spare Switch

444444

111 7774

< >=

< >< >

0 222 555 8881 7

≠ =

=

< >=

=

≠= ≠=-1

333 666 9990 5 82

≠ = =

≠ = ≠ = ≠ =

≠ ≠=

-1 -1-1

Kasus dikelola dalam pohon binerk l k

93 6

Kinerja meningkat secara logaritmik

44

KesimpulanKontrol – Bahasa C

if-then-elseTeknik Standar

Seluruh loop dikonversi menjadido-whilewhileswitch

Seluruh loop dikonversi menjadi bentuk do-whilePernyataan swith yang besar menggunakan jump table

Kontrol – Bahasa AssemblerjumpConditional jump

Kondisi pada CISCMesin CISC umumnya memiliki register condition code j p

CompilerHarus membuat kode assembly untuk dapat

Kondisi pada RISCMenggunakan register umum untuk menyimpan informasi kondisiy p

mengimplementasikan kontrol yang lebih kompleks

45

Recommended