Advertisement
Guest User

iocla tema1 90/90

a guest
Nov 20th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. %include "includes/io.inc"
  2.  
  3. extern getAST
  4. extern freeAST
  5.  
  6. section .data
  7.  
  8. string1 db "-3216969", 0
  9. string2 db "694269830" , 0
  10. CONST_10 dd 10
  11. numar db "0x%x", 0
  12. number db "2222", 0
  13. mesaj_bun db "da boss, e numar", 0
  14.  
  15.  
  16. section .bss
  17. ; La aceasta adresa, scheletul stocheaza radacina arborelui
  18. root: resd 1
  19.  
  20. ; var atoi
  21. ;inmultire: resd 1
  22. suma: resd 1
  23. negativ: resb 1
  24.  
  25. ; alte var
  26. string: resb 10
  27. pointer: resd 1
  28. adresa: resd 1
  29. charp: resd 1
  30.  
  31. section .text
  32.  
  33. global main
  34.  
  35. main:
  36. mov ebp, esp; for correct debugging
  37. ; NU MODIFICATI - ok
  38. push ebp
  39. mov ebp, esp
  40.  
  41. ; Se citeste arborele si se scrie la adresa indicata mai sus
  42. call getAST
  43. mov [root], eax
  44.  
  45. ; Implementati rezolvarea aici:
  46.  
  47. ; push string2
  48. ; call atoi
  49. ; add esp, 4
  50. ; mov [pointer], eax
  51. ; PRINT_DEC 4, pointer
  52.  
  53. ; NEWLINE
  54. ; NEWLINE
  55.  
  56. ; PRINT_STRING "PLM"
  57.  
  58. ; xor eax, eax
  59. ; mov ebx, [root]
  60. ; mov eax, [ebx]
  61. ; PRINT_STRING [eax]
  62. ; mov ecx, [ebx + 4]
  63. ; mov eax, [ecx]
  64. ; PRINT_STRING [eax]
  65. ; mov eax, [ebx + 8]
  66. ; mov eax, [eax]
  67. ; PRINT_STRING [eax]
  68.  
  69. ; NEWLINE
  70.  
  71.  
  72. ;mov eax, [root]
  73. ;add eax, 4
  74. ;mov eax, [eax]
  75. ;mov eax, [eax]
  76.  
  77. ; PRINT_STRING [eax]
  78.  
  79.  
  80. mov eax, [root]
  81. ;mov ebx, [eax + 4] ; I dereferentiere
  82. ;mov eax, [ebx] ;II dereferentiere
  83. push eax
  84. call recursiv
  85. add esp, 4
  86.  
  87.  
  88. ;NEWLINE
  89.  
  90.  
  91. PRINT_DEC 4, eax
  92.  
  93. ;NEWLINE
  94.  
  95. ;PRINT_STRING "PLM"
  96.  
  97. ; push number
  98. ; call parse_data
  99. ; add esp, 4
  100.  
  101. ; PRINT_UDEC 4, eax
  102.  
  103.  
  104.  
  105. ; NU MODIFICATI - ok
  106. ; Se elibereaza memoria alocata pentru arbore
  107. push dword [root]
  108. call freeAST
  109.  
  110. xor eax, eax
  111. leave
  112. ret
  113.  
  114. recursiv:
  115. push ebp
  116. mov ebp, esp
  117.  
  118. ; preferabil sa returnez numarul intr-un registru: de exemplu eax
  119. mov eax, [ebp + 8] ; eax are adresa celulei
  120. ; mov ebx,
  121. mov ebx, [eax] ; ebx are valoare lui data, adica pointerul la string
  122. push ebx ; o salvez pe siva
  123.  
  124. ;PRINT_STRING [ebx]
  125.  
  126. push ebx
  127. call parse_data
  128. add esp, 4
  129.  
  130. push eax ; salvez daca e numar sau daca nue numar ce operatie e
  131.  
  132. cmp eax, 0
  133. jne is_operation
  134.  
  135. ;mov ebx, [ebp + 8] ; accesez din nou valoare lui data, adica pointerul la string
  136. ;mov ebx, [ebx]
  137.  
  138. pop eax
  139. pop ebx
  140. push eax
  141.  
  142.  
  143. push ebx
  144. call atoi
  145. add esp, 8 ; double pop si pentru push eax de mai de sus
  146.  
  147. jmp eof
  148.  
  149. is_operation:
  150.  
  151. mov ebx, [ebp + 8]
  152. add ebx, 4 ; copilul de pe stanga
  153. mov ebx, [ebx] ; abia aici e copilul stanga
  154.  
  155. push ebx
  156. call recursiv
  157. add esp, 4
  158.  
  159. push eax ; aici se salveaza pe stiva operandul 1 venit de pe stanga
  160.  
  161. mov ebx, [ebp + 8]
  162. add ebx, 8 ; copilul de pe dreapta
  163. mov ebx, [ebx] ; abia aici e copilul dreapta
  164.  
  165.  
  166. push ebx
  167. call recursiv
  168. add esp, 4
  169.  
  170. push eax ; aici se salveaza pe stiva operandul 2 venit de pe dreapta
  171.  
  172. pop ecx ; op2
  173. pop ebx ; op1
  174. pop eax ; op {+, -, *, /}
  175. cmp eax, 1
  176. je adunare
  177. cmp eax, 2
  178. je scadere
  179. cmp eax, 3
  180. je inmultire
  181. cmp eax, 4
  182. je impartire
  183.  
  184. adunare:
  185. add ebx, ecx
  186. mov eax, ebx
  187. jmp eof
  188.  
  189. scadere:
  190. sub ebx, ecx
  191. mov eax, ebx
  192. jmp eof
  193.  
  194. inmultire:
  195. mov eax, ebx
  196. imul ecx
  197. jmp eof
  198.  
  199. impartire:
  200. test ebx, 0x80000000
  201. jnz label1
  202. mov edx, 0
  203. jmp skip_imp
  204.  
  205. label1:
  206. mov edx, 0xFFFFFFFF
  207.  
  208. skip_imp:
  209. mov eax, ebx
  210. idiv ecx
  211.  
  212.  
  213.  
  214.  
  215.  
  216. eof:
  217. leave
  218. ret
  219.  
  220. ;~~~~~~~end of recursiv~~~~~~~~~
  221.  
  222. parse_data:
  223. ; preimeste adresa stringului de vazut daca e sau nu numar
  224. ; returneaza in eax 0 daca e numar, 1 adunare, 2 scadere, 3 inmultire, 4 impartire
  225. push ebp
  226. mov ebp, esp
  227.  
  228. mov eax, [ebp + 8]
  229. xor ebx, ebx
  230. mov bl, byte [eax]
  231. cmp bl, 48 ; daca e mai amre sau egal ca 0 atunci claros e numar
  232. jge is_numar
  233. cmp bl, 43 ; daca e +
  234. je is_adunare
  235. cmp bl, 42 ; daca e *
  236. je is_inmultire
  237. cmp bl, 47 ; daca e /
  238. je is_impartire
  239. ; daca nu e numar care incepe cu cifra, nu e +, nu e *, nu e / atunci e -
  240. ; daca dupa minus e null terminator atunci e scadere, altfel e numar
  241. mov bl, byte [eax + 1]
  242. cmp bl, 0 ; compara cu null terminator
  243. jne is_numar
  244.  
  245. mov eax, 2
  246. jmp end_parse_data
  247.  
  248. is_impartire:
  249. mov eax, 4
  250. jmp end_parse_data
  251.  
  252. is_inmultire:
  253. mov eax, 3
  254. jmp end_parse_data
  255.  
  256. is_adunare:
  257. mov eax, 1
  258. jmp end_parse_data
  259.  
  260. is_numar:
  261. mov eax, 0
  262.  
  263. end_parse_data:
  264. leave
  265. ret
  266. ;~~~~~~~~~ end of parse data!~~~~~~~~~
  267.  
  268. atoi:
  269. ; primeste pointer la string
  270. ; returneaza rezultatul in eax
  271. push ebp
  272. mov ebp, esp
  273.  
  274. mov edx, [ebp + 8]
  275. mov ecx, 1
  276. mov ebx, 0
  277. mov eax, 0
  278.  
  279. mov byte [negativ], 0x00 ; nagitv = 0
  280. ; mov byte [suma], 0
  281.  
  282. mov bl, byte [edx]
  283. cmp bl, 45 ; verifica daca primul char este '-'
  284. jne skip_no_negative
  285. mov byte [negativ], 1 ; negativ = 1
  286. jmp while
  287.  
  288. skip_no_negative:
  289. mov al, bl
  290. movsx eax, al
  291. sub al, 0x30
  292.  
  293. while:
  294. mov bl, byte [edx + ecx] ; muta char cu char in bl
  295. cmp bl, 0 ; cmp cu '\0'
  296. je endWhile ; sfarsitul stringulu
  297. sub bl, 48 ; conversie din char in numar
  298. movsx ebx, bl
  299.  
  300. ;mov eax, [suma]
  301. push edx
  302. mul dword[CONST_10]
  303. pop edx
  304. add eax, ebx
  305. ; mov [suma], eax
  306.  
  307. inc ecx
  308. jmp while
  309.  
  310.  
  311.  
  312. endWhile:
  313. ;mov eax, [suma]
  314. cmp byte [negativ], 0 ; cmp negative cu 0
  315. je endFunction ; daca negative == 0 iese din functie
  316. neg eax ; else eax *= -1;
  317. ; mov [suma], eax
  318.  
  319. endFunction:
  320.  
  321. leave
  322. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement