Last active
December 21, 2015 08:09
-
-
Save hjpotter92/6276093 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TITLE Sort: Program to sort numbers as they are inputted by user | |
.MODEL SMALL | |
.STACK 100H | |
.DATA | |
MyARR DW 200 DUP(?) | |
PROMPT DB 10, 13, "Input number? (Press N to quit): $" | |
SEP DB ", $" | |
LST DB 10, 13, "Current array has: $" | |
CR EQU 13 | |
LF EQU 10 | |
.CODE | |
MAIN PROC | |
MOV AX, @DATA | |
MOV DS, AX | |
XOR CX, CX | |
XOR SI, SI | |
CHOICE: | |
XOR BX, BX | |
LEA DX, PROMPT | |
MOV AH, 9 | |
INT 21H | |
MOV AH, 1 | |
INT 21H | |
CMP AL, 'N' | |
JE QUITTING | |
CMP AL, 'n' | |
JE QUITTING | |
INC CX | |
CONV2DEC: | |
AND AX, 000FH | |
PUSH AX | |
MOV AX, 10 | |
MUL BX | |
POP BX | |
ADD BX, AX | |
MOV AH, 1 | |
INT 21H | |
CMP AL, CR | |
JNE CONV2DEC | |
MOV MyARR[SI], BX | |
ADD SI, 2 | |
PUSH SI | |
XOR SI, SI | |
CALL SELSORT | |
POP SI | |
CALL DISPLAY | |
JMP CHOICE | |
QUITTING: | |
XOR SI, SI | |
CALL SELSORT | |
CALL DISPLAY | |
MOV AH, 76 | |
INT 21H | |
MAIN ENDP | |
INCLUDE OUTDEC.ASM | |
INCLUDE SELSORT.ASM | |
DISPLAY PROC | |
;;; INPUT | |
;;; CX: Number of array elements | |
;;; Name of array is MyARR | |
PUSH AX | |
PUSH CX | |
PUSH SI | |
XOR SI, SI | |
LEA DX, LST | |
MOV AH, 9 | |
INT 21H | |
SHOWING: | |
MOV AX, MyARR[SI] | |
CALL OUTDEC | |
ADD SI, 2 | |
LEA DX, SEP | |
MOV AH, 9 | |
INT 21H | |
LOOP SHOWING | |
POP SI | |
POP CX | |
POP AX | |
RET | |
DISPLAY ENDP | |
END MAIN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OUTDEC PROC | |
;;; INPUT | |
;;; AX: Number to be printed as decimal | |
PUSH AX | |
PUSH BX | |
PUSH CX | |
PUSH DX | |
OR AX, AX | |
JGE QUIT | |
PUSH AX | |
MOV DL, '-' | |
MOV AH, 2 | |
INT 21H | |
POP AX | |
NEG AX | |
QUIT: | |
XOR CX, CX | |
MOV BX, 10 | |
REPEAT: | |
XOR DX, DX | |
DIV BX | |
PUSH DX | |
INC CX | |
CMP AX, 0 | |
JNE REPEAT | |
MOV AH, 2 | |
PRINT: | |
POP DX | |
ADD DL, '0' | |
INT 21H | |
LOOP PRINT | |
POP DX | |
POP CX | |
POP BX | |
POP AX | |
RET | |
OUTDEC ENDP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELSORT PROC | |
;;; INPUT | |
;;; CX: Size of array | |
;;; SI: Offset address | |
;;; Name of array var is "MyARR" | |
PUSH AX | |
PUSH BX | |
PUSH CX | |
PUSH DX | |
PUSH SI | |
MOV BX, CX | |
DEC BX | |
JE END_SORT | |
MOV DX, SI | |
SORT_LOOP: | |
MOV SI, DX | |
MOV CX, BX | |
MOV DI, SI | |
MOV AX, MyARR[DI] | |
FIND_BIG: | |
ADD SI, 2 | |
CMP MyARR[SI], AX | |
JNG NEXT | |
MOV DI, SI | |
MOV AX, MyARR[DI] | |
NEXT: | |
LOOP FIND_BIG | |
CALL SWAP | |
DEC BX | |
JNE SORT_LOOP | |
END_SORT: | |
POP SI | |
POP DX | |
POP CX | |
POP BX | |
POP AX | |
RET | |
SELSORT ENDP | |
SWAP PROC | |
;;; Swaps elements at [SI] and [DI] | |
;;; in MyARR using AX as temporary variable. | |
PUSH AX | |
MOV AX, MyARR[SI] | |
XCHG AX, MyARR[DI] | |
MOV MyARR[SI], AX | |
POP AX | |
RET | |
SWAP ENDP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment