Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %include "io.inc"
- ;;;;;;;;;;;;
- ;struct int96:
- ; int digits[30] @ _ + 0x0
- ; int sign @ _ + 0x78; +1 or -1
- ; int size @ _ + 0x7C
- ;;;;;;;;;;;;
- %define SIZE 32
- section .data
- sign_offset equ 120
- size_offset equ 124
- section .bss
- a resd SIZE
- b resd SIZE
- c resd SIZE
- res resd SIZE
- section .text
- ;;;;;;;;;;;;;
- int_to_int96: ; (int96* output, int a)->void
- push ebp
- mov ebp, esp
- sub esp, 24
- mov dword[ebp - 4], edi
- mov dword[ebp - 8], esi
- mov dword[ebp - 12], ebx
- ;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;
- cld
- mov edi, dword[ebp + 8]
- ;lea edi, [eax]
- mov eax, 0
- mov ecx, 10
- rep stosd
- ;;;;;;;;;;;;;;;;;;;;;;;
- mov esi, dword[ebp + 8]
- mov eax, dword[ebp + 12]
- ;;;;;;;;;;;;;;;;;;;;;;;;
- mov dword[esi + sign_offset], 0
- test eax, eax
- sets cl
- mov byte[esi + sign_offset], cl
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;;
- mov edi, 0
- mov ecx, 10
- .digits_loop:
- cdq
- idiv ecx
- test edx, edx
- jns .ns
- neg edx
- .ns:
- ;mov ebx, edi
- ;neg ebx
- mov dword[esi + 4 * edi], edx
- inc edi
- cmp eax, 0
- jnz .digits_loop
- .end_digits_loop:
- mov dword[esi + size_offset], edi
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;
- mov ebx, dword[ebp - 12]
- mov esi, dword[ebp - 8]
- mov edi, dword[ebp - 4]
- mov esp, ebp
- pop ebp
- ret
- ;;;;;;;;;;;;;
- ;;;;;;;;;;;;;
- print_int96: ;(int96* )->void
- push ebp
- mov ebp, esp
- ;;;;;;;;;;;;
- ; PRINT_CHAR '@'
- ; PRINT_DEC 4, [ebp + 8]
- ; PRINT_CHAR ':'
- mov edx, dword[ebp + 8]; num
- mov eax, dword[edx + sign_offset]; sign
- test eax, eax
- jz .no_sign
- PRINT_CHAR '-'
- .no_sign:
- mov ecx, dword[edx + size_offset]
- dec ecx
- ;PRINT_DEC 4, [edx + 124]
- .print_loop:
- cmp ecx, 0
- jl .end_print_loop
- PRINT_DEC 4, [edx + 4 * ecx]
- dec ecx
- jmp .print_loop
- .end_print_loop:
- ;;;;;;;;;;;;
- mov esp, ebp
- pop ebp
- ret
- ;;;;;;;;;;;;;
- ;;;;;;;;;;;;;
- multiply_96_int: ;(int96 *res, int *num, int digit)
- push ebp
- mov ebp, esp
- sub esp, 12
- mov dword[ebp - 4], ebx
- mov dword[ebp - 8], esi
- mov dword[ebp - 12], edi
- ;;;;;;;;;;;;
- ;
- ; lea edi, [ebp - 56]
- ;;;;;;;;;;;;
- mov edi, dword[ebp + 16]
- test edi, edi
- jnz .fair
- mov edi, dword[ebp + 8]
- push 0
- push edi
- call int_to_int96
- jmp .end
- .fair:
- mov edi, dword[ebp + 12]
- mov esi, 0; carry
- mov ecx, 0
- ; mov ebx, dword[ebp + 8]
- ; push ebx
- ; call print_int96
- .mul_loop:
- cmp ecx, dword[edi + size_offset]
- jge .end_mul_loop
- mov eax, dword[edi + 4 * ecx]
- ;PRINT_DEC 4, eax
- ;PRINT_CHAR ' '
- imul dword[ebp + 16]
- add eax, esi
- ;PRINT_DEC 4, eax
- ;PRINT_CHAR ' '
- cdq
- mov ebx, 10
- idiv ebx
- mov esi, eax
- mov ebx, dword [ebp + 8]
- mov dword[ebx + 4 * ecx], edx
- ; PRINT_DEC 4, [ebx + 4 * ecx]
- inc ecx
- jmp .mul_loop
- .end_mul_loop:
- mov ebx, dword [ebp + 8]
- ; NEWLINE
- ; PRINT_DEC 4, [ebp + 12]
- ; NEWLINE
- ; PRINT_DEC 4, [ebp + 8]
- mov eax, dword[edi + sign_offset]
- mov dword[ebx + sign_offset], eax
- mov dword[ebx + size_offset], ecx
- mov dword[ebx + 4 * ecx], esi
- test esi, esi
- jz .no_new_digit
- inc dword[ebx + size_offset]
- .no_new_digit:
- ;PRINT_DEC 4, ebx
- ; NEWLINE
- ; PRINT_DEC 4, [ebx]
- ; PRINT_DEC 4, [ebx + 4]
- ; NEWLINE
- ; push ebx
- ; call print_int96
- ; NEWLINE
- ; PRINT_DEC 4, ecx
- ; NEWLINE
- .end: ;;;;;;;;;;;;
- mov edi, dword[ebp - 12]
- mov esi, dword[ebp - 8]
- mov ebx, dword[ebp - 4]
- mov esp, ebp
- pop ebp
- ret
- ;;;;;;;;;;;;;
- ;;;;;;;;;;;;;
- fastcall_max:
- ;PRINT_DEC 4, ecx
- ;PRINT_DEC 4, edx
- NEWLINE
- cmp ecx, edx
- jle .ge
- mov eax, ecx
- jmp .end
- .ge:
- mov eax, edx
- .end:
- ret
- ;;;;;;;;;;;;;
- ;;;;;;;;;;;;;
- add_shift: ; (int96 *dist, int *shifted, int shift)
- push ebp
- mov ebp, esp
- sub esp, 12
- mov dword[ebp - 4], ebx
- mov dword[ebp - 8], esi
- mov dword[ebp - 12], edi
- ;;;;;;;;;;;;
- ;;;;;;;;;;;;getting maxsize of two int96s
- mov ecx, dword[ebp + 8]
- mov ecx, dword[ecx + size_offset]
- mov edx, dword[ebp + 12]
- mov edx, dword[edx + size_offset]
- add edx, dword[ebp + 16]
- call fastcall_max
- mov ecx, dword[ebp + 12]
- mov dword[ecx + size_offset], eax
- ;PRINT_DEC 4, eax
- ;NEWLINE
- ;;;;;;;;;;;;
- mov esi, 0 ;carry
- mov ecx, 0
- ;mov ebx, dword[ebp + 16]
- mov ebx, dword[ebp + 12]
- ; mov edi, dword[ebp + 8]
- .add_loop:
- cmp ecx, dword[ebx + size_offset]
- jge .end_add_loop
- ;;;;getting index of current digit of shifted
- mov eax, dword[ebp + 8]
- mov edi, dword[ebp + 16]
- add edi, ecx
- ;;;;
- mov eax, dword[eax + 4 * edi]
- ;PRINT_DEC 4, eax
- ;NEWLINE
- add eax, dword[ebx + 4 * ecx]
- add eax, esi
- ;PRINT_DEC 4, eax
- ;NEWLINE
- cdq
- mov edi, 10
- idiv edi
- mov esi, eax
- mov eax, dword[ebp + 8]
- mov edi, dword[ebp + 16]
- add edi, ecx
- mov dword[eax + 4 * edi], edx
- PRINT_DEC 4, [eax + 4 * ecx]
- NEWLINE
- inc ecx
- jmp .add_loop
- .end_add_loop:
- mov eax, dword[ebp + 8]
- mov dword[eax + size_offset], ecx
- mov ecx, dword[ebp + 16]
- ; add dword[eax + size_offset], ecx
- test esi, esi
- jz .no_new_digit
- mov dword[eax + 4 * ecx], esi
- inc dword[eax + size_offset]
- .no_new_digit:
- ;;;;;;;;;;;;
- mov edi, dword[ebp - 12]
- mov esi, dword[ebp - 8]
- mov ebx, dword[ebp - 4]
- mov esp, ebp
- pop ebp
- ret
- ;;;;;;;;;;;;;
- ;;;;;;;;;;;;;
- multiply_96_96: ; (int96 *res, int96 *a, int96* b)
- push ebp
- mov ebp, esp
- ;PRINT_DEC 4, esp
- sub esp, 72
- mov dword[ebp - 4], ebx
- mov dword[ebp - 8], esi
- mov dword[ebp - 12], edi
- ;;;;;;;;;;;;
- ;int96 *tmp @ ebp - 24
- ;
- ;;;;;;;;;;;;
- ;;;tmp = 0
- lea ebx, [ebp - 24]
- mov dword[esp], ebx
- mov dword[esp + 4], 0
- ;call int_to_int96
- mov edi, dword[ebp + 16]
- ;;;;;;;;;;;;
- mov ebx, 0
- .mul_loop:
- cmp ecx, dword[edi + size_offset]
- jge .end_mul_loop
- mov eax, dword[edi + 4 * ecx]
- mov [esp + 8], eax
- mov eax, dword[ebp + 12]
- mov [esp + 4], eax
- lea eax, [ebp - 24]
- mov [esp], eax
- ;PRINT_DEC 4, eax
- call multiply_96_int
- mov eax, dword[ebp + 8]
- mov [esp], eax
- lea eax, [ebp - 24]
- mov [esp + 4], eax
- mov [esp + 8], ebx
- call add_shift
- inc ebx
- jmp .mul_loop
- .end_mul_loop:
- ;;;;;;;;;;;;
- mov edi, dword[ebp - 12]
- mov esi, dword[ebp - 8]
- mov ebx, dword[ebp - 4]
- mov esp, ebp
- B:
- ;PRINT_DEC 4, esp
- pop ebp
- ret
- ;;;;;;;;;;;;;
- global CMAIN
- ;;;;;;;;;;;;
- CMAIN:
- push ebp
- mov ebp, esp
- ;;;;;;;;;;;;
- GET_DEC 4, eax
- push eax
- push a
- call int_to_int96
- GET_DEC 4, eax
- push eax
- push b
- call int_to_int96
- push a
- push b
- push c
- call multiply_96_96
- call print_int96
- ;;;;;;;;;;;;
- mov esp, ebp
- pop ebp
- xor eax, eax
- ret
- ;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement