1、第 5 章 分支与循环程序设计5.1. 概述设计步骤:描述问题 确定算法 绘制流程图分配存储空间和工作单元编写程序上机调试5.2. 分支程序设计5.2.1 分支程序结构二路分支结构 多路分支结构5.2.2 二路分支程序设计方法例 5.2.1 )0(1xY-128x127DATA SEGMENTXX1 DB 10; X=给定一个值YY1 DB ? ; YDATA ENDSCODE SEGMENT条件? 条件?。 。ASSUME CS:CODE, DS:DATASTART: MOV AX, DSMOV DS, AXMOV AL,XX1CMP AL, 0JZ AA1; =0JNS AA2MOV AL
2、,0FFH; =-1JMP AA1AA2: MOV AL,1; =1 AA1: MOV YY1,AL; MOV AH,4CHINT 21HCODE ENDSEND START5.2.3 多路分支程序设计方法逻辑分解法;地址表法; 段内转移表法;1. 逻辑分解法CODE SEGMENTASSUME CS:CODESTART:MOV AH,1INT 21H ;键入值CMP AL,31HJZ WORK1CMP AL,32HJZ WORK2CMP AL,33HJZ WORK3CMP AL,34HJZ WORK4CMP AL,35HJZ WORK5JMP WORK0WORK1: (jmp work0)W
3、ORK2: (jmp work0)WORK3: (jmp work0)WORK4: (jmp work0)WORK5: (jmp work0)WORK0: MOV AH,4CHINT 21HCODE ENDSEND START2. 地址表法把模块的地址保存在一个表中,通过查表跳到相应模块。表地址 = 表首地址 + (键号-1)2DATA SEGMENTTABLE DW WORK1,WORK2,WORK3,WORK4,WORK5DATA ENDSCODE SEGMENTASSUME CS:CODE, DS:DATASTART:MOV AX,DATAMOV DS,AXLEA BX,TABLE ;表
4、首地址MOV AH,1INT 21HAND AL,0FHDEC AL ;键号-1ADD AL,AL ;(键号-1)*2SUB AH,AHADD BX,AX ;表地址 = 表首地址 + (键号-1)2JMP WORD PTRBXWORK1: (jmp work0)WORK2: (jmp work0)WORK3: (jmp work0)WORK4: (jmp work0)WORK5: (jmp work0)WORK0:MOV AH,4CHINT 21HCODE ENDSEND START3. 段内转移表法段内短转移:(jmp disp8 为 2 字节指令)转移表地址=转移表首地址+( 键号-1)2
5、段内近转移:(jmp disp16 为 3 字节指令)转移表地址=转移表首地址+( 键号-1)3段间远转移:(jmp disp32 为 4 字节指令)转移表地址=转移表首地址+( 键号-1)4CODE SEGMENTASSUME CS:CODESTART:LEA BX,WORKMOV AH,1INT 21HAND 0FHDEC ALMOV AH,ALADD AL,ALADD AL,AH ;x3SUB AH,AHADD BX,AXJMP BX;转移表WORK: JMP NEAR PTR WORK1JMP NEAR PTR WORK2JMP NEAR PTR WORK3JMP NEAR PTR W
6、ORK4JMP NEAR PTR WORK5;工作模块WORK1: (jmp work0)WORK2: (jmp work0)WORK3: (jmp work0)WORK4: (jmp work0)WORK5: (jmp work0)WORK0:MOV AH,4CHINT 21HCODE ENDSEND START5.3. 循环程序设计5.3.1 循环程序的结构形式5.3.2 循环程序的设计方法方法:计数控制循环;条件控制循环;变量控制循环1. 计数控制循环(循环次数已知)例:5.3.1, 把 1,2,3 255 加起来。(1)先执行后判断(计数控制)DATA SEGMENTORG 1000H
7、NUMBER1 DB 1,2,3,255SUM1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1MOV AX, 0MOV DH, 0MOV CL, 255AA1: MOV DL, BXADD AX, DXINC BXSUB CL,1 ; DEC CLJNZ AA1MOV SUM1, AXMOV AH,4CHINT 21HCODE ENDSEND START(2)先判断后执行(计数控制)DATA SEGMENTORG 1000HNUMBER1 DB 1,2,3,25
8、5SUM1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1-1MOV AX, 0MOV DH, 0MOV CL, 0 ; 初值 256AA1: INC BXSUB CL,1JZ AA2MOV DL, BXADD AX, DXJMP AA1AA2: MOV SUM1, AXMOV AH,4CHINT 21HCODE ENDSEND START2. 条件控制循环(结束条件已知)(1)先执行后判断(条件控制)DATA SEGMENTORG 1000HNUMBER1 DB
9、 1,2,3,255SUM1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1MOV AX, 0MOV DH, 0 AA1: MOV DL, BXADD AX, DXINC BXCMP DL,255JNZ AA1MOV SUM1, AXMOV AH,4CHINT 21HCODE ENDSEND START(2)先判断后执行(条件控制) DATA SEGMENTORG 1000HNUMBER1 DB 1,2,3,255SUM1 DW ?DATA ENDSCODE SE
10、GMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1-1 ;*MOV AX, 255 ; 初值为 255,原因如下说明MOV DH, 0 AA1: INC BXMOV DL, BXCMP DL,255 ; DL=255 时,循环结束JZ AA2ADD AX, DXJMP AA1AA2: MOV SUM1, AXMOV AH,4CHINT 21HCODE ENDSEND START3. 变量控制循环(结束条件已知)数据 255 的地址为 10Feh,做为变量控制的结束条件(1)先执行后判断(变量控制)DATA
11、SEGMENTORG 1000HNUMBER1 DB 1,2,3,255SUM1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1MOV AX, 0MOV DH, 0 地址 10FEhAA1: MOV DL, BXADD AX, DXINC BXCMP BX, 10FFHJNZ AA1MOV SUM, AXMOV AH,4CHINT 21HCODE ENDSEND START(2)先判断后执行(变量控制) DATA SEGMENTORG 1000HNUMBER1 D
12、B 1,2,3,255SUM1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA BX, NUMBER1-1 ;*MOV AX, 0 ; MOV DH, 0 AA1: INC BXCMP BX,10FFH ;255 的地址为 10FEH JZ AA2 MOV DL, BXADD AX, DXJMP AA1AA2: MOV SUM1, AXMOV AH,4CHINT 21HCODE ENDSEND START5.3.3 单重循环例 5.3.2 求无符号整数的平方根的整数部分, )12(.53
13、12nnDATA SEGMENT NUMBER1 DW 25ROOT1 DW ?DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXMOV AX, NUMBER1MOV CX, 0; MOV BX, 1 AA1: SUB AX, BX INC CXADD BX, 2 JNC AA1DEC CXMOV ROOT1, CX MOV AH,4CHINT 21HCODE ENDSEND START例 5.3.3 在 CRT 上显示“中”字的源程序DATA SEGMENT NUMBER1 DB 0AH, 0DHDB
14、A , 0AH, 0DHDB AAAAAAA, 0AH, 0DHDB A A A, 0AH, 0DHDB A A A, 0AH, 0DHDB AAAAAAA, 0AH, 0DHDB A , 0AH, 0DHDB A , 0AH, 0DHDATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXLEA, BX, NUMBER1MOV CX, 65AA1: MOV DL, BXMOV AH, 2INT 21HINC BXLOOP AA1 MOV AH,4CHINT 21HCODE ENDSEND START例 5.3.4 在 CRT 上显示 16 位十进制数 8988998899989899DATA SEGMENT RULER1 DW 0100110011101011 ;(逻辑尺法) DATA ENDSCODE SEGMENTASSUME CS:CODE,DS:DATASTART:MOV AX,DATAMOV DS, AXMOV BX, RULER1MOV CX, 16AA1: MOV DL, 38HSHL BX, 1JNC AA2INC DL ; MOV DL, 39HAA2: MOV AH, 2 INT 21HLOOP AA1 MOV AH,4CHINT 21HCODE ENDSEND START