以前回答过:
http://blog.163.com/asm_c/blog/static/2482031132015581028111
用子程序实现,就要麻烦一些,如下:
DATA SEGMENT
ARRAY1 DW 0, -1, 2, 3, 4, -5, 6, -7, 8 , 9, -10
COUNT EQU $ - ARRAY1
ARRAY2 DW 20 DUP(?) ;求绝对值后存入的新数组
LEN DW ? ;新数组长度
MSG1 DB 13, 10, 'Before Sort : $'
MSG2 DB 13, 10, 'After Sort : $'
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AH, 09H
LEA DX, MSG1
INT 21H ;显示提示字符串MSG1
LEA SI, ARRAY1
CALL OUTPUT ;调用输出数组子程序OUTPUT,显示原始数据
CALL Absolute ;调用求绝对值子程序Absolute,求绝对值
CALL SORT ;调用排序子程序SORT对数组排序
MOV AH, 09H
LEA DX, MSG2
INT 21H ;显示提示字符串MSG2
LEA SI, ARRAY2
CALL OUTPUT ;调用输出数组子程序OUTPUT,显示求绝对值后的数据
EXIT:
MOV AH, 4CH
INT 21H ;主程序返回DOS
;-----------------------------------------------
;子程序名:SORT
;功能:
SORT PROC NEAR
;此处请增添代码
MOV CX, COUNT
SHR CX, 1
DEC CX
S0: PUSH CX
MOV DI, SI
S1: MOV AX, [DI]
CMP AX, [DI + 2]
JA S3
XCHG AX, [DI + 2]
XCHG AX, [DI]
S3: ADD DI, 2
LOOP S1
POP CX
LOOP S0
RET
SORT ENDP
;------------------------------------------------
;子程序名:Absolute
;功能:求绝对值
Absolute PROC NEAR
;此处请增添代码
LEA SI, ARRAY1
LEA DI, ARRAY2
MOV CX, COUNT
SHR CX, 1
ABS:
MOV AX, [SI]
CMP AX, 0
JGE ABS2
NEG AX
ABS2:
MOV [DI], AX
ADD SI, 2
ADD DI, 2
LOOP ABS
RET
Absolute ENDP
;------------------------------------------------
;子程序名:OUTPUT
;功能:输出数组
OUTPUT PROC NEAR
;此处请增添代码
MOV AH, 2
MOV DL, 13
INT 21H
MOV DL, 10
INT 21H
MOV CX, COUNT
SHR CX, 1
ZH3:
MOV AX, [SI]
CMP AX, 0
JGE ZH0
NEG AX
MOV [SI], AX
MOV AH, 2
MOV DL, '-'
INT 21H
ZH0:MOV AX, [SI]
MOV BX, 10
PUSH CX
MOV CX, 0
ZH1:MOV DX, 0
DIV BX
ADD DL, '0'
PUSH DX
INC CX
CMP AX, 0
JNZ ZH1
MOV AH, 2
ZH2:POP DX
INT 21H
LOOP ZH2
POP CX
MOV DL, ' '
INT 21H
ADD SI, 2
LOOP ZH3
RET
OUTPUT ENDP
;-------------------------------------------------
CODE ENDS
END START
DATA SEGMENT
ARRAY1 DW 0, -1, 2, 3, 4, -5, 6, -7, 8 ,9,-10
COUNT EQU ($-ARRAY1)/2
ARRAY2 DW COUNT DUP(?) ;求绝对值后存入的新数组
LEN EQU COUNT ;新数组长度
MSG1 DB 13, 10, 'Before Sort:$ '
MSG2 DB 13, 10, 'After Sort:$ '
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AH, 09H
LEA DX, MSG1
INT 21H ;显示提示字符串MSG1
LEA SI,ARRAY1
CALL OUTPUT ;调用输出数组子程序OUTPUT,显示原始数据
CALL ABSOLUTE ;调用求绝对值子程序Absolute,求绝对值
CALL SORT ;调用排序子程序SORT对数组排序
MOV AH, 09H
LEA DX, MSG2
INT 21H ;显示提示字符串MSG2
LEA SI,ARRAY2
CALL OUTPUT ;调用输出数组子程序OUTPUT,显示求绝对值后的数据
MOV AH, 4CH
INT 21H ;主程序返回DOS
;-----------------------------------------------
;子程序名:SORT
;功能:
;-----------------------------------------------
SORT PROC NEAR
;///////此处请增添代码
; 数组长度置入cx,字数组首地址置入 si
PUSH AX
PUSH CX
PUSH DX
PUSH SI
PUSH DI
PUSHF
MOV CX,COUNT
PUSH CX
POP DX
DEC DX
@SORTWL1:
MOV CX,DX
MOV DI,SI
@SORTWL2:
MOV AX,[DI+2]
CMP AX,[DI]
JL @SORTWNEXT ; 从大到小(无符号换成 JB)
XCHG AX,[DI]
MOV [DI+2],AX
@SORTWNEXT:
INC DI
INC DI
LOOP @SORTWL2
DEC DX
JNZ @SORTWL1
POPF
POP DI
POP SI
POP DX
POP CX
POP AX
RET
SORT ENDP
;------------------------------------------------
;子程序名:Absolute
;功能:求绝对值
;-------------------------------------------------
ABSOLUTE PROC NEAR
;///////此处请增添代码
PUSH DS
POP ES
LEA SI,ARRAY1
LEA DI,ARRAY2
MOV CX,COUNT
@ABS1:
LODSW
AND AX,AX
JNS @ABS2
NEG AX
@ABS2:
STOSW
LOOP @ABS1
RET
ABSOLUTE ENDP
;------------------------------------------------
;子程序名:OUTPUT
;功能:输出数组
;-------------------------------------------------
OUTPUT PROC NEAR
;///////此处请增添代码
MOV CX,COUNT
@OUT1:
LODSW
CALL DSPAXS
MOV DL,32
MOV AH,2
INT 21H
LOOP @OUT1
RET
OUTPUT ENDP
;===============================
DSPAXS PROC NEAR
PUSH AX
TEST AX,8000H
JNS @DSPAXS1
PUSH AX
PUSH DX
MOV AH,2
MOV DL,'-'
INT 21H
POP DX
POP AX
NEG AX
@DSPAXS1:
CALL DSPAX
POP AX
RET
DSPAXS ENDP
;===============================
DSPAX PROC NEAR
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSHF
XOR CX,CX
MOV BX,10
@DSPAX1:
XOR DX,DX
DIV BX
INC CX
OR DX,30H
PUSH DX
CMP AX,0
JNE @DSPAX1
MOV AH,2
@DISPAX2:
POP DX
INT 21H
LOOP @DISPAX2
POPF
POP DX
POP CX
POP BX
POP AX
RET
DSPAX ENDP
;==============================
CODE ENDS
END START