Top Banner
Carnegie Mellon 1 !"#$%&'()'*'+ -./0."11%&0 23 4"5%#5 !"#$!%&!'# $!%( *+,-./012.+ ,. 3.450,6- 789,649 " ,:  ;61,0-6< 765= !>< $>!% 2&56.7#6/.53 ?@+/8 A-8@+,< B@C6 DEF@GG@-.+< @+/ H-6I J69/6+
45

05-machine-basics.pdf

Jun 04, 2018

Download

Documents

rasromeo
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 1/45

Carnegie Mellon

1

!"#$%&'()'*'+ -./0."11%&0 23 4"5%#5

!"#$!%&!'#$!%( *+,-./012.+ ,. 3.450,6- 789,649" ,: ;61,0-6< 765= !>< $>!%

2&56.7#6/.53?@+/8 A-8@+,< B@C6 DEF@GG@-.+< @+/ H-6I J69/6+

Page 2: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 2/45

Carnegie Mellon

2

8/9":3 !"#$%&' -./0."11%&0 23 4"5%#5

! ;%56/.: /< 2&6'+ =./#'55/.5 "&9 ".#$%6'#67.'5! >? "55'1@+:? 1"#$%&' #/9'! A55'1@+: 4"5%#53 B'0%56'.5? /='."&95? 1/*'! 2&6./ 6/ CDE(EF

Page 3: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 3/45

Carnegie Mellon

3

2&6'+ CDE -./#'55/.5

! 8/6"++: 9/1%&"6' +"=6/=G9'5H6/=G5'.*'. 1".H'6

! I*/+7J/&".: 9'5%0&! A@1KL@-/9 1.45@2MG6 05 0+2G '>'N< O+,-./016/ O+ !PQ'! R//6/ 4.-6 S6@,0-69 @9 246 I.69 .+

! >/1=+'C %&56.7#J/& 5'6 #/1=76'. K>2L>M! T@+8 /OU6-6+, O+9,-012.+9 LO,: 4@+8 /OU6-6+, S.-4@,9

! A0,< .+G8 94@GG 90M96, 6+1.0+,6-6/ LO,: ;O+0V 5-.I-@49! F@-/ ,. 4@,1: 56-S.-4@+16 .S ?6/016/ *+9,-012.+ 76, 3.450,6-9

W?*73X! A0,< *+,6G :@9 /.+6 Y09, ,:@,Z

! *+ ,6-49 .S 9566/= ;699 9. S.- G.L 5.L6-=

Page 4: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 4/45

Page 5: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 5/45

Carnegie Mellon

5

2&6'+ CDE -./#'55/.5? #/&6W! !"#$%&' I*/+7J/&

! %'N !P'" >=%T

! \6+204 !PP% %=!T! \6+204&TTc !PPQ b="T! \6+204\-. !PP" N="T! \6+204 *** !PPP '=$T! \6+204 b $>>! b$T! 3.-6 $ B0. $>>N $P!T! 3.-6 OQ $>>' Q%!T

! A99'9 V'"67.'5! *+9,-012.+9 ,. 9055.-, 40G246/O@ .56-@2.+9! *+9,-012.+9 ,. 6+@MG6 4.-6 6d1O6+, 1.+/O2.+@G .56-@2.+9! e-@+9O2.+ S-.4 %$ MO,9 ,. Nb MO,9! T.-6 1.-69

Page 6: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 6/45

Carnegie Mellon

6

RNOU L6"6' /< 6$' A.6! 3.-6 OQ F@9L6GG $>!% !=bA

! V'"67.'53! b 1.-69 T@V b=> HFf 3G.1K 'b g@h9

Page 7: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 7/45

Carnegie Mellon

7

CDE >+/&'53 A9*"&#'9 !%#./ X'*%#'5 KA!XM

! ;%56/.%#"++:! RTB :@9 S.GG.L6/ Y09, M6:O+/ *+,6G! R GOhG6 MO, 9G.L6-< @ G., 1:6@56-

! 8$'&! ?61-0O,6/ ,.5 1O-10O, /69OI+6-9 S-.4 BOIO,@G ij0O546+, 3.-5= @+/

.,:6- /.L+L@-/ ,-6+/O+I 1.45@+O69! A0OG, D5,6-.+( ,.0I: 1.4562,.- ,. \6+204 b! B6C6G.56/ V'N#Nb< ,:6O- .L+ 6V,6+9O.+ ,. Nb MO,9

Page 8: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 8/45

Carnegie Mellon

8

2&6'+Y5 EF(4%6 ;%56/.:! 2&6'+ AZ'1=6'9 B"9%#"+ L$%[ <./1 2AUR 6/ 2AEF

! e.,@GG8 /OU6-6+, @-1:O,61,0-6 W*,@+O04X! iV610,69 *R%$ 1./6 .+G8 @9 G6I@18! \6-S.-4@+16 /[email protected]+2+I

! A!X L6'=='9 %& \%6$ I*/+7J/&".: L/+7J/&! V'N#Nb W+.L 1@GG6/ ^RTBNb`X

! 2&6'+ V'+6 ]@+%0"6'9 6/ V/#75 /& 2AEF! F@-/ ,. @/4O, 4O9,@K6 .- ,:@, RTB O9 M6h6-

! RNNF3 2&6'+ A&&/7&#'5 I!EF8 'C6'&5%/& 6/ 2AUR! iV,6+/6/ T64.-8 Nb#MO, e61:+.G.I8! RG4.9, O/6+21@G ,. V'N#NbZ

! A++ @76 +/\('&9 CDE =./#'55/.5 57==/.6 CDE(EF! A0,< G.,9 .S 1./6 92GG -0+9 O+ %$#MO, 4./6

Page 9: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 9/45

Carnegie Mellon

9

]7. >/*'."0'

! 2AUR! e:6 ,-@/O2.+@G V'N! shark> gcc –m32 hello.c

! CDE(EF!

e:6 646-IO+I 9,@+/@-/! shark> gcc hello.c! shark> gcc –m64 hello.c

! -.'5'&6"J/&! A..K 5-696+,9 *R%$ O+ 7612.+9 %=!k%=!$! 3.C6-9 V'N#Nb O+ %=!%! g6 LOGG 1.C6- M.,: 9O40G,@+6.09G8! 7.46 G@M9 LOGG M6 M@96/ .+ V'N#Nb< .,:6-9 .+ *R%$

Page 10: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 10/45

Carnegie Mellon

10

8/9":3 !"#$%&' -./0."11%&0 23 4"5%#5

! ;%56/.: /< 2&6'+ =./#'55/.5 "&9 ".#$%6'#67.'5! >? "55'1@+:? 1"#$%&' #/9'! A55'1@+: 4"5%#53 B'0%56'.5? /='."&95? 1/*'! 2&6./ 6/ CDE(EF

Page 11: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 11/45

Carnegie Mellon

11

X'^&%J/&5

! A.#$%6'#67.'3 K"+5/ 2LA3 %&56.7#J/& 5'6 ".#$%6'#67.'M 8$'=".65 /< " =./#'55/. 9'5%0& 6$"6 /&' &''95 6/ 7&9'.56"&96/ \.%6' "55'1@+: #/9'W

! iV@45G69( O+9,-012.+ 96, 9561Ol1@2.+< -6IO9,6-9=

! !%#./".#$%6'#67.'3 21=+'1'&6"J/& /< 6$' ".#$%6'#67.'W! iV@45G69( 1@1:6 9Of69 @+/ 1.-6 S-6j06+18=

! IC"1=+' 2LA5 K2&6'+M3 2AUR? CDE(EF

Page 12: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 12/45

Carnegie Mellon

12

>-_

A55'1@+: -./0."11'.Y5 `%'\

-./0."11'.(`%5%@+' L6"6'! ->3 -./0."1 #/7&6'.

! R//-699 .S +6V, O+9,-012.+! 3@GG6/ ^i*\` W*R%$X .- ^?*\` WV'N#NbX

! B'0%56'. ^+'! F6@COG8 096/ 5-.I-@4 /@,@

! >/&9%J/& #/9'5! 7,.-6 9,@,09 O+S.-4@2.+ @M.0, 4.9,

-616+, @-O,:4621 .- G.IO1@G .56-@2.+! a96/ S.- 1.+/O2.+@G M-@+1:O+I

->

B'0%56'.5

!'1/.:

>/9'X"6"L6"#H

R//-69969B@,@

*+9,-012.+9>/&9%J/&>/9'5

! !'1/.:! A8,6 @//-699@MG6 @--@8! 3./6 @+/ 096- /@,@! 7,@1K ,. 9055.-, 5-.16/0-69

ll

Page 13: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 13/45

Carnegie Mellon

13

'%1'

'%1'

2,*#)3

2,*#)3

>/1=%+'. Kgcc -S M

A55'1@+'. Kgcc /. as M

)%&H'. Kgcc /. ld M

> =./0."1 K p1.c p2.c M

A51 =./0."1 K p1.s p2.s M

]@a'#6 =./0."1 K p1.o p2.o M

IC'#76"@+' =./0."1 K p M

L6"J# +%@.".%'5K.a M

87.&%&0 > %&6/ ]@a'#6 >/9'! 3./6 O+ lG69 p1.c p2.c !

3.45OG6 LO,: 1.44@+/( gcc –O1 p1.c p2.c -o p ! a96 M@9O1 .524Of@2.+9 W-O1 X! \0, -690G2+I MO+@-8 O+ lG6 p

C i M ll

Page 14: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 14/45

Carnegie Mellon

14

>/1=%+%&0 2&6/ A55'1@+:> >/9' K=OW#Mint sum(int x, int y){

int t = x+y;return t;

}

b'&'."6'9 2AUR A55'1@+:sum:

pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax

addl 8(%ebp),%eax

popl %ebpret

]@6"%& K/& 5$".H 1"#$%&'M \%6$ #/11"&9

gcc –O1 –m32 –S p1.c

-./97#'5 ^+' p1.s

4#)*,*5 3 c%++ 0'6 *'.: 9%d'.'&6 .'57+65 /& &/&(L$".H1"#$%&'5 KA&9.'\ )%&7C? !"# ]L(e? fM 97' 6/ 9%d'.'&6*'.5%/&5 /< 0## "&9 9%d'.'&6 #/1=%+'. 5'g&05W

C i M ll

Page 15: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 15/45

Carnegie Mellon

15

A55'1@+: >$"."#6'.%5J#53 X"6" 8:='5!

h2&6'0'.i 9"6" /< O? R? /. F @:6'5! B@,@ C@G069! R//-69969 W0+,856/ 5.O+,6-9X

!

V+/"J&0 =/%&6 9"6" /< F? D? /. ON @:6'5

! j/ "00.'0"6' 6:='5 57#$ "5 "..":5 /. 56.7#67.'5! m09, 1.+2I0.09G8 @GG.1@,6/ M8,69 O+ 464.-8

C gi M ll

Page 16: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 16/45

Carnegie Mellon

16

A55'1@+: >$"."#6'.%5J#53 ]='."J/&5

! -'.</.1 ".%6$1'J# <7&#J/& /& .'0%56'. /. 1'1/.: 9"6"

! 8."&5<'. 9"6" @'6\''& 1'1/.: "&9 .'0%56'.! ;.@/ /@,@ S-.4 464.-8 O+,. -6IO9,6-!

7,.-6 -6IO9,6- /@,@ O+,. 464.-8

! 8."&5<'. #/&6./+! a+1.+/O2.+@G Y0459 ,.&S-.4 5-.16/0-69!

3.+/O2.+@G M-@+1:69

Carnegie Mellon

Page 17: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 17/45

Carnegie Mellon

17

>/9' </. sum

0x08483f4 <sum>:0x550x890xe50x8b0x450x0c0x030x450x080x5d0xc3

]@a'#6 >/9'

! A55'1@+'.! e-@+9G@,69.s O+,. .o! AO+@-8 6+1./O+I .S 6@1: O+9,-012.+! n6@-G8#1.45G6,6 O4@I6 .S 6V610,@MG6 1./6! TO99O+I GO+K@I69 M6,L66+ 1./6 O+ /OU6-6+,

lG69! )%&H'.

! ?69.GC69 -6S6-6+169 M6,L66+ lG69! 3.4MO+69 LO,: 9,@21 -0+#246 GOM-@-O69

! i=I=< 1./6 S.- malloc ? printf! 7.46 GOM-@-O69 @-6!"#$%&'$((" (&#*+!

! ;O+KO+I .110-9 L:6+ 5-.I-@4 M6IO+96V6102.+

o 8/6"+ /< OO @:6'5

o I"#$ %&56.7#J/&

O? R? /. U @:6'5o L6".65 "6 "99.'55

0x401040

Carnegie Mellon

Page 18: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 18/45

Carnegie Mellon

18

!"#$%&' 2&56.7#J/& IC"1=+'! > >/9'

! R// ,L. 9OI+6/ O+,6I6-9

! A55'1@+:! R// $ b#M8,6 O+,6I6-9

! ^;.+I` L.-/9 O+ H33 5@-G@+16! 7@46 O+9,-012.+ L:6,:6- 9OI+6/

.- 0+9OI+6/! D56-@+/9(

x3 ?6IO9,6- %eaxy3 T64.-8 !k %ebp+8]

t 3 ?6IO9,6- %eax p ?6,0-+ S0+12.+ C@G06 O+%eax

! ]@a'#6 >/9'! %#M8,6 O+9,-012.+! 7,.-6/ @, @//-699

0x80483fa

int t = x+y;

addl 8(%ebp),%eax

0x80483fa: 03 45 08

L%1%+". 6/ 'C=.'55%/&3

x += y!/.' =.'#%5'+:3

int eax;int *ebp;

eax += ebp[2]

Carnegie Mellon

Page 19: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 19/45

Carnegie Mellon

19

X%5"55'1@+'9

X%5"55'1@+%&0 ]@a'#6 >/9'

! X%5"55'1@+'.

objdump -d p! a96S0G ,..G S.- 6V@4O+O+I .MY61, 1./6! R+@G8f69 MO, 5@h6-+ .S 96-O69 .S O+9,-012.+9! \-./0169 @55-.VO4@,6 -6+/O2.+ .S @9964MG8 1./6! 3@+ M6 -0+ .+ 6O,:6- a.out W1.45G6,6 6V610,@MG6X .-.o lG6

080483f4 <sum>:80483f4: 55 push %ebp80483f5: 89 e5 mov %esp,%ebp80483f7: 8b 45 0c mov 0xc(%ebp),%eax80483fa: 03 45 08 add 0x8(%ebp),%eax

80483fd: 5d pop %ebp80483fe: c3 ret

Carnegie Mellon

Page 20: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 20/45

Carnegie Mellon

20

X%5"55'1@+'9

Dump of assembler code for function sum:0x080483f4 <sum+0>: push %ebp0x080483f5 <sum+1>: mov %esp,%ebp0x080483f7 <sum+3>: mov 0xc(%ebp),%eax0x080483fa <sum+6>: add 0x8(%ebp),%eax0x080483fd <sum+9>: pop %ebp0x080483fe <sum+10>: ret

A+6'.&"6' X%5"55'1@+:

! c%6$%& 09@ X'@700'.

gdb pdisassemble sum! BO9@9964MG6 5-.16/0-6

x/11xb sum! iV@4O+6 ,:6 !! M8,69 9,@-2+I @, sum

]@a'#60x080483f4:

0x550x890xe50x8b0x450x0c0x030x450x080x5d

0xc3

Carnegie Mellon

Page 21: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 21/45

Carnegie Mellon

21

c$"6 >"& @' X%5"55'1@+'9l

! A&:6$%&0 6$"6 #"& @' %&6'.=.'6'9 "5 'C'#76"@+' #/9'! X%5"55'1@+'. 'C"1%&'5 @:6'5 "&9 .'#/&56.7#65 "55'1@+: 5/7.#'

% objdump -d WINWORD.EXE

WINWORD.EXE: file format pei-i386

No symbols in "WINWORD.EXE".

Disassembly of section .text:30001000 <.text>:30001000: 55 push %ebp30001001: 8b ec mov %esp,%ebp30001003: 6a ff push $0xffffffff

30001005: 68 90 10 00 30 push $0x300010903000100a: 68 91 dc 4c 30 push $0x304cdc91

Carnegie Mellon

Page 22: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 22/45

Carnegie Mellon

22

8/9":3 !"#$%&' -./0."11%&0 23 4"5%#5

! ;%56/.: /< 2&6'+ =./#'55/.5 "&9 ".#$%6'#67.'5! >? "55'1@+:? 1"#$%&' #/9'! A55'1@+: 4"5%#53 B'0%56'.5? /='."&95? 1/*'! 2&6./ 6/ CDE(EF

Carnegie Mellon

Page 23: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 23/45

Carnegie Mellon

23

2&6'0'. B'0%56'.5 K2AURM

%eax%ecx

%edx

%ebx

%esi

%edi

%esp

%ebp

%ax

%cx

%dx

%bx

%si

%di

%sp

%bp

%ah

%ch

%dh

%bh

%al

%cl

%dl

%bl

OE(@%6 *%.67"+ .'0%56'.5K@"#H\".95 #/1="J@%+%6:M

0 ' & ' . "

+ = 7 . = / 5 '

accumulate

counter

data

base

sourceindex

destinationindex

stack pointer

base pointer

].%0%&K1/56+: /@5/+'6'M

Carnegie Mellon

Page 24: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 24/45

Carnegie Mellon

24

!/*%&0 X"6"3 2AUR! !/*%&0 X"6"

movl 6-7)8% ?&%+' 3

! ]='."&9 8:='5! 9$$%:,#'%; 3.+9,@+, O+,6I6- /@,@

! iV@45G6($0x400 ?$-533 ! ;OK6 3 1.+9,@+,< M0, 5-6lV6/ LO,: ‘$’! i+1./6/ LO,: !< $< .- b M8,69

! <%5,+'%); D+6 .S ' O+,6I6- -6IO9,6-9! iV@45G6(%eax, %edx! A0, %esp @+/ %ebp -696-C6/ S.- 9561O@G 096! D,:6-9 :@C6 9561O@G 0969 S.- 5@-210G@- O+9,-012.+9

! .%$-)3; b 1.+96102C6 M8,69 .S 464.-8 @, @//-699 IOC6+ M8 -6IO9,6-! 7O45G69, 6V@45G6((%eax)!

[email protected] .,:6- ^@//-699 4./69`

%eax%ecx%edx%ebx%esi

%edi%esp%ebp

Carnegie Mellon

Page 25: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 25/45

g

25

movl ]='."&9 >/1@%&"J/&5

=#**-' :- $%$-)3>$%$-)3 ')#*+?%) @,'A # +,*5B% ,*+')78C-*

movl

9$$

<%5

.%$

<%5

.%$

<%5

.%$

<%5

L/7.#' X'56 > A&"+/0

movl $0x4,%eax temp = 0x4;

movl $-147,(%eax) *p = -147;

movl %eax,%edx temp2 = temp1;

movl %eax,(%edx) *p = temp;

movl (%eax),%edx temp = *p;

L.#?X'56

Carnegie Mellon

Page 26: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 26/45

g

26

L%1=+' !'1/.: A99.'55%&0 !/9'5

! j/.1"+ KBM !'1kB'0kBmm! ?6IO9,6- ? 9561Ol69 464.-8 @//-699! R:@Z \.O+,6- /6-6S6-6+1O+I O+ 3

movl (%ecx),%eax

! X%5=+"#'1'&6 XKBM !'1kB'0kBmnXm! ?6IO9,6- ? 9561Ol69 9,@-, .S 464.-8 -6IO.+! 3.+9,@+, /O95G@1646+, B 9561Ol69 .U96,

movl 8(%ebp),%edx

Carnegie Mellon

Page 27: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 27/45

g

27

IC"1=+' /< L%1=+' A99.'55%&0 !/9'5

void swap(int *xp, int *yp){

int t0 = *xp;int t1 = *yp;*xp = t1;

*yp = t0;} 4/9:

L'6_=

V%&%5

swap:

pushl %ebp movl %esp,%ebp pushl %ebx

movl 8(%ebp), %edx

movl 12(%ebp), %eax movl (%edx), %ecx movl (%eax), %ebx movl %ebx, (%edx) movl %ecx, (%eax)

popl %ebx popl %ebp

ret

Carnegie Mellon

Page 28: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 28/45

28

_5%&0 L%1=+' A99.'55%&0 !/9'5

void swap(int *xp, int *yp){

int t0 = *xp;int t1 = *yp;*xp = t1;

*yp = t0;}

swap:

pushl %ebp movl %esp,%ebp pushl %ebx

movl 8(%ebp), %edx

movl 12(%ebp), %eax movl (%edx), %ecx movl (%eax), %ebx movl %ebx, (%edx) movl %ecx, (%eax)

popl %ebx popl %ebp

ret

4/9:

L'6_=

V%&%5

Carnegie Mellon

Page 29: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 29/45

29

_&9'.56"&9%&0 L\"=

void swap(int *xp, int *yp){

int t0 = *xp;int t1 = *yp;*xp = t1;*yp = t0;

}

L6"#HK%& 1'1/.:M

B'0%56'. `"+7'

%edx xp

%eax yp%ecx t0%ebx t1

yp

xp

B6& "9.

]+9 o ebp %ebp04

8

12

]d5'6

p

pp

]+9 o ebx -4 %esp

movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

Carnegie Mellon

Page 30: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 30/45

30

_&9'.56"&9%&0 L\"=

0x120

0x124

B6& "9.

%ebp 04

8

12

]d5'6

-4

123

456

A99.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

Page 31: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 31/45

Carnegie Mellon

Page 32: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 32/45

32

_&9'.56"&9%&0 L\"=

0x120

0x124

B6& "9.

%ebp 04

8

12

]d5'6

-4

123

456

A99.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x120

0x104

0x124

0x124

movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

Page 33: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 33/45

Carnegie Mellon

Page 34: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 34/45

34

_&9'.56"&9%&0 L\"=

0x120

0x124

B6& "9.

%ebp 04

8

12

]d5'6

-4

123

456

A99.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x120

0x124

123

0x104

123

456

movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

Carnegie Mellon

Page 35: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 35/45

35

456

456_&9'.56"&9%&0 L\"=

0x120

0x124

B6& "9.

%ebp 04

8

12

]d5'6

-4

A99.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

0x120

0x124

123

456

Carnegie Mellon

Page 36: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 36/45

36

_&9'.56"&9%&0 L\"=

0x120

0x124

B6& "9.

%ebp 04

8

12

]d5'6

-4

456

123

A99.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104 movl 8(%ebp), %edx # edx = xp movl 12(%ebp), %eax # eax = yp movl (%edx), %ecx # ecx = *xp (t0) movl (%eax), %ebx # ebx = *yp (t1) movl %ebx, (%edx) # *xp = t1 movl %ecx, (%eax) # *yp = t0

0x120

0x124

123

456

Carnegie Mellon

Page 37: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 37/45

37

>/1=+'6' !'1/.: A99.'55%&0 !/9'5! !/56 b'&'."+ V/.1

XKB@?B%?LM !'1kB'0kB@mnLqB'0kB%mn Xm! B( 3.+9,@+, ^/O95G@1646+,` !< $< .- b M8,69! ?M( A@96 -6IO9,6-( R+8 .S ' O+,6I6- -6IO9,6-9! ?O( *+/6V -6IO9,6-( R+8< 6V165, S.-%esp

! a+GOK6G8 8.0E/ 096%ebp< 6O,:6-! 7( 71@G6( !< $< b< .- ' W,-" .-+/+ #0%1+2/3 X

! L='#%"+ >"5'5

KB@?B%M !'1kB'0kB@mnB'0kB%mm

XKB@?B%M !'1kB'0kB@mnB'0kB%mnXmKB@?B%?LM !'1kB'0kB@mnLqB'0kB%mm

Carnegie Mellon

Page 38: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 38/45

38

8/9":3 !"#$%&' -./0."11%&0 23 4"5%#5

! ;%56/.: /< 2&6'+ =./#'55/.5 "&9 ".#$%6'#67.'5! >? "55'1@+:? 1"#$%&' #/9'! A55'1@+: 4"5%#53 B'0%56'.5? /='."&95? 1/*'! 2&6./ 6/ CDE(EF

Carnegie Mellon

Page 39: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 39/45

39

X"6" B'=.'5'&6"J/&53 2AUR n CDE(EF

! L%r'5 /< > ]@a'#65 K%& 4:6'5M3 B@,@ e856 H6+6-O1 %$#MO, *+,6G *R%$ V'N#Nb

! 0+9OI+6/ b b b! O+, b b b! G.+I O+, b b '! 1:@- ! ! !! 9:.-, $ $ $! _.@, b b b! /.0MG6 ' ' '! G.+I /.0MG6 ' !>&!$ !>&!N! 1:@- r b b '

p D- @+8 .,:6- 5.O+,6-

Page 40: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 40/45

Carnegie Mellon

Page 41: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 41/45

41

2&56.7#J/&5

! )/&0 \/.9 l KF 4:6'5M s t7"9 \/.9 q KD 4:6'5M

! j'\ %&56.7#J/&53! movl ! movq !

addl !

addq ! sall ! salq ! 6,1=

!

UR(@%6 %&56.7#J/&5 6$"6 0'&'."6' UR(@%6 .'57+65! 76, :OI:6- .-/6- MO,9 .S /692+@2.+ -6IO9,6- ,. 0 ! iV@45G6(addl

Page 42: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 42/45

Page 43: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 43/45

Carnegie Mellon

Page 44: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 44/45

44

EF(@%6 #/9' </. +/&0 %&6 5\"=

! EF(@%6 9"6"! B@,@ :6G/ O+ -6IO9,6-9%rdx @+/ %rax ! movq .56-@2.+

! ^j` 9,@+/9 S.- j0@/#L.-/

void swap(long *xp, long *yp){

long t0 = *xp;long t1 = *yp;*xp = t1;*yp = t0;

}

4/9:

L'6_=

V%&%5

swap_l:

movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi)

ret

Carnegie Mellon

Page 45: 05-machine-basics.pdf

8/14/2019 05-machine-basics.pdf

http://slidepdf.com/reader/full/05-machine-basicspdf 45/45

!"#$%&' -./0."11%&0 23 L711".:

! ;%56/.: /< 2&6'+ =./#'55/.5 "&9 ".#$%6'#67.'5! iC.G02.+@-8 /69OI+ G6@/9 ,. 4@+8 j0O-K9 @+/ @-2S@1,9

! >? "55'1@+:? 1"#$%&' #/9'! 3.45OG6- 409, ,-@+9S.-4 9,@,646+,9< 6V5-699O.+9< 5-.16/0-69 O+,.

G.L#G6C6G O+9,-012.+ 96j06+169

! A55'1@+: 4"5%#53 B'0%56'.5? /='."&95? 1/*'! e:6 V'N 4.C6 O+9,-012.+9 1.C6- LO/6 -@+I6 .S /@,@ 4.C646+,

S.-49

! 2&6./ 6/ CDE(EF! R [email protected] /65@-,0-6 S-.4 ,:6 9,8G6 .S 1./6 966+ O+ *R%$