-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2.linear_search.asm
129 lines (86 loc) · 3.41 KB
/
2.linear_search.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
;SUBMITTED BY - SUSHMITA YADAV
;ROLL NO - 38
;ALP TO PERFORM LINEAR SEARCH OVER AN ARRAY
DATA SEGMENT
ARRAY DB 20 DUP(?)
LEN DB ?
NUM DB ?
TEN DD 10
msg1 DB 'Enter length of array: ','$'
msg2 DB 10, 'Enter element: ','$'
msg3 DB 10, 10, 'Enter number to be searched: ','$'
msg4 DB 10, 10, 'ELEMENT FOUND AT POSITION: ','$'
msg5 DB 10, 10, 'ELEMENT NOT FOUND!','$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV ES, AX ;overlapping ES and DS segment
LEA DX, msg1
CALL OUTPUT_MSG
CALL READH ;getting length of array
CMP BL, 20
JG EXIT
MOV LEN, BL ;saving length in len
XOR CX, CX
MOV CL, LEN
CLD
MOV DI, OFFSET ARRAY
INPUT: LEA DX, msg2 ;reading elements of array
CALL OUTPUT_MSG
CALL READH
MOV AL, BL
STOSB
LOOP INPUT
LEA DX, msg3 ;getting element to be searched
CALL OUTPUT_MSG
CALL READH
MOV NUM, BL ;saving number in 'num'
CALL LSEARCH
CMP CL, LEN ;when element is not found in the array
JLE FOUND
LEA DX, msg5
CALL OUTPUT_MSG
JMP EXIT ;exiting
FOUND: LEA DX, msg4 ;if element is found -> it's index is displayed
CALL OUTPUT_MSG
MOV DL, CL
ADD DL, 30H
MOV AH, 2H
INT 21H
EXIT: HLT
CODE ENDS
READH PROC NEAR ;procedure to read input and save it in BL
XOR DX, DX
XOR BX, BX
REPEAT: MOV AH, 01H
INT 21H
CMP AL, 13 ;returns when enter is pressed
JE RETURN
AND AX, 0FH ;converts ascii to numeric
MOV DL, AL
XCHG AX, BX
MUL TEN
ADD BX, AX
JMP REPEAT
RETURN: RET
READH ENDP
OUTPUT_MSG PROC NEAR ;procedure to display a string on console window
MOV AH, 09H
INT 21H
RET
OUTPUT_MSG ENDP
LSEARCH PROC NEAR
MOV CL, 1
MOV SI, OFFSET ARRAY
MOV BL, NUM ;element to be searched
SEARCH: LODSB
CMP AL, BL ;element found!
JE RE
INC CL
CMP CL, LEN
JBE SEARCH
RE: RET
LSEARCH ENDP
END START