Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;+=======================================================================+
- ;A simple assembly program that computes average of 3 two-digit numbers.
- ;Copyright (C) 2014 Ferriel Lisandro B. Melarpis
- ;
- ; This program is free software: you can redistribute it and/or modify
- ; it under the terms of the GNU General Public License as published by
- ; the Free Software Foundation, either version 3 of the License, or (at
- ; your option) any later version.
- ;
- ; This program is distributed in the hope that it will be useful, but
- ; WITHOUT ANY WARRANTY; without even the implied warranty of
- ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- ; General Public License for more details.
- ;
- ; You should have received a copy of the GNU General Public License
- ; along with this program. If not, see <http://www.gnu.org/licenses/>.
- ;+========================================================================+
- section .data
- menu db 'Main Menu',0xa,\
- '[1] Addition',0xa,\
- '[2] Subtraction',0xa,\
- '[3] Multiplication',0xa,\
- '[4] Division',0xa,\
- '[5] Exit',0xa,\
- 'Enter choice: '
- menulen equ $-menu
- ask db 'Enter number: '
- asklen equ $-ask
- nsign db '-'
- nslen equ $-nsign
- inv db 'Invalid Input!',0xa
- ilen equ $-inv
- n db 0xa
- nlen equ $-n
- section .bss
- choice resb 1
- sign1 resb 1
- num1a resb 1
- num1b resb 1
- num1 resw 1
- sign2 resb 1
- num2a resb 1
- num2b resb 1
- num2 resw 1
- temp resw 1
- res1 resb 1
- res2 resb 1
- res3 resb 1
- section .text
- global _start
- _start:
- _menu:
- ;print menu
- mov eax, 4
- mov ebx, 1
- mov ecx, menu
- mov edx, menulen
- int 0x80
- ;scan choice
- mov eax, 3
- xor ebx, ebx
- mov ecx, choice
- mov edx, 2
- int 0x80
- ;convert choice to int
- sub byte [choice], 0x30
- ;if choice >=5
- cmp byte [choice], 5
- ja _inv ;if choice > 5 invalid
- je _exit ;if choice = 5 exit
- ;ask for first number
- mov eax, 4
- mov ebx, 1
- mov ecx, ask
- mov edx, asklen
- int 0x80
- ;scan sign of first number
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, sign1
- mov edx, 1
- int 0x80
- ;scan tens digit
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, num1a
- mov edx, 1
- int 0x80
- ;scan ones digit
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, num1b
- mov edx, 2
- int 0x80
- ;ask for second number
- mov eax, 4
- mov ebx, 1
- mov ecx, ask
- mov edx, asklen
- int 0x80
- ;scan sign of second number
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, sign2
- mov edx, 1
- int 0x80
- ;scan tens digit
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, num2a
- mov edx, 1
- int 0x80
- ;scan ones digit
- xor ah, ah
- mov eax, 3
- xor ebx, ebx
- mov ecx, num2b
- mov edx, 2
- int 0x80
- ;convert first number's digits to int
- sub byte [num1a], 0x30
- sub byte [num1b], 0x30
- ;combine tens and ones digit
- mov al, [num1a]
- mov bl, 10
- mul bl
- add al, [num1b]
- mov [num1], al
- ;convert second number's digits to int
- sub byte [num2a], 0x30
- sub byte [num2b], 0x30
- ;combine tens and ones digit
- mov al, [num2a]
- mul bl
- add al, [num2b]
- mov [num2], al
- cmp byte [sign1], '-'
- jne _step1
- neg word [num1]
- _step1:
- cmp byte [sign2], '-'
- jne _step2
- neg word [num2]
- _step2:
- cmp byte [choice], 1
- je _add
- cmp byte [choice], 2
- je _sub
- cmp byte [choice], 3
- je _mul
- cmp byte [choice], 4
- je _div
- _add:
- mov ax, [num1]
- add ax, [num2]
- jns _res
- jmp _neg
- _sub:
- jmp _menu
- _mul:
- jmp _menu
- _div:
- jmp _menu
- _inv:
- mov eax, 4
- mov ebx, 1
- mov ecx, inv
- mov edx, ilen
- int 0x80
- jmp _menu
- _neg:
- mov [temp], ax
- mov eax, 4
- mov ebx, 1
- mov ecx, nsign
- mov edx, nslen
- int 0x80
- neg word [temp]
- mov ax, [temp]
- _res:
- mov bl, 10
- xor ah, ah
- div bl
- mov byte[res3], ah
- add byte[res3], 0x30
- mov [temp], al
- mov ax, [temp]
- xor ah, ah
- div bl
- mov byte[res2], ah
- add byte[res2], 0x30
- mov byte[res1], al
- add byte[res1], 0x30
- mov eax, 4
- mov ebx, 1
- mov ecx, res1
- mov edx, 1
- int 0x80
- mov eax, 4
- mov ebx, 1
- mov ecx, res2
- mov edx, 1
- int 0x80
- mov eax, 4
- mov ebx, 1
- mov ecx, res3
- mov edx, 1
- int 0x80
- mov eax, 4
- mov ebx, 1
- mov ecx, n
- mov edx, nlen
- int 0x80
- jmp _menu
- _exit:
- mov eax, 1
- xor ebx, ebx
- int 0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement