-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.asm
160 lines (160 loc) · 2.54 KB
/
string.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
section .bss
digit0: resb 1
digit1: resb 1
array: resb 50 ;Array to store 50 elements of 1 byte each.
element: resb 1
num: resb 1
pos: resb 1
temp: resb 1
ele: resb 1
section .data
msg1: db "Enter the number of elements : "
size1: equ $-msg1
msg2: db "Enter a number:"
size2: equ $-msg2
msg3: db "Enter the number to be searched : "
size3: equ $-msg3
msg_found: db "Element found at position : "
size_found: equ $-msg_found
msg_not: db "Element not found"
size_not: equ $-msg_not
section .text
global _start
_start:
;Printing the message to enter the number
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, size1
int 80h
;Reading the number
mov eax, 3
mov ebx, 0
mov ecx, digit1
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, digit0
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, temp
mov edx, 1
int 80h
sub byte[digit1], 30h
sub byte[digit0], 30h
mov al, byte[digit1]
mov dl, 10
mul dl
mov byte[num], al
mov al, byte[digit0]
add byte[num], al
mov al, byte[num]
mov byte[temp], al
mov ebx, array
reading:
push ebx ;Preserving The value of ebx in stack
;Printing the message to enter each element
mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, size2
int 80h
Introduction to NASM Page 38
;Reading the number
mov eax, 3
mov ebx, 0
mov ecx, digit1
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, digit0
mov edx, 2
int 80h
sub byte[digit1], 30h
sub byte[digit0], 30h
mov al, byte[digit1]
mov dl, 10
mul dl
add al, byte[digit0]
;al now contains the number
pop ebx
mov byte[ebx], al
add ebx, 1
dec byte[temp]
cmp byte[temp], 0 ;Comparing loop variable
jg reading ;Loop using branch statements
;Reading the number to be searched :.....
mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, size3
int 80h
;Reading the number
mov eax, 3
mov ebx, 0
mov ecx, digit1
mov edx, 1
int 80h
mov eax, 3
mov ebx, 0
mov ecx, digit0
mov edx, 2
int 80h
sub byte[digit1], 30h
sub byte[digit0], 30h
mov al, byte[digit1]
mov dl, 10
mul dl
Introduction to NASM Page 39
add al, byte[digit0]
mov byte[ele], al
movzx ecx, byte[num]
mov ebx, array
mov byte[pos], 1
searching:
push ecx
mov al , byte[ebx]
cmp al, byte[ele]
je found
add ebx, 1
pop ecx
add byte[pos], 1
loop searching
mov eax, 4
mov ebx, 1
mov ecx, msg_not
mov edx, size_not
int 80h
exit:
mov eax, 1
mov ebx, 0
int 80h
found:
mov eax, 4
mov ebx, 1
mov ecx, msg_found
mov edx, size_found
int 80h
movzx ax, byte[pos]
mov bl, 10
div bl
mov byte[digit1], al
mov byte[digit0], ah
add byte[digit0], 30h
add byte[digit1], 30h
Introduction to NASM Page 40
mov eax, 4
mov ebx, 1
mov ecx, digit1
mov edx, 1
int 80h
mov eax, 4
mov ebx, 1
mov ecx, digit0
mov edx, 1
int 80h
jmp exit