资源描述:
《ch09-分支与循环程序设计》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第5章分支与循环程序设计5.1.概述设计步骤:描述问题à确定算法à绘制流程图à分配存储空间和工作单元à编写程序à上机调试5.2.分支程序设计5.2.1分支程序结构条件?。。。条件?二路分支结构多路分支结构5.2.2二路分支程序设计方法例5.2.1-128≤x≤127DATASEGMENTXX1DB10;X=给定一个值YY1DB?;YDATAENDSCODESEGMENTASSUMECS:CODE,DS:DATASTART:MOVAX,DSMOVDS,AXMOVAL,XX1CMPAL,0JZAA1;=0JNSAA2MOVAL,0FFH;=-1JM
2、PAA1AA2:MOVAL,1;=1AA1:MOVYY1,AL;MOVAH,4CHINT21HCODEENDSENDSTART5.1.1多路分支程序设计方法逻辑分解法;地址表法;段内转移表法;1.逻辑分解法CODESEGMENTASSUMECS:CODESTART:MOVAH,1INT21H;键入值CMPAL,31HJZWORK1CMPAL,32HJZWORK2CMPAL,33HJZWORK3CMPAL,34HJZWORK4CMPAL,35HJZWORK5JMPWORK0WORK1:…(jmpwork0)WORK2:…(jmpwork0)WOR
3、K3:…(jmpwork0)WORK4:…(jmpwork0)WORK5:…(jmpwork0)WORK0:MOVAH,4CHINT21HCODEENDSENDSTART1.地址表法把模块的地址保存在一个表中,通过查表跳到相应模块。表地址=表首地址+(键号-1)*2DATASEGMENTTABLEDWWORK1,WORK2,WORK3,WORK4,WORK5DATAENDSCODESEGMENTASSUMECS:CODE,DS:DATASTART:MOVAX,DATAMOVDS,AXLEABX,TABLE;表首地址MOVAH,1INT21HAN
4、DAL,0FHDECAL;键号-1ADDAL,AL;(键号-1)*2SUBAH,AHADDBX,AX;表地址=表首地址+(键号-1)*2JMPWORDPTR[BX]WORK1:…(jmpwork0)WORK2:…(jmpwork0)WORK3:…(jmpwork0)WORK4:…(jmpwork0)WORK5:…(jmpwork0)WORK0:MOVAH,4CHINT21HCODEENDSENDSTART2.段内转移表法段内短转移:(jmpdisp8为2字节指令)转移表地址=转移表首地址+(键号-1)*2段内近转移:(jmpdisp16为3字节
5、指令)转移表地址=转移表首地址+(键号-1)*3段间远转移:(jmpdisp32为4字节指令)转移表地址=转移表首地址+(键号-1)*4CODESEGMENTASSUMECS:CODESTART:LEABX,WORKMOVAH,1INT21HAND0FHDECALMOVAH,ALADDAL,ALADDAL,AH;x3SUBAH,AHADDBX,AXJMPBX;转移表WORK:JMPNEARPTRWORK1JMPNEARPTRWORK2JMPNEARPTRWORK3JMPNEARPTRWORK4JMPNEARPTRWORK5;工作模块WORK1:
6、…(jmpwork0)WORK2:…(jmpwork0)WORK3:…(jmpwork0)WORK4:…(jmpwork0)WORK5:…(jmpwork0)WORK0:MOVAH,4CHINT21HCODEENDSENDSTART5.1.循环程序设计5.1.1循环程序的结构形式5.1.1循环程序的设计方法方法:计数控制循环;条件控制循环;变量控制循环1.计数控制循环(循环次数已知)例:5.3.1,把1,2,3…255加起来。(1)先执行后判断(计数控制)DATASEGMENTORG1000HNUMBER1DB1,2,3,…255SUM1DW?
7、DATAENDSCODESEGMENTASSUMECS:CODE,DS:DATASTART:MOVAX,DATAMOVDS,AXLEABX,NUMBER1MOVAX,0MOVDH,0MOVCL,255AA1:MOVDL,[BX]ADDAX,DXINCBXSUBCL,1;DECCLJNZAA1MOVSUM1,AXMOVAH,4CHINT21HCODEENDSENDSTART(2)先判断后执行(计数控制)DATASEGMENTORG1000HNUMBER1DB1,2,3,…255SUM1DW?DATAENDSCODESEGMENTASSUMECS:
8、CODE,DS:DATASTART:MOVAX,DATAMOVDS,AXLEABX,NUMBER1-1MOVAX,0MOVDH,0MOVCL,0;初值