Commits

trikitrok committed a588ff1 Draft

p_add_sub, p_term_add and p_term_add_c mostly working

Comments (0)

Files changed (3)

+syntax: glob
+*~
+*.o
+*.txt
+
+syntax: regexp
+.*\#.*\#$
+^[^.]+$
+section .data          
+
+section .text    
+
+; Se hacen visibles las rutinas de ensamblador que se deben ver desde el programa en C
+global p_term_add_c, p_add_sub
+
+p_add_sub:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+  push rbx
+  push r9
+
+  ;  r_coef(rdi),   r_exp(rsi),    s_coef(rdx),    s_exp(rcx),    suma/resta(r8)
+
+  mov rbx, 0              ; índice
+
+  add_sub_loop:
+    ; Call to p_term add 
+    sub rsp,8
+    push rsi                    ; We push the address of the exponents of the polynomial to the stack
+    push rdi                    ; We push the address of the coeficients of the polynomial to the stack
+    mov eax, dword[rcx + rbx*4] ; We push the exponent of the new term we want to insert
+    push rax    
+    
+    mov eax, dword[rdx + rbx*4] ; We push the coefficient of the term we want to insert
+    cmp r8, 0
+    JE push_coef
+      neg rax
+    push_coef:
+      push rax                    
+                        
+    call p_term_add
+    add rsp,32
+    pop rax
+    
+    cmp rax,-1
+    JE add_sub_error
+    
+    add ebx,1
+    cmp rbx, 20                             
+    jl add_sub_loop   ; si (rbx < 20*4) se hace otra iteración
+        
+  ;Store success operation flag
+  mov rax, 0
+  JMP add_sub_end
+  
+  add_sub_error:
+  ; Delete polynomial
+  mov rax, rdi
+  mov rbx, rsi
+  call p_delete
+  ;Store error operation flag
+  mov rax, -1
+
+  add_sub_end:
+
+  pop r9
+  pop rbx
+  mov rsp, rbp
+  pop rbp
+  
+  ret
+
+p_term_add_c:
+  push rbp                ; Store stack initial state
+  mov  rbp, rsp   
+
+  sub rsp,8
+           
+  push rcx                ; We push the address of the exponents of the polynomial to the stack
+  push rdx                ; We push the address of the coeficients of the polynomial to the stack
+  push rsi                ; We push the exponent of the new term we want to insert
+  push rdi                ; We push the coefficient of the term we want to insert
+  call p_term_add
+  add rsp,32
+  pop rax
+        
+  mov rsp, rbp            ; Restore stack initial state
+  pop rbp
+
+  ret
+
+p_term_add:
+  push rbp                ; Store stack initial state
+  mov  rbp, rsp   
+  push rcx
+  push rbx
+  push rax
+  push rdi
+  push rsi
+           
+  mov rbx, [rbp+40]       ; We move the exponents address (exps) from the stack to RBX
+  mov ecx, dword[rbp+24]  ; We move the exponent (exp_term) from the stack to RCX
+  
+  ;*******************
+  ; Call p_exist_term
+  ;*******************
+  sub rsp, 8              ; We make space for the result by subtracting 8 bytes to the stack pointer
+  push rbx                ; We push the exponents address to the stack
+  push rcx                ; We push the exponent we wish to find            
+  call p_exist_term       ; Now we call p_exist_term
+  add rsp, 16             ; We place the stack pointer in the position were the result is
+  pop rsi                 ; RDI contains the position of the term if it exists -1 if it does not
+  ;********************
+
+  ; if it's -1 the term does no exist
+  cmp rsi,-1
+  JE insert_new_term_call
+
+  ; Sum coefficients
+  mov rdi, [rbp+32]         ; We move the coefficients address (coefs) from the stack to RDI
+  mov eax, dword[rbp+16]    ; We sum the coefficients
+  add eax, dword[rdi+rsi*4]
+  
+  cmp rax,0               ; If sum == 0, we delete the term
+  JE delete_term_call
+  
+  mov dword[rdi+rsi*4], eax ; If not we update it
+  JMP success
+  
+  delete_term_call:
+  ;*******************
+  ; Call p_delete_term 
+  ;*******************
+  push rbx                ; We push the address of the exponents of the polynomial to the stack
+  push rdi                ; We push the address of the coeficients of the polynomial to the stack
+  push rsi                ; We push the position of the term we want to delete
+  call p_delete_term      ; Now we call p_delete_term
+  add rsp,24
+  JMP success
+  ;*******************
+  
+  
+  insert_new_term_call:
+  
+  ;*******************
+  ; Call p_count_term
+  ;*******************
+  sub rsp, 8              ; We make space for the result by subtracting 8 bytes to the stack pointer
+  push rbx                ; We push the exponents to the stack
+  call p_count_terms      ; Now we call p_count_terms
+  add rsp, 8              ; We place the stack pointer to the 
+                          ; position were the result is
+  pop rax                 ; And move it to rax so that it's
+  ;*******************
+  
+  ; if there are already 20 elements in the vector we can't insert one more
+  cmp rax,20
+  JE error
+
+  ;*******************
+  ; Call p_find_position 
+  ;*******************
+  sub rsp, 8              
+  push rbx                ; We push the exponents of the polynomial to the stack
+  mov eax, dword[rbp+24]  ; We push the exponent of the new term we want to find
+  push rax
+  call p_find_position    ; Now we call p_find_position
+  add rsp, 16              
+  pop rsi                 ; And move its result to rsi
+  ;*******************
+  
+  ;*******************
+  ; Call p_insert_term
+  ;******************* 
+  push rbx                ; We push the address of the exponents of the polynomial to the stack
+  mov rdi, [rbp+32]       ; We push the address of the coeficients of the polynomial to the stack
+  push rdi                
+  mov eax, dword[rbp+24]  ; We push the exponent of the new term we want to insert
+  push rax                
+  mov eax, dword[rbp+16]  ; We push the coefficient of the term we want to insert
+  push rax                
+  push rsi                ; We push the position were we want to insert the term
+  call p_insert_term      ; Now we call p_insert_term
+  add rsp, 40
+  ;*******************      
+  JMP success
+          
+  error:     
+  ;Store error operation flag
+  mov qword[rbp+48], -1
+  JMP end_add_term
+  
+  success:
+  ;Store success operation flag
+  mov qword[rbp+48], 0
+  
+  end_add_term:
+
+  pop rsi
+  pop rdi
+  pop rax
+  pop rbx      
+  pop rcx
+
+  mov rsp, rbp            ; Restore stack initial state
+  pop rbp
+
+  ret
+
+p_insert_term:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+        
+  push rax                   
+  push rbx
+  push rcx  
+  push rdx
+  push rdi
+  
+  mov rbx, [rbp+48]       ; We move the exponents address (exps) from the stack to RBX
+  mov rcx, [rbp+40]       ; We move the coefficients address (coefs) from the stack to RCX
+  mov edx, dword[rbp+16]  ; We move the position to insert the term to  RDX
+
+  ; exp_term is at [rbp+32]  
+  ; coef_term is at [rbp+24]
+  
+  mov edi, 19             ; RDI will be the index
+
+  insert_term_loop:
+    cmp rdi, rdx          ; if (index == position) the insert the new term
+    JE insert_new_term
+  
+    ; exps[i] = exps[i-1]
+    mov eax, dword[rbx + (rdi-1)*4]
+    mov [rbx + rdi*4], eax
+    
+    ; coefs[i] = coefs[i-1]
+    mov eax, dword[rcx + (rdi-1)*4]
+    mov [rcx + rdi*4], eax
+    
+    sub edi,1              
+    JMP insert_term_loop   
+  
+  insert_new_term:
+    mov eax, dword[rbp+32]          ; insert exp_term
+    mov dword[rbx + rdi*4], eax
+    mov eax, dword[rbp+24]          ; insert coef_term
+    mov dword[rcx + rdi*4], eax     
+
+  pop rdi
+  pop rdx
+  pop rcx
+  pop rbx
+  pop rax    
+  mov rsp, rbp
+  pop rbp
+  
+  ret
+  
+p_find_position:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+  push rbx
+  push rcx  
+  push rdx
+  
+  mov rbx, [rbp+24]       ; We move the exponents (exps) from the stack to RBX
+  mov edx, dword[rbp+16]  ; We move the exponent (exp) we'd like to find to RDX
+  mov rcx, 0              ; Index
+
+  find_position_loop:
+    cmp dword[rbx + rcx*4], edx ; if exps[i] < exp, then pos = ecx
+    JL found
+    
+    add ecx,1
+    JMP find_position_loop   
+
+  found: 
+    ; Se guarda el resultado en la pila
+    mov qword[rbp+32], rcx
+  
+  pop rdx                 ; Restore stack initial state
+  pop rcx
+  pop rbx
+  mov rsp, rbp
+  pop rbp
+  
+  ret
+  
+p_delete_term:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+        
+  push rax                   
+  push rbx
+  push rcx  
+  push rdx
+  
+  mov rbx, [rbp+32]       ; We move the exponents (exps) from the stack to RBX
+  mov rcx, [rbp+24]       ; We move the coefficients (coefs) from the stack to RCX
+  mov edx, dword[rbp+16]  ; We move the position (pos) of the term we'd like to delete to RDX
+
+  cmp edx, 19             ; if pos == the last one, we don't need to move anything 
+                          ; and we just have to delete it
+  JE delete_last_term
+
+  add edx,1               ; RDX will be our index
+
+  delete_term_loop:
+    ; exps[i-1] = exps[i]
+    mov eax, dword[rbx + rdx*4]
+    mov [rbx + (rdx-1)*4], eax
+    
+    ; coefs[i-1] = coefs[i]
+    mov eax, dword[rcx + rdx*4]
+    mov [rcx + (rdx-1)*4], eax
+    
+    add edx,1              ; if (rdx < 20), we iterate again
+    cmp edx, 20                             
+    jl delete_term_loop   
+  
+  delete_last_term:        ; We delete the last element
+    mov edx,19
+    mov dword[rbx + rdx*4],-1
+    mov dword[rcx + rdx*4],0
+  
+  pop rdx                  ; Restore stack initial state
+  pop rcx
+  pop rbx
+  pop rax
+  mov rsp, rbp
+  pop rbp
+  
+  ret    
+  
+p_exist_term:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+        
+  push rbx
+  push rcx  
+  push rdx
+  
+  mov rbx, [rbp+24]       ; We move the exponents (exps) from the stack to RBX
+  mov edx, dword[rbp+16]  ; We move the exponent (exp) we'd like to find to RDX
+  mov rcx, 0              ; Index
+
+  exist_term_loop:
+    cmp dword[rbx + rcx*4], edx ; if exps[i] == exp, eax = ecx
+    JNE next_term_1
+    JMP done                    ; and we are done
+    
+    next_term_1:
+    
+    add ecx,1                   
+    cmp ecx, 20                             
+    jl exist_term_loop   
+  
+  ; Si llega aquí hay un error
+  mov rcx, -1
+  
+  done: 
+  ; Se guarda la posicion en la pila
+  mov qword[rbp+32], rcx
+  
+  pop rdx                 ; Restore stack initial state
+  pop rcx
+  pop rbx
+  mov rsp, rbp
+  pop rbp
+  
+  ret    
+
+p_count_terms:
+  push rbp
+  mov  rbp, rsp           ; Store stack initial state
+        
+  push rax                   
+  push rbx
+  push rcx  
+  
+  mov rax, 0              ; Initialize rax  
+  mov rbx, [rbp+16]       ; We move the exponents from the stack to RBX
+  mov rcx, 0              ; índice
+
+  count_terms_loop:
+    cmp dword[rbx + rcx*4], -1
+    JE next_term
+    
+    add eax,1              ; si el exponente del término != -1 se cuenta el término
+    
+    next_term:
+    
+    add ecx,1              ; si no (rdx < 20*4) se hace otra iteración
+    cmp ecx, 20                             
+    jl count_terms_loop   
+  
+  ; Se guarda el resultado en la pila  
+  mov qword[rbp+24], rax
+  
+  pop rcx                 ; Restore stack initial state
+  pop rbx
+  pop rax
+  mov rsp, rbp
+  pop rbp
+  
+  ret    
+  
+; Esborrar els termes d'un vector de 20 posicions. El coeficient a 0 i l'exponenet a -1.
+; Paràmetres d'entrada: v_coef(rax), v_exp(rbx)
+; Paràmetre de sortida: cap
+p_delete:
+  push rbp
+  mov  rbp, rsp
+  push rax                   ;guardem a la pila els registres que modifiquem
+  push rbx
+  push rsi
+    
+  mov  rsi, 19               ;comencem per la darrera posició.
+  
+  p_delete_loop:
+    mov dword[rax+rsi*4], 0
+    mov dword[rbx+rsi*4],-1
+    dec  esi
+    jge  p_delete_loop
+ 
+  p_delete_end:
+  pop rsi                    ;restaurar l'estat dels registres.
+  pop rbx
+  pop rax
+  mov rsp, rbp
+  pop rbp
+  ret
+
+#include <stdio.h>
+#include <assert.h>
+
+// Funciones en ensamblador
+extern int p_term_add_c(int coef_term, int exp_term, int* coefs, int* exps);
+extern int p_add_sub(int *r_coef, int * r_exp, int* s_coef, int* s_exp, int suma_resta);
+
+void printTerm(int coef, int exp)
+{
+    if(coef > 0)
+        printf("+%dx^%d ", coef, exp); 
+    else
+        printf("%dx^%d ", coef, exp); 
+}
+
+void p_print(int v_coef[], int v_exp[])
+{  
+    int i;
+    
+    for(i=0; i<20; i++)
+    {
+        printTerm(v_coef[i], v_exp[i]);
+    }
+    
+    printf("\n");   
+}
+
+int equalPolynomial(int *coef1, int * exp1, int *coef2, int *exp2)
+{
+    int i;
+    for(i=0; i<20; i++)
+    {
+        if(( coef1[i] != coef2[i] ) || ( exp1[i] != exp2[i] ))
+        {
+            return 0;
+        }
+    }
+    return 1;
+}
+
+int main(void) 
+{
+
+    int exp_term;
+    int coef_term;
+    int res; 
+
+    printf("Testing p_term_add..\n"); 
+    printf("Test 1. Add term with result not zero: ");
+    int p_coef[20]={-16,+10,+18,+2,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+    int p_exp [20]={+15,+9,+5,+1,+0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+  
+    exp_term = 5;
+    coef_term = 2;
+    res = p_term_add_c(coef_term, exp_term, p_coef, p_exp);
+  
+    int res_coef[20]={-16,+10,+20,+2,-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+    int res_exp [20]={+15,+9,+5,+1,+0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+
+    //printf("\n");
+    //p_print(p_coef, p_exp);    
+    //p_print(res_coef, res_exp);
+    //printf("\n");
+    
+    assert(equalPolynomial(res_coef, res_exp, p_coef, p_exp));
+    assert(res==0);
+    printf("Ok\n");
+
+    //----------------------------------------------------------------------------------
+
+    printf("Test 2. Add term with zero result, so delete element: ");
+
+    int q_coef[20]={-16, +10, +18, +2, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+    int q_exp [20]={+15, +9,  +5,  +1, +0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+  
+    exp_term = 1;
+    coef_term = -2;
+    res = p_term_add_c(coef_term, exp_term, q_coef, q_exp);
+  
+    int q_res_coef[20]={-16, +10, +18, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+    int q_res_exp [20]={+15, +9,  +5, +0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1};
+
+    //printf("\n");
+    //p_print(q_coef, q_exp);    
+    //p_print(q_res_coef, q_res_exp);
+    //printf("\n");
+    
+    assert(equalPolynomial(q_res_coef, q_res_exp, q_coef, q_exp));
+    assert(res==0);
+    printf("Ok\n");
+
+    //----------------------------------------------------------------------------------
+
+    printf("Test 3. Term does not exist, so try to insert term but polynomial is full: ");    
+    
+    int n_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 3};
+    int n_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
+  
+    exp_term = 20;
+    coef_term = -2;
+    res = p_term_add_c(coef_term, exp_term, n_coef, n_exp);
+    
+    assert(res==-1);
+    printf("Ok\n");
+    //----------------------------------------------------------------------------------
+
+    printf("Test 4. Term does not exist, insert it: ");    
+    
+    int m_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int m_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+  
+    exp_term = 20;
+    coef_term = -2;
+    res = p_term_add_c(coef_term, exp_term, m_coef, m_exp);
+
+    int m_res_coef[20]={-2,  -16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7};
+    int m_res_exp [20]={20,  19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
+
+    //printf("\n");
+    //p_print(m_coef, m_exp);    
+    //p_print(m_res_coef, m_res_exp);
+    //printf("\n");
+
+    assert(equalPolynomial(m_coef, m_exp, m_res_coef, m_res_exp));   
+    assert(res==0);
+    printf("Ok\n");
+    //----------------------------------------------------------------------------------
+
+    printf("\n");
+    
+    printf("Testing p_add_sub..\n");
+    printf("Test 1. Adding two equal polynomials (no error): ");    
+    
+    int r1_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int r1_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+
+    int r2_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int r2_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+  
+    res = p_add_sub(r1_coef, r1_exp, r2_coef, r2_exp, 0);
+
+    int sum_res_coef[20]={-32,+20,+36,+4, 6, 2, 4, 8, 10, 12, 14, 16, 18, 20, 22, 24, 6, 8, 14, 0};
+    int sum_res_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+
+    //printf("\n");
+    //p_print(r1_coef, r1_exp);    
+    //p_print(sum_res_coef, sum_res_exp);
+    //printf("\n");
+
+    assert(res==0);
+    assert(equalPolynomial(r1_coef, r1_exp, sum_res_coef, sum_res_exp));   
+    printf("Ok\n");
+
+    //----------------------------------------------------------------------------------
+    printf("Test 2. Subtracting two equal polynomials (no error): ");    
+    
+    int s1_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int s1_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+
+    int s2_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int s2_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+  
+    res = p_add_sub(s1_coef, s1_exp, s2_coef, s2_exp, -1);
+
+    int sum_res2_coef[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+    int sum_res2_exp [20]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+
+    //printf("\n");
+    //p_print(s1_coef, s1_exp);    
+    //p_print(sum_res2_coef, sum_res2_exp);
+    //printf("\n");
+
+    assert(res==0);
+    assert(equalPolynomial(s1_coef, s1_exp, sum_res2_coef, sum_res2_exp));   
+    printf("Ok\n");
+    
+    //----------------------------------------------------------------------------------
+    printf("Test 3. Subtracting two equal polynomials that are already full (no error): ");    
+    
+    int t1_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 4};
+    int t1_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
+
+    int t2_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 4};
+    int t2_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
+  
+    res = p_add_sub(t1_coef, t1_exp, t2_coef, t2_exp, -1);
+
+    int sum_res3_coef[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+    int sum_res3_exp [20]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+
+    //printf("\n");
+    //p_print(t1_coef, t1_exp);    
+    //p_print(sum_res3_coef, sum_res3_exp);
+    //printf("\n");
+
+    assert(res==0);
+    assert(equalPolynomial(t1_coef, t1_exp, sum_res3_coef, sum_res3_exp));   
+    printf("Ok\n");
+    
+    //----------------------------------------------------------------------------------
+    
+    printf("Test 4. Subtracting two different polynomials (no error): ");    
+    
+    int u1_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int u1_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+
+    int u2_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, 0};
+    int u2_exp [20]={20,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,-1};
+  
+    res = p_add_sub(u1_coef, u1_exp, u2_coef, u2_exp, -1);
+
+    int sum_res4_coef[20]={16,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+    int sum_res4_exp [20]={20,19,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+
+    //printf("\n");
+    //p_print(u1_coef, u1_exp);    
+    //p_print(sum_res4_coef, sum_res4_exp);
+    //printf("\n");
+
+    assert(res==0);
+    assert(equalPolynomial(u1_coef, u1_exp, sum_res4_coef, sum_res4_exp));   
+
+    printf("Ok\n");
+
+    //----------------------------------------------------------------------------------
+    
+    printf("Test 5. Subtracting two different polynomials but result with more than 20 terms (error, so delete result): ");    
+    
+    int v1_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, -10};
+    int v1_exp [20]={19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
+
+    int v2_coef[20]={-16,+10,+18,+2,3, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 3, 4, 7, -10};
+    int v2_exp [20]={20,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0};
+  
+    res = p_add_sub(v1_coef, v1_exp, v2_coef, v2_exp, -1);
+
+    int sum_res5_coef[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+    int sum_res5_exp [20]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
+
+    //printf("\n");
+    //p_print(v1_coef, v1_exp);    
+    //p_print(sum_res5_coef, sum_res5_exp);
+    //printf("\n");
+
+    assert(res==-1);
+    assert(equalPolynomial(v1_coef, v1_exp, sum_res5_coef, sum_res5_exp));   
+
+    printf("Ok\n");
+
+    //----------------------------------------------------------------------------------
+ 
+    
+    printf("All tests passed!!\n");
+      
+    return 0;  
+}