-
Notifications
You must be signed in to change notification settings - Fork 1
/
3. Find Second Maximum among Three Numbers.asm
192 lines (132 loc) · 4 KB
/
3. Find Second Maximum among Three Numbers.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
; GIVEN THERE NUMBERS
; PRINT THE SECOND MAXIMUM NUMBER
; INPUT FORMAT:
; NUMBER_1,NUMBER_2,NUMBER_3
; NO SPACE BETWEEN TWO NUMBERS IN INPUT FORMAT
.MODEL SMALL
.STACK 100H
.DATA
CR EQU 0DH
LF EQU 0AH
X DB ?
Y DB ?
Z DB ?
SECOND_HIGHEST_NUMBER DB ?
SAME_VALUE DB ?
MSG DB 'ENTER THE NUMBERS: $'
MSG1 DB CR, LF, 'THE SECOND MAXIMUM IS: $'
EQL_MSG DB CR, LF, 'ALL NUMBERS ARE SAME$'
.CODE
MAIN PROC
; DATA SEGMENT INITIALIZATION
MOV AX, @DATA
MOV DS, AX
; PRINT THE MESSAGE ON CONSOLE TO TAKE INPUT
LEA DX, MSG
MOV AH, 9
INT 21H
; TAKING INPUT EVERY TIME USING (AH, 1) INSTEAD OF USING LOOP
MOV AH, 1
INT 21H
; MOVE THE VALUE TO X
MOV X, AL
;MOV AH, 1
INT 21H
;MOV AH, 1
INT 21H
; MOVE THE VALUE TO Y
MOV Y, AL
;MOV AH, 1
INT 21H
;MOV AH, 1
INT 21H
; MOVE THE VALUE TO Z
MOV Z, AL
JMP FIND_SECOND_MIN
; CALL FIND_SECOND_MIN TO FIND ANS
FIND_SECOND_MIN:
; COMPARE X AND Y FIRST
MOV AL, X
CMP AL, Y
; X EQUAL TO Y (X == Y)
JE EQUAL
; X GREATER THAN Y (X > Y)
JG GREATER
; X LESS THAN Y (X < Y)
JL SMALLER
EQUAL:
CMP AL, Z
; X GREATER THAN Z ( X==Y>Z )
JG PRINT_Z
; X EQUAL TO Z ( X==Y==Z )
JE PRINT_EQUAL
; X LESS THAN Z ( X==Y<Z )
JL PRINT_X
GREATER:
CMP AL, Z
; X EQUAL TO Z ( X==Z>Y )
JE PRINT_Y
; X LESS THAN Z ( Z>X>Y )
JL PRINT_X
; X GREATER THAN Z ( X>Y AND X>Z )
JG COMPARE_Y_Z
SMALLER:
CMP AL, Z
; X LESS THAN Z ( X<Y AND X<Z )
JL COMPARE_Y_Z_2
; X EQUAL TO Z ( X==Z<Y )
JE PRINT_X
; X GREATER THAN Z ( Y>X>Z )
JG PRINT_X
COMPARE_Y_Z:
MOV AL, Y
CMP AL, Z
; Y GREATER THAN Z ( X>Y>Z )
JG PRINT_Y
; Y LESS THAN Z
JL PRINT_Z
; Y EQUAL TO Z
JE PRINT_Z
COMPARE_Y_Z_2:
MOV AL, Y
CMP AL, Z
; Y GREATER THAN Z ( X<Z<Y )
JG PRINT_Z
; Y LESS THAN Z
JL PRINT_Y
; Y EQUAL TO Z
JE PRINT_X
PRINT_X:
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV DL, X
MOV AH, 2
INT 21H
JMP JUMP_END
PRINT_Y:
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV DL, Y
MOV AH, 2
INT 21H
JMP JUMP_END
PRINT_Z:
LEA DX, MSG1
MOV AH, 9
INT 21H
MOV DL, Z
MOV AH, 2
INT 21H
JMP JUMP_END
PRINT_EQUAL:
LEA DX, EQL_MSG
MOV AH, 9
INT 21H
JUMP_END:
; DOS INTERRUPT AND EXIT
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN