Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global main
- extern printf
- extern scanf
- extern puts
- extern exit
- section .text
- main:
- mov ebp, esp ; ebp --> entry point
- sub esp, 16 ; rezerwacja 16 bajtow na 4 zmienne potrzebne w programie (inty rozmiar 4 bajtow)
- mov dword [ebp-12], 0 ; int wybor = 0
- mov dword [ebp-8], 0 ; int liczba1 = 0
- mov dword [ebp-4], 0 ; int liczba2 = 0
- mov dword [ebp], 0 ; int wynik = 0
- push calc_msg
- call printf
- add esp, 4
- push choice_msg
- call printf
- add esp, 4
- push choices
- call puts
- add esp, 4
- push next_choice_msg
- call printf
- add esp, 4
- lea eax, [ebp-12] ; oblicz offset [ebp-12] i wloz do eax
- push eax ; (&wybor)
- push format_int ; ("%i")
- call scanf ; scanf("%i", &wybor)
- add esp, 8
- push write_first_number_msg ; 'Write first number: '
- call printf
- add esp, 4
- lea eax, [ebp-8] ; oblicz offset [ebp-8] i wloz do eax
- push eax ; (&liczba1)
- push format_int ; ("%i")
- call scanf ; scanf("%i", &liczba1)
- add esp, 8
- push write_second_number_msg ; 'Write second number: '
- call printf
- add esp, 4
- lea eax, [ebp-4] ; oblicz offset [ebp-4] i wloz do eax
- push eax ; (&liczba1)
- push format_int ; ("%i")
- call scanf ; scanf("%i", &liczba2)
- add esp, 8
- ; zaczyna sie cos jak switch case w C
- mov eax, [ebp-12] ; eax --> *(wybor)
- cmp eax, 2
- jz subtraction
- cmp eax, 2
- jg cmp_with_3_label
- cmp eax, 1
- jz addition
- jmp default_label
- cmp_with_3_label:
- cmp eax, 3
- jz multiplication
- jmp cmp_with_4_label
- cmp_with_4_label:
- cmp eax, 4
- jz divide
- jmp default_label
- subtraction:
- mov eax, [ebp-8] ; eax --> *(liczba1)
- sub eax, [ebp-4] ; eax --> *(liczba1) - *(liczba2)
- mov [ebp], eax ; *(wynik) = *(liczba1) - *(liczba2)
- jmp write_result
- addition:
- mov eax, [ebp-8]
- add eax, [ebp-4] ; eax --> *(liczba1) + *(liczba2)
- mov [ebp], eax
- jmp write_result
- multiplication:
- mov eax, [ebp-8]
- imul eax ; eax --> *(liczba1) * *(liczba2) (instrukcja imul, bo dzialamy na signed int)
- mov [ebp], eax
- jmp write_result
- divide:
- mov eax, [ebp-8]
- idiv eax ; eax --> *(liczba1) / *(liczba2) (instrukcja idiv, bo dzialamy na signed int)
- mov [ebp], eax
- jmp write_result
- default_label:
- push bad_option
- call puts ; puts, bo na koniec \n co powoduje wyczyszczenie bufora
- add esp, 4
- call exit
- write_result:
- push dword [ebp]
- push write_result_msg
- call printf
- add esp, 8
- call exit
- section .data
- calc_msg db "~~~~~~~~CALCULATOR~~~~~~~~", 0xa, 0
- choice_msg db "Choose one of the operation", 0xa, 0
- choices db "1. Addition", 0xa
- db "2. Subtraction", 0xa
- db "3. Multiplication", 0xa
- db "4. Divide", 0
- next_choice_msg db "Your choice: ", 0
- write_first_number_msg db "Write first number: ", 0
- write_second_number_msg db "Write second number: ", 0
- bad_option db "You chose bad option.", 0
- write_result_msg db "Result: %i", 0xa, 0
- format_int db "%i", 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement