与汇编6语言循环结构的不同实现_第1页
与汇编6语言循环结构的不同实现_第2页
与汇编6语言循环结构的不同实现_第3页
与汇编6语言循环结构的不同实现_第4页
与汇编6语言循环结构的不同实现_第5页
已阅读5页,还剩40页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

80X86汇编语言与C语言-ControlFlow(控制流条件设程序控制If-then-循环结switch语

x86-64条件传送指循环结构的不同实汇编程序员眼中的系统结构(部分当前执行程的信数执行指令

运行栈条运行栈条件SF当前运行栈的栈当前运行栈的“帧指令地址(下一条条件CFCarry(进位/借位SignZFZero这些条件码由算术指令隐addl类似的C语言表达式:taCF进位/借位标

addq(a=Src,b=可用于检测无符号整数运算的ZFsetift==SFsetift<OFsetif补码运算溢出(即带符号整数运算(a>0&&b>0&&||(a<0&&b<0&&lea,inc,dec这些指令不会设置条件比较(Compare)指cmpl cmpqcmplb,a类似与计算a-b(但是不改变目的操作数CFsetifcarryoutfrommostsignificant可用于无符号数的ZFsetifa==SFsetif(a-b)<OFsetiftwo’scomplementoverflow(a>0&&b<0&&(a-b)<0)||(a<0&&b>0&&(a-testlSrc2,Src1testq计算Src1&Src2并设置相应的条件码,但是ZFsetwhena&b==SFsetwhena&b<SetX指当前的条件码(或者某些条件码的组合),并存入目寄存Equal/NotEqual/NotGreaterGreaterorEqualLessLessorEqualAboveBelow当前的条件码(或者某些条件码的组合并存入目的“字节”寄存余下的三个字节不会被通常使用“movzbl”指令对目的寄存行“0intintgt(intx,int{returnx>}movl12(%ebp),%eax#eax=cmpl #Comparex:y setg #al=x>

movzbl

#Zerorestof

x86-64 条件SetX指当前的条件码(或者某些条件码的组合并存入目的“字节”寄存余下的七个字节不会被intintgt(longx,long{returnx>}longlgt(longx,long{returnx>}xinyinBody(samefor

“64-bitoperandsgeneratea64-bitresultinthegeneral-purpose32-bitoperandsgeneratea32-bitresult,zero-extendedtoa64-bitresultinthedestinationgeneral-purposeregister.“摘自 ®64andIA-32SoftwareDeveloper’sVolume1:Basicxorl%eax,%eaxcmpq%rsi,%rdisetg%al

#eax=#Comparex:#al=x>∆微体系结构背景 32-bitoperandsgeneratea32-bitresult,zero-extendedtoa64-bitresultinthedestinationgeneral-purposeregister.”为什么部分原因来 体系结构(即处理器)内部实现的效率方面的考,目的是为了消除“部分数据依处理器流水线的概处理器流水线InstructionFetchMemoryAccessWriteBack”(Datadependency)。Pentium4:>=20stages,upto126instructionson-例子(PartialRegisterStallisaproblemthatoccurswhenyouwritetopartofa32bitregisterandlaterreadfromthewholeregisterorabiggerpartofit.)addw%bx,movl%eax,jX

跳转依赖当前的条件码选择下一条执行语句(是否顺序执行1Equal/NotEqual/NotGreaterGreaterorEqualLessLessorEqualAboveBelow条件跳转指令实intintintx,int{intif(x>y)result=x-}elseresult=y-}return}

%esp,%ebp8(%ebp),%edx12(%ebp),%eax

%edx,

intgoto_ad(intx,int{intif(x<=y)gotoresult=x-returnresult=y-goto}#xindx,yin.L8:%eax,%eax,%edx,%eax#Compare<=GotoElsex-=yresult=x

.L7:# %edx,%eax

#result=y-x#GotoExitC语言:条件表valval=Test?Then-Expr:Else-val=x>y?x-y:y-nt=nt=if(nt)gotoval=Then-..val=Else-Expr;gotoDone;

条件表达式的执行顺序则求解表达式Then-Expr,此时表Else-Expr,其值就是整个条件表intintintintx,int{intif(x>y)result=x-}elseresult=y-}return}#xiny%edi,%eaxv=%esi,%edx=#v-y#-x##较较新的32位Gcc也可以编译出类似代条件传送指cmovCsrc,如果条件C成立,将数据从src传送至从执行角度来看,比一般的条件跳»因为其控制流可预测(即条件C是已知的intintintintx,int{intif(x>y)result=x-}elseresult=y-}return}movl%esp,pushlmovl8(%ebp),%ecxmovl12(%ebp),%edxmovl%ecx,%ebxsubl%edx,%ebxmovl%edx,%eaxsubl%ecx,%eaxcmpl%edx,%ecxcmovg%ebx,%eaxpopl%ebxpopl%ebp∆微体系结构背处理器流水线(五级流水示例InstructionFetchReadRegistersArithmeticOperationMemoryAccessWriteBack∆微体系结构背景Pentium4:>=20stages,upto126instructionson-val=Then-val=Then-vale=Else-val=valeifint=0,xlteyint=0,xltey=intabsdiff_se(intx,inty){intif(x>y)xgty++;result=x-}elsexltey++;result=y-}return}Then-Expr或Else-表达式有“副作用Then-ExprElse-表达式的计算量如何实现循环所有的循环模式(while,do-while,for)都转换为“do-while”式但历史上gcc采用过多种转换模式,经历了“否定之否定”的过do-do-“Do-While”循环实原始的C

Gotointfact_do(intintfact_do(int{intresult=doresult*=x=x-}while(x>return}intfact_goto(int{intresult=result*=x=x-1;if(x>1)gotoloop;returnresult;}编译器先转换为goto模Goto

汇fact_goto(int{intfact_goto(int{intresult=result*=x=x-if(x>gotoreturn}movl

##eax=#edx=imulldecl%edxcmpl$1,%edxjgL11movlpopl

#result*=#x-#Comparex:1#if>gotoloop#Finish#Finish#While”循环-版本原始的C代

intintfact_while_goto(int{{intresult=intresult=while(x>1)if(!(x>gotoresult*=x=x-result*=x;x=x-1;gotoloop;return}return}这个实例与上一个等价吗While”循环-版本2(do-while模式intfact_while(int{intintfact_while(int{intresult=1;while(x>1){result*=x;x=x-return}

intfact_while_goto2(int{intresult=intfact_while_goto2(int{intresult=if(!(x>gotodone;result*=x;x=x-1;if(x>gotoreturn}内部循环与do-while相在循 做额外的条测“For”“While”“Do-While”For Whileforfor(Init;Test;Updatewhile(while(Test){Update;}if(!Test)gotoif(!Test)gotodone;do{Update;}whileifgotodone;Update;if(Test)gotoloop;补充历史上gcc采用过多种转换模式,经历了“否定之否定”的过Jump-to-Jump-to-do-While”循环-版本3(jump-to-intfact_while(int{intfact_while(int{intresult=1;while(x>1){result*=x;x=x-return}

Gotointfact_while_goto3(int{intresultintfact_while_goto3(int{intresult=gotomiddle;result*=x;x=x-1;if(x>gotoreturn}轮循Jump-to-Middleintfact_while(int{intresult=while(x>1){result*=x;return}gcc-#xin%edx,resultin goto #imull%edx,%eax result*=decl%edx #Middle:cmpl$1, if>,goto“For”“While”(Jump-to-while(Testwhile(Test){Update;}forfor(Init;Test;UpdateGotogotomiddle;Update;if(Test)gotoloop;Jump-to-MiddlewhilewhileGoto

特点避免了双重测无条件跳转指令的处理销非常低(可以忽略##xin%edx,resultin#goto#imull%edx,%eax#result*=x#$1,##if>,goto∆微体系结构背景条件跳转指令往往会引起一定的性能损失,BranchPredictiondo-Jump-to-BranchPrediction的表项数有限,且其依据跳转与否的do-Jump-to-BranchPrediction继续发展,采用了循环预测器技术US —即对于“循 Jump-to-Jump-to-do-Switch依据不同情况来采用不同的实现技使用一组if-then-else语句来实使用跳转longlong(longx,longy,long{longw=1;switch(x){case1:w=case2:w=/*FallThroughcasew+=z;casecasew-=w=}return}Switch语多个case对应同一段句“FallCase值并不连跳转Switch Jump Jumpswitch(x)switch(x)caseBlockcaseBlock••caseval_n-Block}

CodeBlockCodeBlockCodeBlock•••CodeCodeBlock•••CodeCodeSwitch语句示例(x86-longlong(longx,longy,long{longw=switch(x)..}return}

%esp,

#Setup#Setup#movl$1,%ebx #w=1movl8(%ebp),%edx#edx=xmovl16(%ebp),%ecx#ecx=zcmpl$6,%edx #x:6

#if>gotodefault#gotoJTab[x]表结每个表项(及跳转地址)占4址是无条件跳转指jmpJumptargetisdenotedbylabeljmpStartofjumptabledenotedbylabelRegister%edxholdsMustscalebyfactorof4togetoffsetintoFetchtargetfromeffectiveAddress.L61+Onlyfor0x

表项内.section

switch(x)casew=casew=

////4#x=4#x=0#x=1#x=2#x=3#x=4#x=5#x=6casew+=casecasew-=w=

//////}.L61://Defaultswitch(x)..case //w=

$2,%ebx,

#w=#Return/*FallThrough

//Casecasew+=

//

c

12(%ebp),%eax##Div

idivl

#..

//

movl%eax,%ebx#w=y/z#Fallthroughw=}

//Case%ecx,%ebx#w+=%ebx,%eax#Return.L6

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论