微型计算机技术与汇编语言程序设计:ch09-分支与循环程序设计.docx
第5章分支与循环程序设计5.1.概述设计步骤:描述问题分确定算法令绘制流程图今分配存储空间和工作单元少编写程序 分上机调试5. 2.分支程序设计分支程序结构二路分支结构多路分支结构522二路分支程序设计方法例 1(x > 0)y =<0(% = 0)-1(x<0)-128<xl27DATASEGMENTXXI DB 10;X二给定一个值YY1 DB ?;YDATAENDSCODE SEGMENTASSUME CS:CODE, DS:DATAMOV DS, AXLEA, BX, NUMBER1MOV CX, 65AA1: MOVMOV INT INC LOOPDL, BXAH, 2 21H BX AA1MOV AH,4CHINT21HCODE ENDSEND START例在CRT上显示16位十进制数8988998899989899DATADATASEGMENT;(逻辑尺法)ENDSCODESEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATAMOV DS, AXAA1:AA2:MOVMOV MOVSHL JNC INC MOVINT LOOPBX, RULERICX, 16DL, 38HBX, 1AA2DL ; MOV DL, 39HAH, 221HAA1MOV AH,4CHINT21HCODEENDSEND START534多重循环例535用逻辑尺法,在CRT上显示“中”空格一0表示41表示000100001 OhlllllllOOFeh1001001092h1001001092hlllllllOOFeh000100001 Oh000100001 OhDATA SEGMENTRULERI DB lOh, OFeh, 92h, 92h, OFeh, 10h, lOhDATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATA MOV DS, AXLEAMOVAA1: MOVMOVMOVINTMOVMOVINTSI, RULERICX, 7;外循环计数次数DH,8 ;内循环计数次数DL, 0AHAH, 221HDL, 0DHAH, 221HMOVAA2: MOVSHLJNCBL, SIDL, 20HBL, 1AA3ADDAA3: MOVINTDL, 21HAH, 2 21H;MOV DL,41H; ADEC DHJNZ AA2 ;内循环控制MOV DL, 0AHMOV AH, 2INT 21HMOV DL, ODHMOV AH, 2INT 21HINC SILOOP AA1 ;外循环控制MOV AH,4cHINT21HCODE ENDSEND START例五个学生参加4门课的考试,成绩以压缩BCB码方式存放在COURSE1 数组中,计算每个学生的总分。DATA SEGMENTCOURSE 1 DB 70H, 88H, 92H, 90H, 99H;第一门课成绩NUM1DATA ENDSDB 67H, 77H, 88H, 76H, 69HDB 74H, 87H, 77H, 74H, 70H;第二门课成绩;第三门课成绩DB 99H, 97H, 94H, 98H, 96H ;第四门课成绩DW 5 DUP(O) ;5个学生的总成绩结果单元CODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATA MOV DS, AXLEA SI, COURSEILEA DI, NUM1MOV CL, 5;外循环计数次数AA1: MOV BX, SIMOV AX, 0MOV CH, 4;放累加结果;内循环计数次数A42: ADD ALf BXDAAADC AH, 0ADD BX, 5DEC CHJNZ AA2;内循环控制MOV DI, AX ;存一个学生的总分成绩INC SIADD DI, 2DEC CLJNZ AA1 ;外循环控制MOV AH,4cHINT21HCODE ENDSEND START例键入39间的数字,输出一个用“* "组成的三角形。例如键入5,输出如下:* * * * * *大 *火*CODE SEGMENTASSUME CS:CODESTART: MOV AH JINT 21HCMP AL, 33HJC START; JB startCMP AL,3AHJNC START ;界限为 39 JAE startAND AL, OFHMOV BL, AL;保存键入的值,作为外循环的次数MOV BH, 1;内循环次数,它是可变的,但不大于BLAA1: MOVAA1: MOVDL, 0AHMOV AH, 2INT 21HMOV DL, ODHMOV AH, 2INT 21HMOV CL, BHAA2:MOV DL, MOV AH, 2INT 21HDEC CLJNZAA2 ;内循环控制,显示一行的"k:个数由BH定INC BHCMP BL, BHJNC AA1 ;外循环控制 JBE AA1MOV AH,4CHINT21HCODE ENDSEND START例 冒泡法排序算法:图大1 2 34卬小 N1)(N1)*(N1)次比较法第一次内循环比较N-1次,第二次循环比较N-1次,最后一次(N-1次)循 环比较N-1次。DATA SEGMENTNUMBER 1 DB 100,3,90,80,99,77,44,66,50N_1 EQU $-NUMBERl -1DATA ENDSCODE SEGMENTASSUME CS:CODE, DS:DATASTART: MOVMOVAX, DATADS, AXMOV DX, N_1 ;外循环计数次数AA1:AA1:LEAMOVBX, NUMBER 1CX, N_1;内循环计数次数AA2:MOV AL, BXCMP AL, BX+1JNC AA3 ;JGE AA3XCHG AL, BX+1MOV BX,AL ;交换A A3: INC BXLOOP AA2DEC DXJNZ AA1MOV AH, 4CHINT21HCODE ENDSEND STARTS2) (N-D!次比较法第一次内循环比较N-l次,第二次循环比较N-2次,最后一次(N-1次)循 环比较1次。DATA SEGMENTNUMBER 1 DB 100,3,90,80,99,77,44,66,50N_1 EQU $-NUMBERl -1DATA ENDSCODE SEGMENTASSUME CS:CODE, DS:DATASTART: MOVMOVAX, DATADS, AXMOV DX, N_1 ;外循环计数次数AA1:LEA BX, NUMBER 1MOV CX, DX ; *内循环计数次数NJ, N-2,1AA2:MOV AL JBXCMP AL, BX+1JNC AA3 ;JGEAA3A A3:XCHG AL, BX+1MOV BX,AL ;交换INC BXLOOP AA2DEC DXJNZ AA1MOV AH, 4CHINT21HCODE ENDSEND STARTS3)(N1)(N2)(Nm)次比较法第一次内循环比较N-l次,第二次循环比较N-2次,最后一次(m次)循环 比较N-m次(此次比较时无交换,排序结束)。内循环有交换标志AH=1,无交换标志AH=0,若内循环一次无交换,则排序完成。DATA SEGMENTNUMBER 1 DB 100,3,90,80,99,77,44,66,50N_1 EQU $-NUMBERl -1DATA ENDSCODE SEGMENTASSUME CS:CODE, DS:DATASTART: MOVMOVAX, DATADS, AXMOV DX, N_1 ;外循环计数次数AA1:LEA BX, NUMBER 1MOV AH, 0;标志清零MOV CX, DX; *内循环计数次数N-l, N-2AA2:AA2:AA3:MOV CMP JNC XCHG MOV MOV INC LOOPAL, BX AL, BX+1 AA3 ALJBX+1 BX,AL AH, 1 BX AA2;JGE AA3;交换;表示有交换ORJZAH, AHAA4;AH=0,表示无新交换,排序结束,;SHR AH, 1; JNC AA4DEC DXJNZ AA1AA4: MOV AH, 4CHINT21HCODE ENDSEND STARTS考试题:P200 5.4, 5.2, 5.81 .试编写从键盘上接收一个4位的16进制数,并在CRT上显示出与它等值的二 进制数的源程序。START:AA2:MOVAX, DSMOVDS, AXMOVAL,XXICMPAL, 0JZAA1;=0JNSAA2MOVALQFFH;=-lJMPAA1MOVAL,1;=1AA1:CODEMOV YY1,AL;MOV AH,4CHINT 21HENDSEND START多路分支程序设计方法逻辑分解法;地址表法;段内转移表法;1.逻辑分解法CODE SEGMENTASSUME CS:CODESTART: MOV AH,1INT 21H;键入值CMP AL,31HJZ WORK1CMP AL,32HJZWORK2CMP AL,33HJZWORK3CMP AL,34HJZWORK4CMP AL,35HJZWORK5JMP WORKOWORK1:.(jmpworkO)WORK2: .(jmp workO)WORK3: .(jmp workO)WORK4: .(jmp workO)WORK5: .(jmp workO)WORKO: MOV AH,4cHINT 21HCODE ENDS END START.地址表法把模块的地址保存在一个表中,通过查表跳到相应模块。表地址=表首地址+(键号")*2DATA SEGMENTTABLE DWWORK 1, WORK2, WORK3, WORK4, WORK5DATA ENDSCODE SEGMENTASSUME CS:CODE, DS:DATASTART: MOVAX,DATAMOV DS,AXLEA BX,TABLE ;表首地址MOV AH,1AND AL,0FHDEC ALADD AL,ALSUB AH,AH ADD BX,AXAND AL,0FHDEC ALADD AL,ALSUB AH,AH ADD BX,AXINT 21H;键号-1;(键号-1)*2;表地址=表首地址+(键号")*2JMP WORD PTRBXWORK1:.(jmp workO)WORK2:.(jmp workO)WORK3:.(jmp workO)WORK4:.(jmp workO)WORK5:.(jmp workO)WORKO:MOV AH,4CHINT 21HCODE ENDSEND START.段内转移表法段内短转移:(jmpdisp8为2字节指令)转移表地址二转移表首地址+(键号-1)* 2段内近转移:(jmpdispl6为3字节指令)转移表地址=转移表首地址+(键号-1) * 3 段间远转移:(jmp disp32为4字节指令)转移表地址=转移表首地址+(键号-1)*4CODE SEGMENTASSUME CS:CODEBX,WORKAH,121HOFHALAH,AL AL,AL AL,AH AH,AH BX,AX BXBX,WORKAH,121HOFHALAH,AL AL,AL AL,AH AH,AH BX,AX BX;x3START: LEA MOV INT AND DEC MOV ADD ADD SUB ADD JMPNEAR PTR W0RK1NEAR PTR W0RK2NEAR PTR W0RK3NEAR PTR WORK4NEAR PTR WORK5;转移表WORK: JMP JMP JMP JMP JMP ;工作模块 WORK1:WORK2: WORK3: WORK4: WORK5:.(jmp workO) .(jmp workO) .(jmp workO) .(jmp workO) .(jmp workO)AH,4CH 21HSTARTWORKO:MOVINTCODE ENDSEND5. 3.循环程序设计 循环程序的结构形式532循环程序的设计方法方法:计数控制循环;条件控制循环;变量控制循环1 .计数控制循环(循环次数已知)例:5.3.1,把1, 2, 3255力口起来。先执行后判断(计数控制)DATA SEGMENTORG 1000HNUMBER 1 DB 1,2,3, .255SUMI DW?DATA ENDSCODE SEGMENTASSUME CS :CODE,DS :D ATASTART: MOV AX,DATAMOV DS, AXLEA BX, NUMBER 1MOV AX, 0MOV DH, 0MOV CL, 255AA1: MOV DL, BXADD AX, DXINC BXSUB CL J ; DEC CLJNZ AA1MOV SUMI, AXMOV AH,4cHINT21HCODE ENDSEND START先判断后执行(计数控制)DATA SEGMENTORG 1000HNUMBER 1 DB 1,2,3, .255SUMI DW?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATAMOV DS, AXLEA BX, NUMBER 1-1MOV AX, 0MOV DH, 0MOV CL, 0 ;初值 256AA1: INC BXSUB CL JZAA2 MOV DL, BX ADD AX, DX JMPAA1AA2: MOV SUMI, AX MOV AH,4cH INT21HCODE ENDSEND START2 .条件控制循环(结束条件已知) 先执行后判断(条件控制) DATA SEGMENTORG 1000HNUMBER! DB 1,2,3,.255SUMI DW?DATA ENDSCODE SEGMENTASSUME CS: CODE,DS :DATA START: MOV AX,DATAMOV DS, AXLEA BX, NUMBER 1MOV AX, 0MOV DH, 0AA1: MOV DL, BXADD AX, DXINC BXCMP DL,255JNZ AA1MOV SUMI, AXMOV AH,4cHINT21HCODE ENDSEND START先判断后执行(条件控制)DATA SEGMENTORG 1000HNUMBER 1 DB 1,2,3,.255SUMI DW?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATAMOV DS, AXLEA BX, NUMBER1-1 产*MOV AX, 255 ;初值为255,原因如下说明MOV DH, 0AA1: INC BXMOV DL, BXCMP DL,255;DL=255 时,循环结束JZ AA2ADD AX, DXJMP AA1AA2: MOV SUMI, AXMOV AH,4cHINT21HCODE ENDSEND START.变量控制循环(结束条件已知)数据255的地址为lOFeh,做为变量控制的结束条件先执行后判断(变量控制)DATA SEGMENTORG 1000HNUMBER! DB 1,2,3,.255地址lOFEh地址lOFEhSUMI DW?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATAMOV DS, AXLEA BX, NUMBER 1MOV AX, 0MOV DH, 0AA1: MOV DL, BXADD AX, DXINC BXCMP BX, 10FFHJNZ AA1MOV SUM, AXMOV AH,4cHINT21HCODE ENDSEND START先判断后执行(变量控制)DATA SEGMENTORG 1000HNUMBER 1 DB 1,2,3,.255SUMI DW?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATAMOV DS, AXLEA BX, NUMBER1-1 /*MOV AX9 0 ;MOV DH, 0AA1: INC BXCMP BX,10FFH ;255 的地址为 10FEHJZ AA2MOV DL, BXADD AX, DXJMP AA1AA2: MOV SUMI, AXMOV AH,4CHINT21HCODE ENDSEND START533单重循环例532求无符号整数的平方根的整数部分,=i + 3 + 5 + (2-1)DATA SEGMENT NUMBER 1DW 25R00T1DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART: MOV AX,DATA MOV DS, AXMOV AX, NUMBER1MOV CX,0;MOV BX, 1AA1: SUBINCAA1: SUBINCAX, BXCXADD BX, 2JNC AA1DEC CXMOV ROOT1,CXMOV AH,4CH INT21H CODE ENDS END START例在CRT上显示“中”字的源程序 DATA SEGMENTNUMBER 1 DB OAH, ODH DB ' A OAH, ODH DB 'AAAAAAA', OAH, ODH DB 'A A A OAH, ODH DB 'A A A OAH, ODH DB 6AAAAAAA OAH, ODH DB 6A OAH, ODHDB 4A OAH, ODHDATA ENDSCODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA