计算机组成与设计第五版(Chapter2)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Patterson-1610874978-0-12-407726-3PIISolutions2Chapter2SolutionsS-32.1addif,h,-5(note,nosubi)addf,f,g2.2f=g+h+i2.3sub$t0,$s3,$s4add$t0,$s6,$t0lw$t1,16($t0)sw$t1,32($s7)2.4B[g]=A[f]+A[1+f];2.5add$t0,$s6,$s0add$t1,$s7,$s1lw$s0,0($t0)lw$t0,4($t0)add$t0,$t0,$s0sw$t0,0($t1)2.62.6.1temp=Array[0];temp2=Array[1];Array[0]=Array[4];Array[1]=temp;Array[4]=Array[3];Array[3]=temp2;2.6.2lw$t0,0($s6)lw$t1,4($s6)lw$t2,16($s6)sw$t2,0($s6)sw$t0,4($s6)lw$t0,12($s6)sw$t0,16($s6)sw$t1,12($s6)S-4Chapter2Solutions2.7Little-EndianBig-EndianAddressDataAddressData12ab12128cd8ef4ef4cd0120ab2.828824000182.9sll$t0,$s1,2#$t0--4*gadd$t0,$t0,$s7#$t0--Addr(B[g])lw$t0,0($t0)#$t0--B[g]addi$t0,$t0,1#$t0--B[g]+1sll$t0,$t0,2#$t0--4*(B[g]+1)=Addr(A[B[g]+1])lw$s0,0($t0)#f--A[B[g]+1]2.10f=2*(&A);2.11typeopcodersrtrdimmedaddi$t0,$s6,4I-type82284add$t1,$s6,$0R-type02209sw$t1,0($t0)I-type43890lw$t0,0($t0)I-type35880add$s0,$t1,$t0R-type098162.122.12.1500000002.12.2overflow2.12.3B00000002.12.4nooverflow2.12.5D00000002.12.6overflow2.132.13.11282311,x231129and128x231,x231128(impossible)2.13.2128x2311,x231129and128x231,x231128(impossible)2.13.3x128231,x231128andx1282311,x231127(impossible)Chapter2SolutionsS-52.14r-type,add$s0,$s0,$s02.15i-type,0xAD4900202.16r-type,sub$v1,$v1,$v0,0x006218222.17i-type,lw$v0,4($at),0x8C2200042.182.18.1opcodewouldbe8bits,rs,rt,rdfieldswouldbe7bitseach2.18.2opcodewouldbe8bits,rsandrtfieldswouldbe7bitseach2.18.3moreregisters→morebitsperinstruction→couldincreasecodesizemoreregisters→lessregisterspills→lessinstructionsmoreinstructions→moreappropriateinstruction→decreasecodesizemoreinstructions→largeropcodes→largercodesize2.192.19.10xBABEFEF82.19.20xAAAAAAA02.19.30x000055452.20srl$t0,$t0,11sll$t0,$t0,26ori$t2,$0,0x03ffsll$t2,$t2,16ori$t2,$t2,0xffffand$t1,$t1,$t2or$t1,$t1,$t02.21nor$t1,$t2,$t22.22lw$t3,0($s1)sll$t1,$t3,42.23$t2=32.24jump:no,beq:noS-6Chapter2Solutions2.252.25.1i-type2.25.2addi$t2,$t2,–1beq$t2,$0,loop2.262.26.1202.26.2i=10;do{B+=2;i=i–1;}while(i0)2.26.35*N2.27addi$t0,$0,0beq$0,$0,TEST1LOOP1:addi$t1,$0,0beq$0,$0,TEST2LOOP2:add$t3,$t0,$t1sll$t2,$t1,4add$t2,$t2,$s2sw$t3,($t2)addi$t1,$t1,1TEST2:slt$t2,$t1,$s1bne$t2,$0,LOOP2addi$t0,$t0,1TEST1:slt$t2,$t0,$s0bne$t2,$0,LOOP12.2814instructionstoimplementand158instructionsexecuted2.29for(i=0;i100;i++){result+=MemArray[s0];s0=s0+4;}Chapter2SolutionsS-72.30addi$t1,$s0,400LOOP:lw$s1,0($t1)add$s2,$s2,$s1addi$t1,$t1,-4bne$t1,$s0,LOOP2.31fib:addi$sp,$sp,-12#makeroomonstacksw$ra,8($sp)#push$rasw$s0,4($sp)#push$s0sw$a0,0($sp)#push$a0(N)bgt$a0,$0,test2#ifn0,testifn=1add$v0,$0,$0#elsefib(0)=0jrtn#test2:addi$t0,$0,1#bne$t0,$a0,gen#ifn1,genadd$v0,$0,$t0#elsefib(1)=1jrtngen:subi$a0,$a0,1#n-1jalfib#callfib(n-1)add$s0,$v0,$0#copyfib(n-1)sub$a0,$a0,1#n-2jalfib#callfib(n-2)add$v0,$v0,$s0#fib(n-1)+fib(n-2)rtn:lw$a0,0($sp)#pop$a0lw$s0,4($sp)#pop$s0lw$ra,8($sp)#pop$raaddi$sp,$sp,12#restorespjr$ra#fib(0)=12instructions,fib(1)=14instructions,#fib(N)=26+18NinstructionsforN=22.32Duetotherecursivenatureofthecode,itisnotpossibleforthecompilertoin-linethefunctioncall.2.33aftercallingfunctionfib:old$sp-0x7ffffffc???-4contentsofregister$raforfib(N)-8contentsofregister$s0forfib(N)$sp--12contentsofregister$a0forfib(N)therewillbeN-1copiesof$ra,$s0and$a0S-8Chapter2Solutions2.34f:addi$sp,$sp,-12sw$ra,8($sp)sw$s1,4($sp)sw$s0,0($sp)move$s1,$a2move$s0,$a3jalfuncmove$a0,$v0add$a1,$s0,$s1jalfunclw$ra,8($sp)lw$s1,4($sp)lw$s0,0($sp)addi$sp,$sp,12jr$ra2.35Wecanusethetail-calloptimizationforthesecondcalltofunc,butthenwemustrestore$ra,$s0,$s1,and$spbeforethatcall.Wesaveonlyoneinstruction(jr$ra).2.36Register$raisequaltothereturnaddressinthecallerfunction,registers$spand$s3havethesamevaluestheyhadwhenfunctionfwascalled,andregister$t5canhaveanarbitraryvalue.Forregister$t5,notethatalthoughourfunctionfdoesnotmodifyit,functionfuncisallowedtomodifyitsowecannotassumeanythingabouttheof$t5afterfunctionfunchasbeencalled.2.37MAIN:addi$sp,$sp,-4sw$ra,($sp)add$t6,$0,0x30#‘0’add$t7,$0,0x39#‘9’add$s0,$0,$0add$t0,$a0,$0LOOP:lb$t1,($t0)slt$t2,$t1,$t6bne$t2,$0,DONEslt$t2,$t7,$t1bne$t2,$0,DONEsub$t1,$t1,$t6beq$s0,$0,FIRSTmul$s0,$s0,10FIRST:add$s0,$s0,$t1addi$t0,$t0,1jLOOPChapter2SolutionsS-9DONE:add$v0,$s0,$0lw$ra,($sp)addi$sp,$sp,4jr$ra2.380x000000112.39Generally,allsolutionsaresimilar:lui$t1,top_16_bitsori$t1,$t1,bottom_16_bits2.40No,jumpcangoupto0x0FFFFFFC.2.41No,rangeis0x604+0x1FFFC=0x00020600to0x604–0x20000=0xFFFE0604.2.42Yes,rangeis0x1FFFF004+0x1FFFC=0x2001F000to0x1FFFF004-0x20000=1FFDF0042.43trylk:li$t1,1ll$t0,0($a0)bnez$t0,trylksc$t1,0($a0)beqz$t1,trylklw$t2,0($a1)slt$t3,$t2,$a2bnez$t3,skipsw$a2,0($a1)skip:sw$0,0($a0)2.44try:ll$t0,0($a1)slt$t1,$t0,$a2bnez$t1,skipmov$t0,$a2sc$t0,0($a1)beqz$t0,tryskip:2.45ItispossibleforoneorbothprocessorstocompletethiscodewithouteverreachingtheSCinstruction.IfonlyoneexecutesSC,itcompletessuccessfully.IfbothreachSC,theydosointhesamecycle,butoneSCcompletesfirstandthentheotherdetectsthisandfails.S-10Chapter2Solutions2.462.46.1Answerisnoinallcases.Slowsdown

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功