-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargest.asm
More file actions
168 lines (149 loc) · 3.16 KB
/
Largest.asm
File metadata and controls
168 lines (149 loc) · 3.16 KB
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
section .data
mesg1: db 'NUMBER OF ELEMENTS: '
mesg1l: equ $-mesg1
mesg2: db 'INPUT ARRAY: '
mesg2l: equ $-mesg2
mesglarge: db 0Ah, 'LARGEST: '
mesglargel: equ $-mesglarge
mesgsmall: db 'SMALLEST: '
mesgsmalll: equ $-mesgsmall
section .bss
array: resb 100
n: resd 1
l: resw 1
s: resw 1
;REQUIRED BY PRINT, READ
counter: resb 1
num: resw 1
buf: resb 1
section .text
global _start
;FUNCTIONS
read:
pusha
mov word[num], 0
readloop_001:
mov eax, 3
mov ebx, 0
mov ecx, buf
mov edx, 1
int 80h
cmp byte[buf], 10
je readfinish_001
mov ax, word[num]
mov bx,10
mul bx
mov bl, byte[buf]
sub bl, 30h
mov bh, 0
add ax, bx
mov word[num], ax
jmp readloop_001
readfinish_001:
popa
ret
print:
mov byte[counter], 0 ;to keep count of no of digits
pusha ;make new stack
ext_digit: ;To extract digit by digit from right end of the number
cmp word[num], 0
je printno
add byte[counter], 1
mov ax, word[num]
mov dx, 0 ;Remainder in dx
mov bx, 10
div bx
;Now remainder in dx, push into stack
push dx
mov word[num], ax
jmp ext_digit
printno:
cmp byte[counter], 0
je print_end
sub byte[counter], 1
pop dx
mov byte[buf], dl
add byte[buf], 30h
;Now print digit by digit
mov eax,4
mov ebx,1
mov ecx, buf
mov edx, 1
int 80h
jmp printno
print_end:
popa
ret
;***************************************************************************
_start:
;CODE TO READ ARRAY SIZE
mov eax, 4
mov ebx, 1
mov ecx, mesg1
mov edx, mesg1l
int 80h
;Read n
call read
mov eax, 0
mov ax, [num]
mov dword[n], eax
;Read n over
;CODE TO DISPLAY INPUT ARRAY MESSAGE
mov eax, 4
mov ebx, 1
mov ecx, mesg2
mov edx, mesg2l
int 80h
;CODE TO READ n NUMBERS
mov eax, array
mov ebx, 0 ;ACT AS COUNTER
readloop:
cmp ebx, dword[n]
je readloopfinish
;GET A NUMBER
call read ;read saves register to stack and pops it on end
;SAVE THE NUMBER IN ARRAY
mov cx, word[num]
mov word[eax+2*ebx], cx
inc ebx
jmp readloop
readloopfinish:
;CODE TO PERFORM SEARCH
mov ebx, 1 ;ACT AS COUNTER
mov eax, array
mov edx, [array]
mov word[l], dx
mov word[s], dx
searchloop:
cmp ebx, dword[n]
je endsearchloop
mov ecx, 0
mov cx, word[eax+2*ebx]
cmp cx, word[l]
jna notlarger
mov word[l], cx
notlarger:
cmp cx, word[s]
jnb notsmaller
mov word[s], cx
notsmaller:
inc ebx
jmp searchloop
endsearchloop:
mov eax, 4
mov ebx, 1
mov ecx, mesgsmall
mov edx, mesgsmalll
int 80h
mov ax, word[s]
mov word[num], ax
call print
mov eax, 4
mov ebx, 1
mov ecx, mesglarge
mov edx, mesglargel
int 80h
mov ax, word[l]
mov word[num], ax
call print
exit: