Advertisement
playerr17

Untitled

Sep 26th, 2023
2,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .global supersum
  2. supersum:
  3.    // eax - pointer to A
  4.    // ebx - pointer to B
  5.    // ecx - pointer to C
  6.    // esi - size A
  7.    // edi - size B
  8.     pushl %ebp
  9.     pushl %edx
  10.     pushl %esi
  11.     pushl %edi
  12.     pushl %ebx
  13.  
  14.  
  15.     cmpl %esi, %edi // edi > esi (size B > size A) go to start
  16.     jg start_loop_first
  17.    //swap A, B
  18.     movl %esi, %ebp
  19.     movl %edi, %esi
  20.     movl %ebp, %edi
  21.  
  22.     movl %eax, %ebp
  23.     movl %ebx, %eax
  24.     movl %ebp, %ebx
  25.  
  26.  
  27.     // use ebp as itearator
  28. start_loop_first:
  29.  
  30.     xor %ebp, %ebp // i = 0
  31. loop_first:
  32.     cmpl %esi, %ebp // i > size A
  33.     jg finish_first_loop
  34.     movb $0, (%ecx, %ebp) // C[i] = 0
  35.     addl $1, %ebp // i++
  36.     jmp loop_first
  37.  
  38. finish_first_loop:
  39.     xor %ebp, %ebp // i = 0
  40. loop_second:
  41.     cmpl %edi, %ebp // i > size B
  42.     jg finish_second_loop
  43.     movb $0, (%ecx, %ebp) // C[i] = 0
  44.     addl $1, %ebp // i++
  45.     jmp loop_second
  46.  
  47. finish_second_loop:
  48.     xor %ebp, %ebp // i = 0
  49.     clc // CF = 0
  50.     pushf
  51. loop_add_a:
  52.     cmpl %esi, %ebp // i >= size A
  53.     jge finish_add_a
  54.     movb (%eax, %ebp), %dl // tmp (dl) = A[i]
  55.     popf
  56.     adcb %dl, (%ecx, %ebp) // C[i] += tmp + CF
  57.     pushf
  58.     addl $1, %ebp // i++
  59.     jmp loop_add_a
  60.  
  61. finish_add_a:
  62.     popf
  63.     adcb $0, (%ecx, %ebp) // C[i] += CF
  64.  
  65.     xor %ebp, %ebp // i = 0
  66.     clc // CF = 0
  67.     pushf
  68. loop_add_b:
  69.     cmpl %edi, %ebp // i >= size B
  70.     jge finish_add_b
  71.     movb (%ebx, %ebp), %dl // tmp (dl) = B[i]
  72.     popf
  73.     adcb %dl, (%ecx, %ebp) // C[i] += tmp + CF
  74.     pushf
  75.     addl $1, %ebp // i++
  76.     jmp loop_add_b
  77.  
  78. finish_add_b:
  79.     popf
  80.     adcb $0, (%ecx, %ebp) // C[i] += CF
  81.  
  82.     // C = A + B
  83.    // now need to find length
  84.     cmpl %edi, %esi // esi > edi
  85.     jg swap_registers
  86.    //edi >= esi
  87.     movl %edi, %ebp
  88.     jmp find_loop
  89.  
  90. swap_registers: // esi > edi
  91.     movl %esi, %ebp
  92. find_loop: // ebp - iterator to last
  93.     cmpl $0, %ebp
  94.     jz finish_program // if ebp == 0
  95.     cmpb $0, (%ecx, %ebp) // C[i] != 0
  96.     jnz finish_program
  97.     subl $1, %ebp // --i
  98.     jmp find_loop
  99.  
  100.  
  101. finish_program:
  102.     cmpb $0, (%ecx, %ebp) // C[i] == 0
  103.     jz zeroo
  104.     jmp finish_last
  105.  
  106. zeroo:
  107.     movl $-1, %ebp
  108.  
  109. finish_last:
  110.     addl $1, %ebp
  111.  
  112.     movl %ebp, %eax
  113.     popl %ebx
  114.     popl %edi
  115.     popl %esi
  116.     popl %edx
  117.     popl %ebp
  118.     ret
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement