Advertisement
ivandre

Untitled

Jun 7th, 2022 (edited)
1,846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unionfind:
  2.     push rbp
  3.     mov rbp, rsp
  4.     push rsp
  5.     push rbx
  6.     push r12
  7.     push r13
  8.     push r14
  9.     push r15
  10.    
  11.    
  12.     #rbx = n, r12= instructionString, r13 = solutionString
  13.     mov rbx, rdi
  14.     mov r12, rsi
  15.     mov r13, rdx
  16.    
  17.     #r14 = array of elements, r15 = array of size of elements
  18.     mov rdi, rbx
  19.     imul rdi, rdi, 8
  20.     call malloc
  21.     mov r14, rax
  22.    
  23.     mov rdi, rbx
  24.     imul rdi, rdi, 8
  25.     call malloc
  26.     mov r15, rax
  27.    
  28.     #initialize array of elements and array of size
  29.     mov rax, rbx
  30.    
  31.     init:
  32.       dec rax
  33.       cmp rax, 0
  34.       jl continue_with_reading
  35.       mov qword ptr [r14 + rax * 8], rax
  36.       mov qword ptr [r15 + rax * 8], 1
  37.       jmp init
  38.      
  39.     #Read instruction
  40.     continue_with_reading:
  41.       xor rdi, rdi
  42.       mov dil, byte ptr [r12]
  43.       cmp dil, 70
  44.       je F
  45.       cmp dil, 85
  46.       je U
  47.       jmp ende
  48.    
  49.     #---------------------------------- Functionality -------------------------------------------
  50.    
  51.     F:
  52.       # Write 'F'
  53.       mov rdi, 70
  54.       mov rsi, r13
  55.       call putint
  56.       mov r13, rax
  57.  
  58.       # Read char number and transform it to int64
  59.       inc r12
  60.       mov rdi, r12
  61.       call getint
  62.       call find
  63.  
  64.       # Write representant (in rax) to solution, 'L' and number of levels in rcx
  65.       push rcx #save it bc it can be changed by putint
  66.  
  67.       mov rdi, rax
  68.       mov rsi, r13
  69.       call putint
  70.       mov r13, rax
  71.  
  72.       mov rdi, 76
  73.       mov rsi, r13
  74.       call putint
  75.       mov r13, rax
  76.  
  77.       pop rcx
  78.       mov rdi, rcx
  79.       mov rsi, r13
  80.       call putint
  81.       mov r13, rax
  82.  
  83.       jmp continue_with_reading
  84.  
  85.    
  86.     U:
  87.       # Write 'U'
  88.       mov rdi, 85
  89.       mov rsi, r13
  90.       call putint
  91.       mov r13, rax
  92.  
  93.       # Read first char number and transform it to int64
  94.       inc r12
  95.       mov rdi, r12
  96.       call getint
  97.       call find
  98.       push rax #Representant 1 of rax
  99.       push rcx #levels first tree
  100.  
  101.       # Read second char number and transform it to int64
  102.       inc r12 #skips '&'
  103.       mov rdi, r12
  104.       call getint
  105.       call find # representant 2 in rax, levels of second tree in rcx
  106.  
  107.       pop rdx #get levels
  108.       add rcx, rdx #total num of levels
  109.  
  110.       pop rdx #Representant 1
  111.       cmp rdx, rax
  112.       je nodes_in_same_tree
  113.       mov rdi, [r15 + rdx * 8] #save num of elements of 1st tree in rdi
  114.       cmp rdi, [r15 + rax * 8] #compare num of elements of both trees. RDX one is first tree
  115.       jge union_left
  116.       jmp union_right
  117.      
  118.     nodes_in_same_tree:
  119.    
  120.       #write group representant
  121.       mov rdi, rdx
  122.       mov rsi, r13
  123.       call putint
  124.       mov r13, rax
  125.  
  126.       #write 'L'
  127.       mov rdi, 76
  128.       mov rsi, r13
  129.       call putint
  130.       mov r13, rax
  131.  
  132.       #Write levels
  133.       mov rdi, rcx
  134.       mov rsi, r13
  135.       call putint
  136.       mov r13, rax     
  137.      
  138.       jmp continue_with_reading
  139.    
  140.     union_left:
  141.       mov qword ptr [r14 + rax * 8], rdx #unify right tree to left tree (change of representant)
  142.       add rdi, qword ptr [r15 + rax * 8] #add number of elements of first tree with num of elements of second tree
  143.       mov qword ptr [r15 + rdx * 8], rdi #save new num of elements in representant of first tree
  144.       mov qword ptr [r15 + rax * 8], 0
  145.  
  146.       #write group representant
  147.       mov rdi, rdx
  148.       mov rsi, r13
  149.       call putint
  150.       mov r13, rax
  151.  
  152.       #write 'L'
  153.       mov rdi, 76
  154.       mov rsi, r13
  155.       call putint
  156.       mov r13, rax
  157.  
  158.       #Write levels
  159.       mov rdi, rcx
  160.       mov rsi, r13
  161.       call putint
  162.       mov r13, rax
  163.  
  164.       jmp continue_with_reading
  165.    
  166.    
  167.     union_right:
  168.       mov qword ptr [r14 + rdx * 8], rax
  169.       add rdi, qword ptr [r15 + rax * 8]
  170.       mov qword ptr [r15 + rdx * 8], 0
  171.       mov qword ptr [r15 + rax * 8], rdi
  172.  
  173.       #write group representant
  174.       mov rdi, rax
  175.       mov rsi, r13
  176.       call putint
  177.       mov r13, rax
  178.  
  179.       #write 'L'
  180.       mov rdi, 76
  181.       mov rsi, r13
  182.       call putint
  183.       mov r13, rax
  184.  
  185.       #Write levels
  186.       mov rdi, rcx
  187.       mov rsi, r13
  188.       call putint
  189.       mov r13, rax
  190.      
  191.       jmp continue_with_reading
  192.    
  193.  
  194.     #params: rax = 64bit integer number from instruction, return value: rax = representant, rcx = levels
  195.     find:
  196.         push rbp
  197.         mov rbp, rsp
  198.         push rsp
  199.        
  200.       xor rcx, rcx
  201.       xor rdx, rdx
  202.       xor rdi, rdi
  203.      
  204.       # num which we search its representant
  205.       mov rdx, rax
  206.  
  207.       loop_array:
  208.       cmp rax, qword ptr [r14 + rax * 8]
  209.       je relink
  210.       inc rcx
  211.       mov rax, qword ptr [r14 + rax * 8]
  212.       jmp loop_array
  213.        
  214.       # rax = representant, rdx = original num, rdi = temporal variable  
  215.       relink:
  216.       cmp qword ptr [r14 + rdx * 8], rax
  217.       je end_find
  218.       mov rdi, qword ptr [r14 + rdx * 8]
  219.       mov qword ptr [r14 + rdx * 8], rax
  220.       mov rdx, rdi
  221.       jmp relink
  222.      
  223.       end_find:
  224.      
  225.       pop rsp
  226.       pop rbp
  227.       ret
  228.    
  229.      
  230.     ende:
  231.       #null byte in solution string
  232.       mov rdi, 0
  233.       mov rsi, r13
  234.       call putint
  235.  
  236.       pop r15
  237.       pop r14
  238.       pop r13
  239.       pop r12
  240.       pop rbx
  241.       pop rsp
  242.       pop rbp
  243.  
  244.       ret
  245.    
  246.  
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement