|
; Sort and output the signed decimal number from the input, but the output is incorrect, please ask the teacher what is wrong?
M_DIRECT MACRO STRING; Define a macro, call the 09 DOS function to display a string on the screen
MOV DX, OFFSET STRING
MOV AH, 09
INT 21H
ENDM
DATA SEGMENT
COUNT_NUM DB 'The count of numbers to sort: $'; Prompt for the number of data to be sorted
INMSG DB 'Please input the sorting numbers:', 0dh, 0ah, '$'; prompt for the data to be sorted
OUTMSG DB 'The sorting result:', 0dh, 0ah, '$'; Prompt for sorted data on the screen
COUNT = 100
WTEMP DW?
ARRAY DW COUNT DUP (?); Used to save the entered data
CRLF DB 13,10, '$'; realize the function of carriage return and line feed, 13 → 0DH, carriage return; 10 → 0AH, line feed
LEN DW?; Save the number of data actually entered
DATA ENDS
CODE SEGMENT
MAIN PROC FAR
ASSUME CS: CODE, DS: DATA
; ************************************************ ***********************************************
START:
PUSH DS
SUB AX, AX; set AX to 0
PUSH AX
MOV AX, DATA; Send the address of the data segment to DX
MOV DS, AX
M_DIRECT COUNT_NUM; Macro call, prompt on the screen for the number of data to be entered
CALL READ; Call subroutine P-GETNEW, enter the number of data to be entered
MOV CX, AX
MOV LEN, AX
LEA BX, ARRAY
MOV AH, 09; Call function 09 to realize carriage return and line feed
LEA DX, CRLF
INT 21H; Screen prompts for data to be sorted
M_DIRECT INMSG
GETNUMBER:; Save input data to ARRAY
CALL READ
MOV [BX + SI], DX
ADD SI, 2
LOOP GETNUMBER
SORT:; Call SORT subroutine sort
CALL data_SORT
MOV AH, 09
LEA DX, CRLF
INT 21H
M_DIRECT OUTMSG
CALL OUTPUT; output sorted data
; RET
MAIN ENDP
; ************************************************ ***************************************
Read data
; ************************************************ *************************************
READ PROC; subroutine to enter a data from the keyboard
PUSH BX
PUSH CX
PUSH DX
XOR BX, BX
XOR CX, CX
MOV AH, 1
INT 21H
CMP AL, '+'
JZ READ1
CMP AL, '-'
JNZ READ2
MOV CX, -1
READ1: MOV AH, 1
INT 21H
READ2: CMP AL, '0'
JB READ3
CMP AL, '9'
JA READ3
SUB AL, 30H
SHL BX, 1
SHL BX, 1
ADD BX, DX
MOV AH, 0
ADD BX, AX
JMP READ1
READ3: CMP CX, 0
JZ READ4
NEG BX
READ4: MOV AX, BX
POP DX
POP CX
POP BX
RET
READ ENDP
; ************************************************ ******************************************
; Sort subroutine
; ************************************************ ***************************************
data_SORT PROC
PUSH BX
PUSH CX
PUSH DX
MOV SI, OFFSET LEN
MOV CX, [SI]
mov ax, data
mov ds, ax
; MOV SI, OFFSET ARRAY
; MOV DX, [SI]
; mov cx, 4
dec cx
mov bx, 0
mov si, 0
you: mov di, cx
mov ax, array [si]
mov bx, si
twop: cmp ax, array [bx + 2]
jle on1
mov ax, array [bx + 2]
mov dx, bx
inc dx
inc dx
on1: inc bx
inc bx
dec di
cmp di, 0
jnz twop
xchg ax, array [si]
mov bx, dx
mov array [bx], ax
inc si
inc si
loop you
; mov ah, 4ch
; int 21h
POP DX
POP CX
POP BX
data_SORT ENDP
; ************************************************ ***********************************
Data output
; ************************************************ *************************************
OUTPUT PROC; data output subroutine
MOV CX, LEN
MOV BX, OFFSET ARRAY
AGAIN: MOV AX, [BX]
MOV WTEMP, AX
JMP WRITE
ADD BX, 2
MOV AH, 09H
LEA DX, CRLF
INT 21H
LOOP AGAIN
WRITE: PUSH AX
PUSH BX
PUSH DX
MOV AX, WTEMP
TEST AX, 80h; AX
JNZ WRITE1
MOV DL, '0'
MOV AH, 2
INT 21H
JMP WRITE5
WRITE1: JNS WRITE2
MOV BX, AX
MOV DL, '-'
MOV AH, 2
INT 21H
MOV AX, BX
NEG AX
WRITE2: MOV BX, 10
PUSH BX
WRITE3: CMP AX, 0
JZ WRITE4
SUB DX, DX
DIV BX
ADD DL, 30H
PUSH DX
JMP WRITE3
WRITE4: POP DX
CMP DL, 10
JE WRITE5
MOV AH, 2
INT 21H
JMP WRITE4
WRITE5: POP DX
POP BX
POP AX
RET
OUTPUT ENDP
; ************************************************ ***************************
; ************************************************ ****************************
CODE ENDS
END START |
|