Outtishi macro haha
Push DX
PUSH AX
Lea dx, haha
MoV ah, 9
Int 21 h
Endm
. 386
Data Segment use16
Buf1 DW 123,100,-1,-2,-3,-4,-2,-5
N = ($-buf1)/2
Clrt dB 0dh, 0ah, '$'
Print dB 'sort result: $'
Data ends
Stack segment use16 Stack
DB 200 DUP (0)
Stack ends
Code segment use16
Assume Cs: code, DS: data, SS: Stack
Start:
MoV ax, Data
MoV ds, ax
; Call the sort subroutine
Call sort
; Output prompt
Outtishi print
Outtishi clrt
Call the subroutine to convert a value to a string, and call the interrupt output No. 2.
MoV Cl, N; set the number of times to call this subroutine
MoV Di, 0
Lp1:
Call f10ta_xch
Add Di, 2; process the next word of data
Loop lp1; cx-1 = Cx, CX is not equal to 0, then loop
MoV ah, 4ch
Int 21 h
Sort proc near
Push CX
PUSH AX
Push Si
Push di
Push BX
; Bubble Sorting
MoV BL, 1; flag
MoV Cl, n
MoV CH, 0
Dec Cl
Loop1:
MoV Di, CX; Di retention cycles
MoV Si, 0
Cmp bl, 0
Je exit
Xor bl, BL; BL please 0
Loop2:
MoV ax, buf1 [Si]
CMP ax, buf1 [Si + 2]
JG loop3; if the first one is greater than the last one, add Si, 2, all push back one
Xchg buf1 [Si + 2], ax; values smaller than or equal
MoV buf1 [Si], ax
MoV BL, 1
Loop3:
Add Si, 2
Loop loop2; number of Cx internal cycles
MoV CX, Di
Loop loop1
Exit:
Pop BX
Pop di
Pop Si
Pop ax
Pop CX
RET
Sort endp
F10ta_xch proc near
Push CX
PUSH AX
Push Si
Push di
MoV Cl, 0
MoV ax, 0
MoV Si, 0ah; SI = 10
MoV ax, buf1 [di]
CMP ax, 0; check whether it is a negative number
Jge lp2
Neg ax; negative number, inverse
Push ax; Because ah is used, put ax into the stack first
MoV ah, 2; if it is a negative number, output '-'
MoV DL ,'-'
Int 21 h
Pop ax
Lp2:
MoV dx, 0; Be careful, the word Division dx is high 16 bits, ax is low 16 bits, without this, often divide Error
Div Si; for example, divide 15 by 10 to get 5 and 1; The operator has ax, and the remainder has DX
Push DX
INC Cl; records the number of remainder values into the stack.
CMP ax, 0; when the operator is 0, it indicates that all the digits of the data have been processed.
JNE lp2
Lp3:
Pop ax; Output Stack
Add Al, 30 h; convert to ASCII code Display
MoV DL, Al
MoV ah, 2
Int 21 h
Dec Cl
CMP Cl, 0
JNE lp3
; Two spaces
MoV Al, 20 h
MoV DL, Al
MoV ah, 2
Int 21 h
MoV Al, 20 h
MoV DL, Al
MoV ah, 2
Int 21 h
Pop di
Pop Si
Pop ax
Pop CX
RET
F10ta_xch endp
Code ends
End start