DATA SEGMENT
BUF DB 06H,5,3,94,3,156,255,4
LENGTH EQU $-BUF
MAX DB 00H
MIN DB 00H
DATA ENDS
CODE SEGMENT
MAIN PROC FAR
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA BX,BUF
MOV CX,LENGTH
CALL FIND_MINMAX
MOV MAX,AH
MOV MIN,AL
RET
MAIN ENDP
FIND_MINMAX PROC
PUSH BX
PUSH CX
JCXZ OUT_FUN ; if length is 0 , out
MOV AL,[BX] ; SET MIN = BUF[0]
MOV AH,[BX] ; SET MAX = BUF[0]
LOOP_FIND:
CMP AL,[BX] ; IF MIN > BUF[I],MIN = BUF[I]
JNA NEXT2MAX ; ELSE CMPARE MAX
MOV AL,[BX] ; JA
NEXT2MAX:
CMP AH,[BX] ; IF MAX < ...
JNB CMP_END
MOV AH,[BX] ; JB
CMP_END:
INC BX
LOOP LOOP_FIND
POP CX
POP BX
OUT_FUN: RET
FIND_MINMAX ENDP
CODE ENDS
END MAIN
运行到如图 MAIN 过程的return 处,就会不断死循环
自己的代码
但是,老师给的示例代码:
[Asm] 纯文本查看复制代码
DATA SEGMENT ;数据段
BUF DB 8FH,90H,45H,0F9H,20H,23H,11H,01H
DLEN EQU $ - BUF
MAX DB 00H ;存放最大值
MIN DB 00H ;存放最小值
DATA ENDS
EXTRA SEGMENT ; 附加段
EXTRA ENDS
;
STACK SEGMENT PARA STACK ‘STACK’ ;堆栈段
STAPN DB 100 DUP (00H) ;定义100字节空间
TOP EQU LENGTH STAPN
STACK ENDS
CODE SEGMENT
MAIN PROC FAR ;过程定义语句
;说明4个段寄存器分别与哪些段有关
ASSUME CS:CODE, DS:DATA,ES:EXTRA,SS:STACK
START:
MOV AX,DATA
MOV DS,AX
MOV AX, EXTRA
MOV ES,AX
MOV AX,STACK
MOV SS,AX
; 此处输入代码
MOV SI, OFFSET BUF
MOV CX, DLEN
CALL FIND_MAXMIN
MOV MAX,BH
MOV MIN, BL
RET
MAIN ENDP ;MAIN过程结束
;输入 SI 指向内存单元的指针
; CX 数据段的长度
;输出 BH--->MAX BL---->MIN
FIND_MAXMIN PROC NEAR
PUSH SI
PUSH CX
JCXZ OUT_P
MOV BH, [SI]
MOV BL, [SI]
STRAT_LOOP: MOV AL,[SI]
INC SI
CMP AL,BH
JA SAVE_MAX
CMP AL,BL
JB SAVE_MIN
JMP END_LOOP:
SAVE_MAX: MOV BH,AL
JMP END_LOOP
SAVE_MIN: MOV BL,AL
END_LOOP: LOOP STRAT_LOOP
POP CX
POP SI
OUT_P: RET
FIND_MAXMIN ENDP
CODE ENDS ;代码段结束
END MAIN ;整个源代码结束