汇编语言循环程序设计,怎么复制和排序?

2024-12-01 15:32:00
推荐回答(1个)
回答1:

ASSUME DS:seg1, CS:seg2

seg1 SEGMENT
first DB 5,-8,3,9,-2,-5,7,-9,0,5,4,9,-6,1,-1,8,-3,6,2,-7,0,-4
LEN_first EQU $ - first

second DB 30 DUP(?)

seg1 ENDS

seg2 SEGMENT
start:
MOV AX, seg1
MOV DS, AX
;--------------------------下面是复制

;2.复制
;将first行中的非负数复制到second行

MOV SI, 0
MOV DI, 0
MOV CX, LEN_first
LOP:
MOV AL, first [SI]
CMP AL, 80H
JNB NEXT
MOV second [DI], AL
INC DI
NEXT:
INC SI
LOOP LOP

;--------------------------下面是排序

;3.排序
;将second行中的数据按升序重新排列。

MOV CX, DI
DEC CX
LP1:
PUSH CX
MOV SI, 0
LP2:
MOV AL, second [SI]
CMP AL, second [SI + 1]
JB NEXT2
XCHG AL, second [SI + 1]
MOV second [SI], AL
NEXT2:
INC SI
LOOP LP2
POP CX
LOOP LP1

;--------------------------下面是结束程序

EXIT:
MOV AH, 4CH
INT 21H

seg2 ENDS
END start