Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;%include "includes/io.inc"
  2. %include "io.inc"
  3. extern getAST
  4. extern freeAST
  5. extern puts
  6. section .bss
  7.     ; La aceasta adresa, scheletul stocheaza radacina arborelui
  8.     root: resd 1
  9.     left: resd 1
  10.     right: resd 1
  11.     verif1: resd 1
  12.     verif2: resd 1
  13.     negativ: resd 1
  14. section .data
  15.     impartitor dd 10
  16. section .text
  17. global main
  18.  
  19. myAtoi:
  20.    
  21.    
  22.    
  23.     push ebp
  24.     mov ebp, esp
  25.    
  26.     xor eax, eax
  27.     xor ecx, ecx
  28.    
  29.     mov ebx, [ebp + 8]
  30.    
  31.     cmp byte[ebx], 48
  32.     jle esteNegativ
  33.     mov dword[negativ], 0
  34.     jmp estePozitiv
  35. esteNegativ:
  36.     inc ebx
  37.     mov dword[negativ], 1
  38.    
  39. estePozitiv:  
  40.     push ebx
  41.     push ecx
  42.     push edx
  43.    
  44. aux:    
  45.     mov cl, byte[ebx]
  46.     cmp cl, 0
  47.     je done
  48.     ;PRINT_CHAR CL
  49.     ;NEWLINE
  50.     inc ebx
  51.    
  52.     cmp cl, '9'
  53.     ja done
  54.     cmp cl, '0'
  55.     jb done
  56.    
  57.     sub ecx, '0'
  58.    
  59.     imul eax, 10
  60.     add eax, ecx
  61.     jmp aux
  62. done:
  63.     cmp dword[negativ], 1
  64.     je negatie
  65.     jmp pozitiv
  66. negatie:
  67.     mov edx, 0
  68.     sub edx, eax
  69.     mov eax, edx
  70. pozitiv:
  71.     pop edx
  72.     pop ecx
  73.     pop ebx
  74.  
  75.     leave
  76.     ret
  77.    
  78.    
  79.    
  80.  
  81. verifCaract:
  82.    
  83.     push ebp
  84.     mov ebp, esp
  85.    
  86.     mov ebx, [ebp + 8]
  87.    
  88.    
  89.     cmp byte[ebx], '+'
  90.     jne esteInmultire
  91.    
  92.     mov eax, 0
  93.     leave
  94.     ret
  95.  
  96. esteInmultire:
  97.     cmp byte[ebx], '*'
  98.     jne esteImpartire
  99.    
  100.     mov eax, 0
  101.     leave
  102.     ret
  103.    
  104. esteImpartire:
  105.     cmp byte[ebx], '/'
  106.     jne esteMinus
  107.    
  108.     mov eax, 0
  109.     leave
  110.     ret
  111.    
  112. esteMinus:
  113.     mov cl, byte[ebx + 1]
  114.     cmp cl, 0
  115.     jne esteNumar
  116.    
  117.    
  118.     mov cl, [ebx]
  119.     cmp cl, 48
  120.     jge esteNumar
  121.    
  122.     mov eax, 0
  123.     leave
  124.     ret
  125.    
  126. esteNumar:
  127.     mov eax, 1
  128.     leave
  129.     ret
  130.  
  131. potiCalcula:
  132.     xor ecx, ecx
  133.     xor edx, edx
  134.    
  135.     mov ecx, dword[eax + 4]
  136.     mov edx, dword[eax + 8]
  137.    
  138.    
  139.     mov ebx, [eax]
  140.     push ebx
  141.     call verifCaract
  142.     add esp, 4
  143.    
  144.     xor ecx, ecx
  145.     mov ecx, eax
  146.    
  147.     mov ebx, [edx]
  148.     push ebx
  149.     call verifCaract
  150.     add esp, 4
  151.    
  152.     cmp eax, 1
  153.     je esteValida
  154.    
  155.    
  156. esteValida:
  157.     cmp ecx, 1
  158.     je operatie
  159.    
  160.  
  161.    
  162. auxAdunare:
  163.  
  164.     push ebp
  165.     mov ebp, esp
  166.    
  167.     mov eax, [ebp + 8]
  168.     mov ebx, [ebp + 12]
  169.    
  170.     add eax, ebx
  171.    
  172.     leave
  173.     ret
  174. adunare:
  175.  
  176.     push dword[left]
  177.     push dword[right]
  178.     call auxAdunare
  179.     add esp, 8
  180.     jmp outOperatie
  181.    
  182. auxScadere:
  183.     push ebp
  184.     mov ebp, esp
  185.    
  186.     mov ebx, [ebp + 8]
  187.     mov eax, [ebp + 12]    
  188.    
  189.     sub eax, ebx
  190.    
  191.     leave
  192.     ret
  193.    
  194. scadere:
  195.     push dword[left]
  196.     push dword[right]
  197.     call auxScadere
  198.     add esp, 8
  199.     jmp outOperatie  
  200. auxInmultire:
  201.     push ebp
  202.     mov ebp, esp
  203.    
  204.     mov ebx, [ebp + 8]
  205.     mov eax, [ebp + 12]
  206.     push edx
  207.     xor edx, edx
  208.     ;TODO NUMERE NEGATIvE
  209.     ;cmp ebx, 0
  210.     ;jle numereNegative
  211.     ;cmp eax, 0
  212.    ; jle numereNegative
  213.    
  214.     mul ebx
  215.     pop edx
  216.     leave
  217.     ret
  218.    
  219.    
  220. inmultire:
  221.     push dword[left]
  222.     push dword[right]
  223.     call  auxInmultire
  224.     add esp, 8
  225.     jmp outOperatie  
  226. operatie:
  227.        
  228.         push ebp
  229.         mov ebp, esp
  230.        
  231.         mov eax, [ebp + 8]
  232.        
  233.         mov ecx, [eax + 4]
  234.         mov edx, [eax + 8]
  235.        
  236.         mov ebx, [eax]
  237.        
  238.         push eax
  239.         push ebx
  240.        
  241.         mov ebx, [ecx]
  242.         push ebx
  243.         call myAtoi
  244.         add esp, 4
  245.         mov [left], eax
  246.         xor ebx, ebx
  247.         mov ebx, [edx]
  248.         push ebx
  249.         call myAtoi
  250.         add esp, 4
  251.         mov [right], eax
  252.        
  253.         pop ebx
  254.         cmp byte[ebx], '+'
  255.         je adunare
  256.         cmp byte[ebx], '-'
  257.         je scadere
  258.         cmp byte[ebx], '*'
  259.         je inmultire
  260.        
  261. outOperatie:
  262.         mov ecx, eax
  263.         pop eax
  264.         push ecx
  265.         push dword[eax]
  266.         call intToChar
  267.         add esp, 8
  268.         leave
  269.         ret
  270.  
  271. esteFrunza:
  272.     cmp dword[eax + 8], 0
  273.     je label3    
  274.    
  275. label3:
  276.     leave
  277.     ret
  278.  
  279.  
  280. parcurgere:
  281.    
  282.     push ebp
  283.     mov ebp, esp
  284.    
  285.     mov eax, [ebp + 8]
  286.    
  287.    
  288.     mov ebx, [eax]
  289.     ;PRINT_CHAR [ebx]
  290.    
  291.     ;VERIFIC DACA COPIII SUNT NUMERE
  292.     push eax
  293.     push eax
  294.    
  295.     push ebx
  296.     call verifCaract
  297.     add esp, 4
  298.    
  299.     cmp eax, 0
  300.     jne aici
  301.    
  302.     pop eax
  303.     mov ebx, [eax + 4]
  304.     mov edx, [eax + 8]
  305.    
  306.     push ebx
  307.     mov ebx, [ebx]
  308.     push ebx
  309.     call verifCaract
  310.     add esp, 4
  311.     mov [verif1], eax
  312.     pop ebx
  313.    
  314.     push edx
  315.     mov ebx, [edx]
  316.     push ebx
  317.     call verifCaract
  318.     add esp, 4
  319.     mov [verif2], eax
  320.     pop edx
  321.    
  322.     cmp dword[verif1], 1
  323.     jne aici
  324.     cmp dword[verif2], 1
  325.     jne aici
  326.    
  327.     pop eax
  328.    
  329.     push eax
  330.     call operatie
  331.     add esp, 4
  332.     jmp aici2
  333.    
  334. aici:  
  335.     pop eax
  336. aici2:
  337.     cmp dword[eax + 4], 0
  338.     je esteFrunza
  339.    
  340.    
  341.     push eax
  342.     mov eax, [eax + 4]
  343.     push eax
  344.     call parcurgere
  345.     add esp, 4
  346.     pop eax
  347.    
  348.    
  349.     push eax
  350.     mov eax, [eax + 8]
  351.     push eax
  352.     call parcurgere
  353.     add esp, 4
  354.     pop eax
  355.    
  356.     leave
  357.     ret
  358.    
  359. nrCifre:    
  360.     xor eax, eax
  361.     push ebp
  362.     mov ebp, esp
  363.     mov eax, [ebp + 8]
  364.     mov edx, 0
  365.    
  366.     xor ecx, ecx
  367.     mov ebx, 10
  368.    
  369.    
  370.    
  371.    ; PRINT_DEC 4, eax
  372. loop:
  373.     inc ecx
  374.     div ebx
  375.     mov edx, 0
  376.     cmp eax, 0
  377.     jg loop
  378.    
  379.     mov eax, ecx
  380.     leave
  381.     ret
  382.    
  383.    
  384. intToChar:
  385.    
  386.     push ebp
  387.     mov ebp, esp
  388.     push eax
  389.  
  390.     mov ebx, [ebp +8]
  391.     mov eax, [ebp + 12]
  392.    
  393.    
  394.     cmp eax, 0
  395.     jl nrNegativ
  396.     mov dword[negativ], 0
  397.     jmp nrPozitiv
  398. nrNegativ:
  399.     mov dword[negativ], 1
  400.     push edx
  401.     mov edx, 0
  402.     sub edx, eax
  403.     mov eax, edx
  404.     pop edx
  405. nrPozitiv:
  406.     push eax
  407.     push ebx
  408.     push eax
  409.     call nrCifre
  410.     add esp, 4
  411.     ;IN ECX SE VA AFLA NUMARUL DE CIFRE
  412.     mov ecx, eax
  413.     pop ebx
  414.     pop eax
  415.     cmp dword[negativ], 1
  416.     jne convert
  417.     mov dl, '-'
  418.     mov byte[ebx], dl
  419.    
  420. convert:
  421.     xor edx, edx
  422.     push ecx
  423.     mov ecx, [impartitor]
  424.     div ecx
  425.  
  426.     pop ecx
  427.     add dl, '0'
  428.     cmp dword[negativ], 1
  429.     je nuScad_1
  430.     mov byte[ebx + ecx - 1], dl
  431.     jmp scad_1
  432. nuScad_1:
  433.     mov byte[ebx + ecx], dl
  434. scad_1:
  435.     loop convert
  436.     pop eax
  437.    
  438.     leave
  439.     ret
  440.        
  441.    
  442.  
  443. main:
  444.     mov ebp, esp; for correct debugging
  445.     ; NU MODIFICATI
  446.     push ebp
  447.     mov ebp, esp
  448.    
  449.     ; Se citeste arborele si se scrie la adresa indicata mai sus
  450.     call getAST
  451.     mov [root], eax
  452.    
  453.     ; Implementati rezolvarea aici:
  454.     ;IN EAX PUN NODE*
  455.     mov eax, [root]
  456.    
  457.     ;IN EBX PUN CHAR*
  458.     push eax
  459.     call parcurgere
  460.     add esp, 4
  461.    
  462.     mov ebx, [eax]
  463.     cmp byte[ebx], 48
  464.     jge iesire
  465.    
  466.     push eax
  467.     call operatie
  468.     add esp, 4
  469.    
  470.  
  471.    
  472.     ;push eax
  473.     ;call parcurgere
  474.     ;add esp, 4
  475.     ;IN ECX PUN COPILUL STANG
  476.     ;IN EDX PUN COPILUL DREPT
  477. iesire:
  478.  
  479.     push ebx
  480.     call puts
  481.     add esp, 4
  482.     ; NU MODIFICATI
  483.     ; Se elibereaza memoria alocata pentru arbore
  484.     push dword [root]
  485.     call freeAST
  486.    
  487.     xor eax, eax
  488.     leave
  489.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement