Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %include "io.inc"
  2.  
  3.  
  4. ;;;;;;;;;;;;
  5. ;struct int96:
  6. ;   int digits[30] @ _ + 0x0
  7. ;   int sign @ _ + 0x78; +1 or -1
  8. ;   int size @ _ + 0x7C
  9. ;;;;;;;;;;;;
  10. %define SIZE 32
  11. section .data
  12. sign_offset equ 120
  13. size_offset equ 124
  14.  
  15.  
  16. section .bss
  17. a resd SIZE
  18. b resd SIZE
  19. c resd SIZE
  20. res resd SIZE
  21.  
  22.  
  23. section .text
  24.  
  25. ;;;;;;;;;;;;;
  26. int_to_int96: ; (int96* output, int a)->void
  27.     push ebp
  28.     mov ebp, esp
  29.     sub esp, 24
  30.     mov dword[ebp - 4], edi
  31.     mov dword[ebp - 8], esi
  32.     mov dword[ebp - 12], ebx
  33.     ;;;;;;;;
  34.  
  35.       ;;;;;;;;;;;;;;;;;;;;;;;
  36.       cld
  37.       mov edi, dword[ebp + 8]
  38.    
  39.       ;lea edi, [eax]
  40.       mov eax, 0
  41.       mov ecx, 10
  42.       rep stosd
  43.       ;;;;;;;;;;;;;;;;;;;;;;;
  44.      
  45.       mov esi, dword[ebp + 8]
  46.       mov eax, dword[ebp + 12]
  47.  
  48.       ;;;;;;;;;;;;;;;;;;;;;;;;
  49.       mov dword[esi + sign_offset], 0
  50.       test eax, eax
  51.       sets cl
  52.       mov byte[esi + sign_offset], cl
  53.       ;;;;;;;;;;;;;;;;;;;;;;;;
  54.      
  55.       ;;;;;;;;;;;;;;;;;;;;;;;;
  56.       mov edi, 0
  57.       mov ecx, 10
  58.       .digits_loop:  
  59.         cdq
  60.         idiv ecx
  61.         test edx, edx
  62.         jns .ns
  63.             neg edx
  64.         .ns:
  65.  
  66.         ;mov ebx, edi
  67.         ;neg ebx
  68.         mov dword[esi + 4 * edi], edx
  69.  
  70.         inc edi
  71.         cmp eax, 0
  72.         jnz .digits_loop
  73.       .end_digits_loop:
  74.       mov dword[esi + size_offset], edi
  75.       ;;;;;;;;;;;;;;;;;;;;;;;;
  76.     ;;;;;;;;
  77.     mov ebx, dword[ebp - 12]
  78.     mov esi, dword[ebp - 8]
  79.     mov edi, dword[ebp - 4]
  80.     mov esp, ebp
  81.     pop ebp
  82.     ret
  83.  
  84. ;;;;;;;;;;;;;
  85.  
  86. ;;;;;;;;;;;;;
  87.  
  88. print_int96: ;(int96* )->void
  89.     push ebp
  90.     mov ebp, esp
  91.     ;;;;;;;;;;;;
  92. ;    PRINT_CHAR '@'
  93. ;    PRINT_DEC 4, [ebp + 8]
  94. ;    PRINT_CHAR ':'
  95.         mov edx, dword[ebp + 8]; num
  96.         mov eax, dword[edx + sign_offset]; sign
  97.         test eax, eax
  98.         jz .no_sign
  99.             PRINT_CHAR '-'
  100.         .no_sign:
  101.  
  102.         mov ecx, dword[edx + size_offset]
  103.         dec ecx
  104.         ;PRINT_DEC 4, [edx + 124]
  105.         .print_loop:
  106.             cmp ecx, 0
  107.             jl .end_print_loop
  108.            
  109.             PRINT_DEC 4, [edx + 4 * ecx]
  110.  
  111.             dec ecx
  112.             jmp .print_loop
  113.         .end_print_loop:
  114.  
  115.  
  116.  
  117.     ;;;;;;;;;;;;
  118.     mov esp, ebp
  119.     pop ebp
  120.     ret
  121. ;;;;;;;;;;;;;
  122.  
  123. ;;;;;;;;;;;;;
  124.  
  125. multiply_96_int: ;(int96 *res, int *num, int digit)
  126.    
  127.     push ebp
  128.     mov ebp, esp
  129.     sub esp, 12
  130.     mov dword[ebp - 4], ebx
  131.     mov dword[ebp - 8], esi
  132.     mov dword[ebp - 12], edi
  133.     ;;;;;;;;;;;;
  134.     ;
  135.     ; lea edi, [ebp - 56]
  136.     ;;;;;;;;;;;;
  137.    
  138.     mov edi, dword[ebp + 16]
  139.     test edi, edi
  140.  
  141.     jnz .fair
  142.         mov edi, dword[ebp + 8]
  143.         push 0
  144.         push edi
  145.         call int_to_int96
  146.         jmp .end
  147.     .fair:
  148.        
  149.     mov edi, dword[ebp + 12]
  150.     mov esi, 0; carry
  151.     mov ecx, 0
  152.        
  153.       ;  mov ebx, dword[ebp + 8]
  154.       ;  push ebx
  155.       ;  call print_int96
  156.     .mul_loop:
  157.         cmp ecx, dword[edi + size_offset]
  158.         jge .end_mul_loop
  159.            
  160.         mov eax, dword[edi + 4 * ecx]
  161.         ;PRINT_DEC 4, eax
  162.         ;PRINT_CHAR ' '
  163.         imul dword[ebp + 16]
  164.         add eax, esi
  165.         ;PRINT_DEC 4, eax
  166.         ;PRINT_CHAR ' '
  167.         cdq
  168.         mov ebx, 10
  169.         idiv ebx
  170.         mov esi, eax
  171.  
  172.         mov ebx, dword [ebp + 8]
  173.         mov dword[ebx + 4 * ecx], edx
  174.      ;   PRINT_DEC 4, [ebx + 4 * ecx]    
  175.         inc ecx
  176.         jmp .mul_loop
  177.     .end_mul_loop:
  178.         mov ebx, dword [ebp + 8]
  179.     ;    NEWLINE
  180.     ;    PRINT_DEC 4, [ebp + 12]
  181.     ;    NEWLINE
  182.     ;    PRINT_DEC 4, [ebp + 8]
  183.         mov eax, dword[edi + sign_offset]
  184.         mov dword[ebx + sign_offset], eax
  185.         mov dword[ebx + size_offset], ecx
  186.         mov dword[ebx + 4 * ecx], esi
  187.         test esi, esi
  188.         jz .no_new_digit
  189.             inc dword[ebx + size_offset]
  190.         .no_new_digit:
  191.         ;PRINT_DEC 4, ebx
  192.      ;   NEWLINE
  193.      ;   PRINT_DEC 4, [ebx]
  194.      ;   PRINT_DEC 4, [ebx + 4]
  195.      ;   NEWLINE
  196.      ;   push ebx
  197.      ;   call print_int96
  198.        ; NEWLINE
  199.        ; PRINT_DEC 4, ecx
  200.        ; NEWLINE
  201. .end:    ;;;;;;;;;;;;
  202.     mov edi, dword[ebp - 12]
  203.     mov esi, dword[ebp - 8]
  204.     mov ebx, dword[ebp - 4]
  205.  
  206.     mov esp, ebp
  207.     pop ebp
  208.     ret
  209. ;;;;;;;;;;;;;
  210.  
  211. ;;;;;;;;;;;;;
  212.     fastcall_max:
  213.         ;PRINT_DEC 4, ecx
  214.         ;PRINT_DEC 4, edx
  215.         NEWLINE
  216.         cmp ecx, edx
  217.         jle .ge
  218.             mov eax, ecx
  219.             jmp .end
  220.         .ge:
  221.             mov eax, edx
  222.         .end:
  223.     ret
  224. ;;;;;;;;;;;;;
  225.  
  226. ;;;;;;;;;;;;;
  227. add_shift: ; (int96 *dist, int *shifted, int shift)
  228.    
  229.     push ebp
  230.     mov ebp, esp
  231.     sub esp, 12
  232.     mov dword[ebp - 4], ebx
  233.     mov dword[ebp - 8], esi
  234.     mov dword[ebp - 12], edi
  235.     ;;;;;;;;;;;;
  236.    
  237.     ;;;;;;;;;;;;getting maxsize of two int96s
  238.     mov ecx, dword[ebp + 8]
  239.     mov ecx, dword[ecx + size_offset]
  240.     mov edx, dword[ebp + 12]
  241.     mov edx, dword[edx + size_offset]
  242.     add edx, dword[ebp + 16]
  243.    
  244.     call fastcall_max
  245.     mov ecx, dword[ebp + 12]
  246.     mov dword[ecx + size_offset], eax
  247.     ;PRINT_DEC 4, eax
  248.     ;NEWLINE
  249.     ;;;;;;;;;;;;
  250.        
  251.     mov esi, 0 ;carry
  252.    
  253.     mov ecx, 0
  254.     ;mov ebx, dword[ebp + 16]
  255.        mov ebx, dword[ebp + 12]
  256.       ; mov edi, dword[ebp + 8]
  257.     .add_loop:
  258.        cmp ecx, dword[ebx + size_offset]
  259.        jge .end_add_loop
  260.            
  261.            ;;;;getting index of current digit of shifted
  262.            mov eax, dword[ebp + 8]
  263.            mov edi, dword[ebp + 16]
  264.            add edi, ecx
  265.            ;;;;
  266.            mov eax, dword[eax + 4 * edi]
  267.            ;PRINT_DEC 4, eax
  268.            ;NEWLINE
  269.            add eax, dword[ebx + 4 * ecx]
  270.            add eax, esi
  271.            
  272.            ;PRINT_DEC 4, eax
  273.            ;NEWLINE
  274.  
  275.            cdq
  276.            mov edi, 10
  277.            idiv edi
  278.            mov esi, eax
  279.  
  280.            mov eax, dword[ebp + 8]
  281.            mov edi, dword[ebp + 16]
  282.            add edi, ecx
  283.  
  284.            mov dword[eax + 4 * edi], edx
  285.            PRINT_DEC 4, [eax + 4 * ecx]
  286.            NEWLINE
  287.  
  288.             inc ecx
  289.        jmp .add_loop
  290.     .end_add_loop:
  291.    
  292.     mov eax, dword[ebp + 8]
  293.     mov dword[eax + size_offset], ecx
  294.     mov ecx, dword[ebp + 16]
  295.    ; add dword[eax + size_offset], ecx
  296.     test esi, esi
  297.     jz .no_new_digit
  298.         mov dword[eax + 4 * ecx], esi
  299.         inc dword[eax + size_offset]
  300.     .no_new_digit:
  301.    
  302.  
  303.  
  304.     ;;;;;;;;;;;;
  305.     mov edi, dword[ebp - 12]
  306.     mov esi, dword[ebp - 8]
  307.     mov ebx, dword[ebp - 4]
  308.     mov esp, ebp
  309.     pop ebp
  310.     ret
  311.  
  312. ;;;;;;;;;;;;;
  313.  
  314. ;;;;;;;;;;;;;
  315.  
  316. multiply_96_96: ; (int96 *res, int96 *a, int96* b)
  317.  
  318.     push ebp
  319.     mov ebp, esp
  320.     ;PRINT_DEC 4, esp
  321.     sub esp, 72
  322.     mov dword[ebp - 4], ebx
  323.     mov dword[ebp - 8], esi
  324.     mov dword[ebp - 12], edi  
  325.    
  326.     ;;;;;;;;;;;;
  327.     ;int96 *tmp @ ebp - 24
  328.     ;
  329.     ;;;;;;;;;;;;
  330.  
  331.     ;;;tmp = 0
  332.     lea ebx, [ebp - 24]
  333.     mov dword[esp], ebx
  334.     mov dword[esp + 4], 0
  335.     ;call int_to_int96
  336.     mov edi, dword[ebp + 16]
  337.    
  338.     ;;;;;;;;;;;;
  339.  
  340.     mov ebx, 0
  341.  
  342.     .mul_loop:
  343.         cmp ecx, dword[edi + size_offset]
  344.         jge .end_mul_loop
  345.        
  346.         mov eax, dword[edi + 4 * ecx]
  347.         mov [esp + 8], eax
  348.         mov eax, dword[ebp + 12]
  349.         mov [esp + 4], eax
  350.         lea eax, [ebp - 24]
  351.         mov [esp], eax  
  352.         ;PRINT_DEC 4, eax
  353.         call multiply_96_int
  354.        
  355.        
  356.         mov eax, dword[ebp + 8]
  357.         mov [esp], eax
  358.         lea eax, [ebp - 24]
  359.         mov [esp + 4], eax
  360.         mov [esp + 8], ebx
  361.         call add_shift
  362.  
  363.         inc ebx
  364.         jmp .mul_loop
  365.     .end_mul_loop:  
  366.    
  367.  
  368.     ;;;;;;;;;;;;
  369.     mov edi, dword[ebp - 12]
  370.     mov esi, dword[ebp - 8]
  371.     mov ebx, dword[ebp - 4]
  372.     mov esp, ebp
  373.     B:
  374.     ;PRINT_DEC 4, esp
  375.     pop ebp
  376.     ret
  377. ;;;;;;;;;;;;;
  378.  
  379.  
  380. global CMAIN
  381. ;;;;;;;;;;;;
  382. CMAIN:
  383.     push ebp
  384.     mov ebp, esp
  385.    
  386.     ;;;;;;;;;;;;
  387.     GET_DEC 4, eax
  388.     push eax
  389.     push a
  390.     call int_to_int96
  391.    
  392.     GET_DEC 4, eax
  393.     push eax
  394.     push b
  395.     call int_to_int96
  396.    
  397.     push a
  398.     push b
  399.     push c
  400.     call multiply_96_96
  401.     call print_int96
  402.     ;;;;;;;;;;;;
  403.     mov esp, ebp
  404.     pop ebp
  405.     xor eax, eax
  406.     ret
  407.  
  408. ;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement