0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/supplements/05-machin… · denison...

22
Denison University 1 !"#$%&'()'*'+ -./0."11%&0 23 4"5%#5 !"#$%&' )*+,-./01-* +- !-23/+4, "56+426 !"#$%&'$(%) 78-296 !: ;,466-/. Denison University 2 2&6'+ 789 -./#'55/.5 :/6"++; </1%&"6' +"=6/=><'5?6/=>5'.*'. 1".?'6 @*/+AB/&".; <'5%0& ;90<=9,.6 0-2391>?4 /3 /*1? %@%AB C*+,-./04. C* &DE% F..4. 2-,4 G49+/,46 96 124 H-46 -* C/1=+'7 %&56.A#B/& 5'6 #/1=A6'. DC2ECF I9*5 .CJ4,4*+ C*6+,/01-*6 =C+8 29*5 .CJ4,4*+ G-,29+6 ;/+B -*?5 629?? 6/>64+ 4*0-/*+4,4. =C+8 KC*/L 3,-H,926 M9,. +- 29+08 34,G-,29*04 -G N4./04. )*6+,/01-* "4+ !-23/+4,6 ON)"!P ;/+B )*+4? 896 .-*4 Q/6+ +89+R )* +4,26 -G 6344.: K466 6- G-, ?-= 3-=4,:

Upload: others

Post on 01-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

1

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

!"#$%&'()*+,-./01-*(+-(!-23/+4,("56+426(

!"#$%&'$(%)*78-296(!:(;,466-/.

Denison University

2

2&6'+,789,-./#'55/.5:/6"++;,</1%&"6',+"=6/=><'5?6/=>5'.*'.,1".?'6

@*/+AB/&".;,<'5%0&;90<=9,.6(0-2391>?4(/3(/*1?(%@%AB(C*+,-./04.(C*(&DE%F..4.(2-,4(G49+/,46(96(124(H-46(-*

C/1=+'7,%&56.A#B/&,5'6,#/1=A6'.,DC2ECFI9*5(.CJ4,4*+(C*6+,/01-*6(=C+8(29*5(.CJ4,4*+(G-,29+6

;/+B(-*?5(629??(6/>64+(4*0-/*+4,4.(=C+8(KC*/L(3,-H,926M9,.(+-(29+08(34,G-,29*04(-G(N4./04.()*6+,/01-*("4+(!-23/+4,6(ON)"!P;/+B()*+4?(896(.-*4(Q/6+(+89+R

)*(+4,26(-G(6344.:((K466(6-(G-,(?-=(3-=4,:

Page 2: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

3

2&6'+,789,@*/+AB/&3,!%+'56/&'5! "#$%! &#'%! ()#*+,+'-)+! ./08G89, HIJ8, KIL, M(HG

SC,6+(&A#>C+(3,-0466-,:((;96C6(G-,();I(T!(U(VW"&I;(9..,466(63904

N89, HI8M, KJML, H9(NN,SC,6+(X$(>C+(3,-0466-,(B(,4G4,,4.(+-(96()FX$F..4.(YZ9+(9..,466C*H[!939>?4(-G(,/**C*H(\*CLX$#>C+(KC*/L]H00(/646(*-(C*6+,/01-*6(C*+,-./04.(C*(?9+4,(2-.4?6

-'&BA1,OP, KGGO, HKM!, K8GG(N8GGSC,6+(A^#>C+(3,-0466-,B(,4G4,,4.(+-(96(L%A#A^

C/.',%J, KGG8, JNH!, K99J(NNNN

Denison University

4

2&6'+,789,-./#'55/.53,Q*'.*%'R

_%A#A^(](`IA^+

_%A#X$])FX$

_%A#&A 8G89

K89N89O89

-'&BA1-'&BA1,!!S

-'&BA1,222

-'&BA1,O

-'&BA1,O@

-'&BA1,OP

C/.',K,TA/C/.',%J

2U3,/V'&,.'<'W&'<,"5,+"6'56,2&6'+,".#$%6'#6A.'

B1'

U.#$%6'#6A.'5 -./#'55/.5

..1

223

2234

2235

2236

Page 3: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

5

2&6'+,789,-./#'55/.5X,#/&6<Y!"#$%&',@*/+AB/&

X%A( &D%a( @:XI(T4*1/2( &DDX( X:&IT4*1/2]II_( &DDE( ^:aIT4*1/2T,-( &DDa( A:aIT4*1/2()))( &DDD( %:$IT4*1/2(^( $@@&( ^$I!-,4($(V/-( $@@A( $D&I!-,4(CE( $@@%( EX&I

U<<'<,P'"6A.'5)*6+,/01-*6(+-(6/33-,+(2/?124.C9(-34,91-*6

T9,9??4?(-34,91-*6(-*(&B($B(9*.(^#>5+4(.9+9B(>-+8(C*+4H4,(U(ST)*6+,/01-*6(+-(4*9>?4(2-,4(4b0C4*+(0-*.C1-*9?(-34,91-*6

Denison University

6

!/.',2&Z/.1"B/&2&6'+,=./#'55/.5,D[%?%='<%"F2&6'+,1%#./".#$%6'#6A.'5

Page 4: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

7

\'R,E='#%'53,%"9OX,6$'&,2-PX,6$'&,26"&%A1X],! "#$%! &#'%! ()#*+,+'-)+26"&%A1, KGGH, HG!

SC,6+(68-+(9+(A^#>C+(9,08C+40+/,4'(c,6+(09??4.()FA^N9.C09??5(*4=(C*6+,/01-*(64+(.46CH*4.(G-,(8CH8(34,G-,29*04!9*(,/*(4LC61*H()FX$(3,-H,926

W*#>-9,.(YL%A(4*HC*4[d-C*+(3,-Q40+(=C+8(M4=?4e#T90<9,.

26"&%A1,K, KGGK, KKH!;CH(34,G-,29*04(>--6+

26"&%A1,K,TA"+(C/.', KGG9, HYJ426"&%A1,$"5,&/6,6"?'&,/^,%&,1".?'6=+"#'

K90<(-G(>90<=9,.(0-2391>C?C+5B(*-(H--.(0-23C?4,(6/33-,+B(T4*1/2(^(H-+(+--(H--.

Denison University

8

789,C+/&'53,U<*"&#'<,!%#./,T'*%#'5,DU!TF

_%56/.%#"++;FIV(896(G-??-=4.(Q/6+(>48C*.()*+4?F(?Ce?4(>C+(6?-=4,B(9(?-+(084934,

:$'&N40,/C+4.(+-3(0C,0/C+(.46CH*4,6(G,-2(VCHC+9?(`f/C324*+(!-,3:(9*.(-+84,(.-=*=9,.(+,4*.C*H(0-239*C46;/C?+(W3+4,-*'(+-/H8(0-2341+-,(+-(T4*1/2(^V4g4?-34.(L%A#A^B(+84C,(-=*(4L+4*6C-*(+-(A^(>C+6

Page 5: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

9

2&6'+`5,9O(4%62&6'+,Ua'1=6'<,b"<%#"+,E$%V,Z./1,2UNK,6/,2U9O

7-+9??5(.CJ4,4*+(9,08C+40+/,4(O)+9*C/2P`L40/+46()FX$(0-.4(-*?5(96(?4H905T4,G-,29*04(.C6933-C*1*H

U!T,E6'=='<,%&,R%6$,@*/+AB/&".;,E/+AB/&L%A#A^(O*-=(09??4.(YFIVA^[P

2&6'+,P'+6,Qc+%0"6'<,6/,P/#A5,/&,2U9OM9,.(+-(9.2C+(2C6+9<4(-,(+89+(FIV(C6(>4e4,

KGGO3,2&6'+,U&&/A&#'5,@!9O:,'76'&5%/&,6/,2UNK`L+4*.4.(I42-,5(A^#>C+(7408*-?-H5F?2-6+(C.4*109?(+-(L%A#A^R

U++,cA6,+/R('&<,789,=./#'55/.5,5A==/.6,789(9O;/+B(?-+6(-G(0-.4(61??(,/*6(C*(X$#>C+(2-.4

Denison University

10

QA.,C/*'."0'2UNK

784(+,9.C1-*9?(L%A

789(9O>@!9O:784(424,HC*H(6+9*.9,.

-.'5'&6"B/&;--<(3,464*+6()FX$(C*("401-*6(X:&hX:&$!-g4,6(L%A#A^(C*(X:&Xi4(=C??(0-g4,(>-+8(6C2/?+9*4-/6?5

Page 6: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

11

T'W&%B/&5U.#$%6'#6A.'3,D"+5/,%&56.A#B/&,5'6,".#$%6'#6A.'3,2EUF,:$',=".65,/Z,",=./#'55/.,<'5%0&,6$"6,/&',&''<5,6/,A&<'.56"&<,6/,R.%6',"55'1c+;,#/<'Y,

`L923?46'((C*6+,/01-*(64+(6340Cc091-*B(,4HC6+4,6:

!%#./".#$%6'#6A.'3,21=+'1'&6"B/&,/Z,6$',".#$%6'#6A.'Y`L923?46'(09084(6Cj46(9*.(0-,4(G,4f/4*05:

@7"1=+',2EU5,D2&6'+F3,789X,2UX,2-P

Denison University

12

C-dU55'1c+;,-./0."11'.`5,e%'R

T,-H,9224,#kC6C>?4("+9+4-C3,-./0."1,#/A&6'.

U<<.'55,/Z,&'76,%&56.A#B/&C"++'<,f@2-g,D2UNKF,/.,fb2-g,D789(9OF

b'0%56'.,W+'_'"*%+;,A5'<,=./0."1,<"6"

C/&<%B/&,#/<'5E6/.',56"6A5,%&Z/.1"B/&,"c/A6,1/56,.'#'&6,".%6$1'B#,/='."B/&d5'<,Z/.,#/&<%B/&"+,c."&#$%&0

-Cb'0%56'.5

!'1/.;W>Q40+(!-.4T,-H,92(V9+9

W"(V9+9

F..,46646

V9+9

)*6+,/01-*6

E6"#?

C/&<%B/&C/<'5

!+,-(%.! 4;6',"<<.'55"c+',"..";! C/<'X,A5'.,<"6"X,D5/1'F,QE,<"6"! 2&#+A<'5,56"#?,A5'<,6/,5A==/.6,=./#'<A.'5

Page 7: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

13

'%7'

'%7'

8,*#)9

8,*#)9

!-23C?4,(Ogcc -SP

F6642>?4,(Ogcc(-,(asP

KC*<4,(Ogcc(-, ldP

!(3,-H,92(Op1.c p2.cP

F62(3,-H,92(Op1.s p2.sP

W>Q40+(3,-H,92(Op1.o p2.oP

`L40/+9>?4(3,-H,92(OpP

"+910(?C>,9,C46(O.aP

:A.&%&0,C,%&6/,Qch'#6,C/<'! !-.4(C*(c?46((p1.c p2.c! !-23C?4(=C+8(0-229*.'((gcc –O1 p1.c p2.c -o p

! \64(>96C0(-312Cj91-*6(O-O1P! T/+(,46/?1*H(>C*9,5(C*(c?4(p

Denison University

14

C/1=%+%&0,2&6/,U55'1c+;C,C/<'int sum(int x, int y){ int t = x+y; return t;}

i'&'."6'<,2UNK,U55'1c+;sum: pushl %ebp movl %esp,%ebp movl 12(%ebp),%eax addl 8(%ebp),%eax popl %ebp ret

Qc6"%&,R%6$,#/11"&<

/usr/local/bin/gcc –O1 -S code.c

-./<A#'5,W+',code.s

E/1',#/1=%+'.5,A5',%&56.A#B/&,fleaveg

Page 8: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

15

U55'1c+;,C$"."#6'.%5B#53,T"6",:;='5! f2&6'0'.g,<"6",/Z,HX,KX,/.,O,c;6'5

! V9+9(g9?/46! F..,46646(O/*+534.(3-C*+4,6P

! P+/"B&0,=/%&6,<"6",/Z,OX,8X,/.,HG,c;6'5

! \/,"00.'0"6',6;='5,5A#$,"5,"..";5,/.,56.A#6A.'5! d/6+(0-*1H/-/6?5(9??-09+4.(>5+46(C*(242-,5

Denison University

16

U55'1c+;,C$"."#6'.%5B#53,Q='."B/&5! -'.Z/.1,".%6$1'B#,ZA&#B/&,/&,.'0%56'.,/.,1'1/.;,<"6"

! :."&5Z'.,<"6",c'6R''&,1'1/.;,"&<,.'0%56'.! K-9.(.9+9(G,-2(242-,5(C*+-(,4HC6+4,! "+-,4(,4HC6+4,(.9+9(C*+-(242-,5

! :."&5Z'.,#/&6./+! \*0-*.C1-*9?(Q/236(+-]G,-2(3,-04./,46! !-*.C1-*9?(>,9*0846

Page 9: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

17

C/<',Z/.,sum0x401040 <sum>: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x5d 0xc3

Qch'#6,C/<'! U55'1c+'.

! 7,9*6?9+46(.s(C*+-(.o! ;C*9,5(4*0-.C*H(-G(4908(C*6+,/01-*! l49,?5#0-23?4+4(C29H4(-G(4L40/+9>?4(0-.4! IC66C*H(?C*<9H46(>4+=44*(0-.4(C*(.CJ4,4*+(

c?46! )%&?'.

! N46-?g46(,4G4,4*046(>4+=44*(c?46! !-2>C*46(=C+8(6+910(,/*#124(?C>,9,C46

! `:H:B(0-.4(G-,(malloc/*printf! "-24(?C>,9,C46(9,4(!"#$%&'$((")(&#*+!

! KC*<C*H(-00/,6(=84*(3,-H,92(>4HC*6(4L40/1-*

! "#$%&'#('))'*+$,-

! .%/0'12-$34/5#2')6'76'#3'8'*+$,-

! 9$%3$-'%$'%::3,--'0x401040

Denison University

18

!"#$%&',2&56.A#B/&,@7"1=+'! C,C/<'

! F..(+=-(6CH*4.(C*+4H4,6! U55'1c+;

! F..($(^#>5+4(C*+4H4,6! YK-*H[(=-,.6(C*(m!!(39,?9*04! "924(C*6+,/01-*(=84+84,(6CH*4.(-,(/*6CH*4.

! W34,9*.6'x)( N4HC6+4,( %eaxy)( I42-,5( +0%ebp+8]t)( N4HC6+4,( %eax

n N4+/,*(G/*01-*(g9?/4(C*(%eax! Qch'#6,C/<'

! X#>5+4(C*6+,/01-*! "+-,4.(9+(9..,466(0x80483ca

int t = x+y;

addl 8(%ebp),%eax

0x80483ca: 03 45 08

E%1%+".,6/,'7=.'55%/&3,

x += y

!/.',=.'#%5'+;3int eax;int *ebp;eax += ebp[2]

Page 10: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

19

T%5"55'1c+'<

T%5"55'1c+%&0,Qch'#6,C/<'

! T%5"55'1c+'.objdump -d p! \64G/?(+--?(G-,(4L92C*C*H(->Q40+(0-.4! F*9?5j46(>C+(39e4,*(-G(64,C46(-G(C*6+,/01-*6! T,-./046(933,-LC29+4(,4*.C1-*(-G(96642>?5(0-.4! !9*(>4(,/*(-*(4C+84,(a.out(O0-23?4+4(4L40/+9>?4P(-,(.o(c?4

080483c4 <sum>: 80483c4: 55 push %ebp 80483c5: 89 e5 mov %esp,%ebp 80483c7: 8b 45 0c mov 0xc(%ebp),%eax 80483ca: 03 45 08 add 0x8(%ebp),%eax 80483cd: 5d pop %ebp 80483ce: c3 ret

Denison University

20

T%5"55'1c+'<

Dump of assembler code for function sum:0x080483c4 <sum+0>: push %ebp0x080483c5 <sum+1>: mov %esp,%ebp0x080483c7 <sum+3>: mov 0xc(%ebp),%eax0x080483ca <sum+6>: add 0x8(%ebp),%eax0x080483cd <sum+9>: pop %ebp0x080483ce <sum+10>: ret

U+6'.&"6',T%5"55'1c+;

! [%6$%&,0<c,T'cA00'.gdb pdisassemble sum! VC696642>?4(3,-04./,4x/11xb sum

! `L92C*4(+84(&&(>5+46(6+9,1*H(9+(sum

Qch'#60x401040: 0x55 0x89 0xe5 0x8b 0x45 0x0c 0x03 0x45 0x08 0x5d 0xc3

Page 11: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

21

[$"6,C"&,c',T%5"55'1c+'<j

! U&;6$%&0,6$"6,#"&,c',%&6'.=.'6'<,"5,'7'#A6"c+',#/<'! T%5"55'1c+'.,'7"1%&'5,c;6'5,"&<,.'#/&56.A#65,"55'1c+;,5/A.#'

% 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 $0xffffffff30001005: 68 90 10 00 30 push $0x300010903000100a: 68 91 dc 4c 30 push $0x304cdc91

Denison University

22

2&6'0'.,b'0%56'.5,D2UNKF

%eax%ecx%edx%ebx%esi%edi%esp%ebp

%ax

%cx

%dx

%bx

%si

%di

%sp

%bp

%ah

%ch

%dh

%bh

%al

%cl

%dl

%bl

H9(c%6,*%.6A"+,.'0%56'.5Dc"#?R".<5,#/1="Bc%+%6;F

0'&'

."+,=A.=/

5'

accumulate

counter

data

base

source index

destinationindex

stack pointer

basepointer

Q.%0%&D1/56+;,/c5/+'6'F

Page 12: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

23

!/*%&0,T"6"3,2UNK! !/*%&0,T"6"

movl*!"#$%&/*'&())

! Q='."&<,:;='5! *++&,-.)&/(!-*6+9*+(C*+4H4,(.9+9

! `L923?4'($0x400/*$-533! KC<4(!(0-*6+9*+B(>/+(3,4cL4.(=C+8(‘$’! `*0-.4.(=C+8(&B($B(-,(^(>5+46

! 0&1-()&$/2W*4(-G(%(C*+4H4,(,4HC6+4,6! `L923?4'(%eax, %edx! ;/+(%esp 9*.(%ebp ,464,g4.(G-,(6340C9?(/64! W+84,6(89g4(6340C9?(/646(G-,(39,10/?9,(C*6+,/01-*6

! 3&+"$4/(^(0-*640/1g4(>5+46(-G(242-,5(9+(9..,466(HCg4*(>5(,4HC6+4,! "C23?46+(4L923?4'((%eax)! k9,C-/6(-+84,(Y9..,466(2-.46[

%eax%ecx%edx%ebx%esi%edi%esp%ebp

Denison University

24

movl,Q='."&<,C/1c%&"B/&5

:#**-'!;-!$%$-)9<$%$-)9!')#*+=%)!>,'?!#!+,*@A%!,*+')BCD-*

movl

E$$

F%@

.%$

F%@.%$

F%@.%$

F%@

E/A.#' T'56 C,U&"+/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;

E.#XT'56

Page 13: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

25

E%1=+',!'1/.;,U<<.'55%&0,!/<'5! \/.1"+, DbF, !'1kb'0kbll

! b'0%56'.,b,5='#%W'5,1'1/.;,"<<.'55

movl (%ecx),%eax

! T%5=+"#'1'&6, TDbF, !'1kb'0kblmTl! b'0%56'.,b,5='#%W'5,56".6,/Z,1'1/.;,.'0%/&! C/&56"&6,<%5=+"#'1'&6,T,5='#%W'5,/^5'6

movl 8(%ebp),%edx

Denison University

26

d5%&0,E%1=+',U<<.'55%&0,!/<'5

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

4/<;

E'6d=

P%&%5$

swap: pushl %ebp movl %esp,%ebp pushl %ebx

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

popl %ebx popl %ebp ret

Page 14: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

27

d5%&0,E%1=+',U<<.'55%&0,!/<'5

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

4/<;

E'6d=

P%&%5$

swap: pushl %ebp movl %esp,%ebp pushl %ebx

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

popl %ebx popl %ebp ret

Denison University

28

d&<'.56"&<%&0,ER"=

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

E6"#?D%&,1'1/.;F

b'0%56'., e"+A'%edx xp%ecx yp%ebx t0%eax t1

yp

xp

b6&,"<.

Q+<,nebp %ebp 0

4

8

12

Q^5'6

ooo

Q+<,nebx-4 %esp

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

Page 15: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

29

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

123

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp 0x104

Denison University

30

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

123

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x124

0x104

Page 16: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

31

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

123

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x124

0x120

0x104

Denison University

32

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

123

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

0x124

0x120

123

0x104

Page 17: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

33

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

123

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Denison University

34

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

456

456

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Page 18: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

35

d&<'.56"&<%&0,ER"=

0x120

0x124

b6&,"<.%ebp 0

4

8

12

Q^5'6

-4

456

123

U<<.'550x124

0x120

0x11c

0x118

0x114

0x110

0x10c

0x108

0x104

0x100

yp

xp

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

%eax

%edx

%ecx

%ebx

%esi

%edi

%esp

%ebp

456

0x124

0x120

123

0x104

Denison University

36

C/1=+'6',!'1/.;,U<<.'55%&0,!/<'5!!/56,i'&'."+,P/.1, , TDbcXb%XEF, !'1kb'0kbclmEpb'0kb%lm,Tl

! V'(( !-*6+9*+(Y.C63?90424*+[(&B($B(-,(^(>5+46! N>'(( ;964(,4HC6+4,'(F*5(-G(%(C*+4H4,(,4HC6+4,6! NC'( )*.4L(,4HC6+4,'(F*5B(4L043+(G-,(%esp

! \*?C<4?5(5-/o.(/64(%ebpB(4C+84,! "'(( "09?4'(&B($B(^B(-,(%(O,-").-+/+)#0%1+2/3P

! E='#%"+,C"5'5, , DbcXb%F, !'1kb'0kbclmb'0kb%ll, , TDbcXb%F, !'1kb'0kbclmb'0kb%lmTl, , DbcXb%XEF, !'1kb'0kbclmEpb'0kb%ll

Page 19: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

37

T"6",b'=.'5'&6"B/&53,2UNK,m,789(9O! E%q'5,/Z,C,Qch'#65,D%&,4;6'5F!(((222252'.).2647&2 8&9&$-%2:;<=-)2 *9)&>2*?:;2 @AB<BC

! /*6CH*4.( ^( ^( ^! C*+( ^( ^( ^! ?-*H(C*+( ^( ^( %! 089,( &( &( &! 68-,+( $( $( $! Z-9+( ^( ^( ^! .-/>?4( %( %( %! ?-*H(.-/>?4( %( &@]&$( &A! 089,(p( ^( ^( %

4 52)$#")6.-+2)76&#.+2

Denison University

38

%rsp

789(9O,2&6'0'.,b'0%56'.5

! `L+4*.(4LC61*H(,4HC6+4,6:((F..(%(*4=(-*46:! I9<4(%ebp1%rbp*H4*4,9?(3/,3-64

%eax

%ebx

%ecx

%edx

%esi

%edi

%esp

%ebp

%r8d

%r9d

%r10d

%r11d

%r12d

%r13d

%r14d

%r15d

%r8

%r9

%r10

%r11

%r12

%r13

%r14

%r15

%rax

%rbx

%rcx

%rdx

%rsi

%rdi

%rbp

Page 20: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

39

2&56.A#B/&5! )/&0,R/.<,l,DO,4;6'5F,r,sA"<,R/.<,q,D8,4;6'5F

! \'R,%&56.A#B/&53! movl(!(movq! addl(!(addq! sall(!(salq! 4+0:

! NK(c%6,%&56.A#B/&5,6$"6,0'&'."6',NK(c%6,.'5A+65! "4+(8CH84,(-,.4,(>C+6(-G(.461*91-*(,4HC6+4,(+-(0! `L923?4'(addl

Denison University

40

NK(c%6,#/<',Z/.,5R"=

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

4/<;

E'6d=

P%&%5$

swap: pushl %ebp movl %esp,%ebp pushl %ebx

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

popl %ebx popl %ebp ret

Page 21: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

41

9O(c%6,#/<',Z/.,5R"=

! Q='."&<5,="55'<,%&,.'0%56'.5,DR$;,A5'ZA+jF! SC,6+(OxpP(C*(%rdiB(640-*.(OypP(C*(%rsi! A^#>C+(3-C*+4,6

! \/,56"#?,/='."B/&5,.'tA%.'<! NK(c%6,<"6"! V9+9(84?.(C*(,4HC6+4,6(%eax(9*.(%edx! (movl(-34,91-*

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

4/<;

E'6d=

P%&%5$

swap:

movl (%rdi), %edx movl (%rsi), %eax movl %eax, (%rdi) movl %edx, (%rsi) ret

Denison University

42

9O(c%6,#/<',Z/.,+/&0,%&6,5R"=

! 9O(c%6,<"6"! V9+9(84?.(C*(,4HC6+4,6(%rax(9*.(%rdx! (movq(-34,91-*

! Yf[(6+9*.6(G-,(f/9.#=-,.

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

4/<;

E'6d=

P%&%5$

swap_l:

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

ret

Page 22: 0.11%&0,23,45%#5personal.denison.edu/~bressoud/cs281-f12/Supplements/05-machin… · Denison University 17 C

Denison University

43

!"#$%&',-./0."11%&0,23,EA11".;! _%56/.;,/Z,2&6'+,=./#'55/.5,"&<,".#$%6'#6A.'5

! `g-?/1-*9,5(.46CH*(?49.6(+-(29*5(f/C,<6(9*.(9,1G90+6! CX,"55'1c+;X,1"#$%&',#/<'

! !-23C?4,(2/6+(+,9*6G-,2(6+9+424*+6B(4L3,466C-*6B(3,-04./,46(C*+-(?-=#?4g4?(C*6+,/01-*(64f/4*046

! U55'1c+;,4"5%#53,b'0%56'.5X,/='."&<5X,1/*'! 784(L%A(2-g4(C*6+,/01-*6(0-g4,(=C.4(,9*H4(-G(.9+9(2-g424*+(

G-,26! 2&6./,6/,789(9O

! F(29Q-,(.439,+/,4(G,-2(+84(6+5?4(-G(0-.4(644*(C*()FX$