-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.asm
More file actions
175 lines (136 loc) · 3.01 KB
/
Copy pathast.asm
File metadata and controls
175 lines (136 loc) · 3.01 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
section .data
delim db " ", 0
section .bss
root resd 1
section .text
extern check_atoi
extern print_tree_inorder
extern print_tree_preorder
extern evaluate_tree
extern malloc
extern strtok
extern strlen
extern strdup
global create_tree
global iocla_atoi
iocla_atoi:
enter 0, 0
mov ecx, [esp + 8]
xor eax, eax
xor edx, edx
cmp byte[ecx], '-' ; se verifica daca este numar negativ
jnz number
inc ecx
number:
mov dl, byte[ecx]
; cifrele apartin in intervalul ascii [48, 57]
cmp dl, 48 ; se verifica daca este cifra
jl negative
cmp dl, 57
jg negative
sub dl, 48
imul eax, 10 ; se creeaza numarul in modul
add eax, edx
inc ecx
jmp number
negative:
mov ecx, [esp + 8]
cmp byte[ecx], '-'
jnz out_atoi
neg eax ; trasformare in numar negativ, daca este cazul
out_atoi:
leave
ret
reverse:
enter 0, 0
push eax
push eax
call strlen
add esp, 4
mov ecx, eax
dec ecx
mov edi, ecx
sar ecx, 1 ; lungime = lungime/2
pop eax
xor ebx, ebx
create:
mov dl, byte [eax + edi] ; swap cifrele aflate in pozitiile extreme
mov dh, byte [eax + ebx] ; exemplu swap prima cifra cu ultima cifra
mov byte [eax+ ebx], dl
mov byte [eax + edi], dh
dec ecx
dec edi
inc ebx
cmp ecx, 0
jge create
leave
ret
create_tree:
enter 0, 0
push ebx ; se salveaza toata stiva
push ecx
push edx
push edi
push esi
push eax
call reverse ; reverse forma poloneza prefixata
add esp, 4
mov ebx, [eax] ; se salveaza valoarea din eax
push delim ; punem pe stiva valorile necesare
push eax ; pt a se putea executa strtok
call strtok
add esp, 8
integer:
cmp byte[eax], '+' ; se verifica daca este numar sau operator
jz operator
cmp byte[eax], '-'
jz operator
cmp byte[eax], '*'
jz operator
cmp byte[eax], '/'
jz operator
push eax
call reverse ; reverse numar extras cu strtok
add esp, 4
push eax
call strdup ; copiere numar extras cu strtok
add esp, 4
mov ebx, eax
push 4 ; malloc pentru un integer
call malloc
add esp, 4
mov [eax], ebx ; punem in spatiul alocat numarul extras
; cu strtok
push eax ; salvare nod
extract:
push delim ; se extrag caracterele pana la urmatorul spatiu
push 0
call strtok
add esp, 8
cmp eax, 0 ; se executa pana la finalul sirului de caractere
jz out
jmp integer
operator:
pop esi ; nodul stang
pop edi ; nodul drept
push eax
call strdup
add esp, 4
mov ebx, eax
push 12
call malloc ; malloc pentru 3 noduri (nodul parinte ce
add esp, 4 ; contine operatorul + nodul drept + nodul stang)
mov [eax], ebx ; se pune operator in nodul parinte
mov [eax + 4], esi ; se adauga in nodul stang
mov [eax + 8], edi ; se adauga in nodul drept
push eax ; se salveaza tree-ul
jmp extract
out:
pop eax ; se restaureaza stiva
pop esi
pop edi
pop edx
pop ecx
pop ebx
leave
ret