汇编语言:数据段中,字数组ARRAY有N个元素 ,把数组中的负数求绝对值后按降序排列,用子程序实现
以前回答过:
/asm_c/blog/static/2482031132015581028111
用子程序实现,就要麻烦一些,如下:
DATASEGMENT
ARRAY1?DW?0,?-1,?2,?3,?4,?-5,?6,?-7,?8?,?9,?-10
COUNT?EQU?$?-?ARRAY1
ARRAY2?DW?20?DUP(?);求绝对值后存入的新数组
LENDW;新数组长度?
MSG1DB?13,?10,?'Before?Sort?:?$'
MSG2DB?13,?10,?'AfterSort?:?$'
DATA?ENDS
CODE?SEGMENT
ASSUME?CS:?CODE,?DS:?DATA
START:
MOVAX,?DATA?
MOVDS,?AX
MOVAH,?09H
LEADX,?MSG1
INT21H?;显示提示字符串MSG1
LEASI,?ARRAY1
CALL?OUTPUT;调用输出数组子程序OUTPUT,显示原始数据
CALL?Absolute?;调用求绝对值子程序Absolute,求绝对值
CALL?SORT?;调用排序子程序SORT对数组排序
MOVAH,?09H
LEADX,?MSG2
INT21H?;显示提示字符串MSG2?
LEASI,?ARRAY2
CALL?OUTPUT;调用输出数组子程序OUTPUT,显示求绝对值后的数据?
EXIT:
MOVAH,?4CH
INT21H?;主程序返回DOS
;-----------------------------------------------?
;子程序名:SORT
;功能:?
SORT?PROC?NEAR?
;此处请增添代码
MOVCX,?COUNT
SHRCX,?1
DECCX
S0:?PUSH?CX
MOVDI,?SI
S1:?MOVAX,?[DI]
CMPAX,?[DI?+?2]
JAS3
XCHG?AX,?[DI?+?2]
XCHG?AX,?[DI]
S3:?ADDDI,?2
LOOP?S1
POPCX
LOOP?S0?
RET
SORT?ENDP
;------------------------------------------------?
;子程序名:Absolute
;功能:求绝对值?
Absolute?PROC?NEAR
;此处请增添代码
LEASI,?ARRAY1
LEADI,?ARRAY2
MOVCX,?COUNT
SHRCX,?1
ABS:
MOVAX,?[SI]
CMPAX,?0
JGEABS2
NEGAX
ABS2:
MOV[DI],?AX
ADDSI,?2
ADDDI,?2
LOOP?ABS
RET
Absolute?ENDP?
;------------------------------------------------?;子程序名:OUTPUT
;功能:输出数组
OUTPUT?PROC?NEAR?
;此处请增添代码
MOVAH,?2
MOVDL,?13
INT21H
MOVDL,?10
INT21H
MOVCX,?COUNT
SHRCX,?1
ZH3:
MOVAX,?[SI]
CMPAX,?0
JGEZH0
NEGAX
MOV[SI],?AX
MOVAH,?2
MOVDL,?'-'
INT21H
ZH0:MOVAX,?[SI]
MOVBX,?10
PUSH?CX
MOVCX,?0
ZH1:MOVDX,?0
DIVBX
ADDDL,?'0'
PUSH?DX
INCCX
CMPAX,?0
JNZZH1
MOVAH,?2
ZH2:POPDX
INT21H
LOOP?ZH2
POPCX
MOVDL,?'?'
INT21H
ADDSI,?2
LOOP?ZH3
RET
OUTPUT?ENDP
;-------------------------------------------------
CODEENDS
ENDSTART