-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.asm
More file actions
214 lines (174 loc) · 4.67 KB
/
Copy patharray.asm
File metadata and controls
214 lines (174 loc) · 4.67 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
section .data
; Array-related keywords and messages
kw_array db 'सूची', 0 ; Array keyword
kw_push db 'योजय', 0 ; Push operation
kw_pop db 'निकाल', 0 ; Pop operation
kw_get db 'प्राप्त', 0 ; Get element
kw_set db 'स्थापय', 0 ; Set element
kw_len db 'मात्रा', 0 ; Length operation
array_error_index db 'त्रुटि: Invalid array index', 0xA
array_error_len equ $ - array_error_index
array_error_full db 'त्रुटि: Array is full', 0xA
array_error_full_len equ $ - array_error_full
array_error_empty db 'त्रुटि: Array is empty', 0xA
array_error_empty_len equ $ - array_error_empty
; Array configuration
max_arrays equ 16 ; Maximum number of arrays
array_size equ 100 ; Size of each array
section .bss
; Array storage
arrays resb max_arrays * array_size * 4 ; Each element is 4 bytes
array_lengths resb max_arrays * 4 ; Length of each array
array_count dd 0 ; Number of arrays
section .text
global init_array
global array_push
global array_pop
global array_get
global array_set
global array_length
; Initialize a new array
init_array:
push ebp
mov ebp, esp
; Check if we can create more arrays
mov eax, [array_count]
cmp eax, max_arrays
jge array_init_error
; Initialize array length to 0
mov dword [array_lengths + eax * 4], 0
; Increment array count and return array ID
inc dword [array_count]
dec eax
mov esp, ebp
pop ebp
ret
; Push value to array
; Parameters: array_id (ebp+8), value (ebp+12)
array_push:
push ebp
mov ebp, esp
; Get array length
mov eax, [ebp + 8] ; array_id
mov ecx, [array_lengths + eax * 4]
; Check if array is full
cmp ecx, array_size
jge array_full_error
; Calculate element position
imul eax, array_size * 4
lea edi, [arrays + eax + ecx * 4]
; Store value
mov eax, [ebp + 12] ; value
mov [edi], eax
; Increment length
mov eax, [ebp + 8] ; array_id
inc dword [array_lengths + eax * 4]
mov esp, ebp
pop ebp
ret 8
; Pop value from array
; Parameter: array_id (ebp+8)
array_pop:
push ebp
mov ebp, esp
; Get array length
mov eax, [ebp + 8] ; array_id
mov ecx, [array_lengths + eax * 4]
; Check if array is empty
test ecx, ecx
jz array_empty_error
; Calculate element position
dec ecx
imul eax, array_size * 4
lea edi, [arrays + eax + ecx * 4]
; Get value
mov eax, [edi]
; Decrement length
mov edi, [ebp + 8] ; array_id
dec dword [array_lengths + edi * 4]
mov esp, ebp
pop ebp
ret 4
; Get array element
; Parameters: array_id (ebp+8), index (ebp+12)
array_get:
push ebp
mov ebp, esp
; Check index bounds
mov eax, [ebp + 8] ; array_id
mov ecx, [ebp + 12] ; index
cmp ecx, [array_lengths + eax * 4]
jge array_index_error
; Calculate element position
imul eax, array_size * 4
lea edi, [arrays + eax + ecx * 4]
; Return value
mov eax, [edi]
mov esp, ebp
pop ebp
ret 8
; Set array element
; Parameters: array_id (ebp+8), index (ebp+12), value (ebp+16)
array_set:
push ebp
mov ebp, esp
; Check index bounds
mov eax, [ebp + 8] ; array_id
mov ecx, [ebp + 12] ; index
cmp ecx, [array_lengths + eax * 4]
jge array_index_error
; Calculate element position
imul eax, array_size * 4
lea edi, [arrays + eax + ecx * 4]
; Store value
mov eax, [ebp + 16] ; value
mov [edi], eax
mov esp, ebp
pop ebp
ret 12
; Get array length
; Parameter: array_id (ebp+8)
array_length:
push ebp
mov ebp, esp
mov eax, [ebp + 8] ; array_id
mov eax, [array_lengths + eax * 4]
mov esp, ebp
pop ebp
ret 4
; Error handlers
array_init_error:
mov eax, -1
mov esp, ebp
pop ebp
ret
array_full_error:
mov eax, 4
mov ebx, 1
mov ecx, array_error_full
mov edx, array_error_full_len
int 0x80
mov eax, -1
mov esp, ebp
pop ebp
ret 8
array_empty_error:
mov eax, 4
mov ebx, 1
mov ecx, array_error_empty
mov edx, array_error_empty_len
int 0x80
mov eax, -1
mov esp, ebp
pop ebp
ret 4
array_index_error:
mov eax, 4
mov ebx, 1
mov ecx, array_error_index
mov edx, array_error_len
int 0x80
mov eax, -1
mov esp, ebp
pop ebp
ret