Advertisement
Guest User

something's wrong

a guest
Sep 21st, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;+=======================================================================+
  2. ;A simple assembly program that computes average of 3 two-digit numbers.          
  3. ;Copyright (C) 2014 Ferriel Lisandro B. Melarpis                                                            
  4. ;                                                                                                                                      
  5. ; This program is free software: you can redistribute it and/or modify                          
  6. ; it under the terms of the GNU General Public License as published by                      
  7. ; the Free Software Foundation, either version 3 of the License, or (at                        
  8. ; your option) any later version.                                                                                    
  9. ;                                                                                                                                    
  10. ; This program is distributed in the hope that it will be useful, but    
  11. ; WITHOUT ANY WARRANTY; without even the implied warranty of            
  12. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      
  13. ; General Public License for more details.                                
  14. ;                                                                        
  15. ; You should have received a copy of the GNU General Public License      
  16. ; along with this program.  If not, see <http://www.gnu.org/licenses/>.  
  17. ;+========================================================================+
  18. section .data
  19.     menu db 'Main Menu',0xa,\
  20.     '[1] Addition',0xa,\
  21.     '[2] Subtraction',0xa,\
  22.     '[3] Multiplication',0xa,\
  23.     '[4] Division',0xa,\
  24.     '[5] Exit',0xa,\
  25.     'Enter choice: '
  26.     menulen equ $-menu
  27.     ask db 'Enter number: '
  28.     asklen equ $-ask
  29.     nsign db '-'
  30.     nslen equ $-nsign
  31.     inv db 'Invalid Input!',0xa
  32.     ilen equ $-inv
  33.     n db 0xa
  34.     nlen equ $-n
  35. section .bss
  36.     choice resb 1
  37.     sign1 resb 1
  38.     num1a resb 1
  39.     num1b resb 1
  40.     num1 resw 1
  41.     sign2 resb 1
  42.     num2a resb 1
  43.     num2b resb 1
  44.     num2 resw 1
  45.     temp resw 1
  46.     res1 resb 1
  47.     res2 resb 1
  48.     res3 resb 1
  49. section .text
  50.     global _start
  51. _start:
  52. _menu:
  53.     ;print menu
  54.     mov eax, 4
  55.     mov ebx, 1
  56.     mov ecx, menu
  57.     mov edx, menulen
  58.     int 0x80
  59.     ;scan choice
  60.     mov eax, 3
  61.     xor ebx, ebx
  62.     mov ecx, choice
  63.     mov edx, 2
  64.     int 0x80
  65.     ;convert choice to int
  66.     sub byte [choice], 0x30
  67.     ;if choice >=5
  68.     cmp byte [choice], 5
  69.     ja _inv                                             ;if choice > 5 invalid
  70.     je _exit                                            ;if choice = 5 exit
  71.     ;ask for first number
  72.     mov eax, 4
  73.     mov ebx, 1
  74.     mov ecx, ask
  75.     mov edx, asklen
  76.     int 0x80
  77.     ;scan sign of first number
  78.     xor ah, ah
  79.     mov eax, 3
  80.     xor ebx, ebx
  81.     mov ecx, sign1
  82.     mov edx, 1
  83.     int 0x80
  84.     ;scan tens digit
  85.     xor ah, ah
  86.     mov eax, 3
  87.     xor ebx, ebx
  88.     mov ecx, num1a
  89.     mov edx, 1
  90.     int 0x80
  91.     ;scan ones digit
  92.     xor ah, ah
  93.     mov eax, 3
  94.     xor ebx, ebx
  95.     mov ecx, num1b
  96.     mov edx, 2
  97.     int 0x80
  98.     ;ask for second number
  99.     mov eax, 4
  100.     mov ebx, 1
  101.     mov ecx, ask
  102.     mov edx, asklen
  103.     int 0x80
  104.     ;scan sign of second number
  105.     xor ah, ah
  106.     mov eax, 3
  107.     xor ebx, ebx
  108.     mov ecx, sign2
  109.     mov edx, 1
  110.     int 0x80
  111.     ;scan tens digit
  112.     xor ah, ah
  113.     mov eax, 3
  114.     xor ebx, ebx
  115.     mov ecx, num2a
  116.     mov edx, 1
  117.     int 0x80
  118.     ;scan ones digit
  119.     xor ah, ah
  120.     mov eax, 3
  121.     xor ebx, ebx
  122.     mov ecx, num2b
  123.     mov edx, 2
  124.     int 0x80
  125.     ;convert first number's digits to int
  126.     sub byte [num1a], 0x30
  127.     sub byte [num1b], 0x30
  128.     ;combine tens and ones digit
  129.     mov al, [num1a]
  130.     mov bl, 10
  131.     mul bl
  132.     add al, [num1b]
  133.     mov [num1], al
  134.     ;convert second number's digits to int
  135.     sub byte [num2a], 0x30
  136.     sub byte [num2b], 0x30
  137.     ;combine tens and ones digit
  138.     mov al, [num2a]
  139.     mul bl
  140.     add al, [num2b]
  141.     mov [num2], al
  142.    
  143.     cmp byte [sign1], '-'
  144.     jne _step1
  145.     neg word [num1]
  146.     _step1:
  147.     cmp byte [sign2], '-'
  148.     jne _step2
  149.     neg word [num2]
  150.     _step2:
  151.     cmp byte [choice], 1
  152.     je _add
  153.     cmp byte [choice], 2
  154.     je _sub
  155.     cmp byte [choice], 3
  156.     je _mul
  157.     cmp byte [choice], 4
  158.     je _div
  159.    
  160. _add:
  161.     mov ax, [num1]
  162.     add ax, [num2]
  163.     jns _res
  164.     jmp _neg
  165.    
  166.  _sub:
  167.     jmp _menu
  168.    
  169. _mul:
  170.     jmp _menu
  171.    
  172. _div:
  173.     jmp _menu
  174.    
  175. _inv:
  176.     mov eax, 4
  177.     mov ebx, 1
  178.     mov ecx, inv
  179.     mov edx, ilen
  180.     int 0x80
  181.    
  182.     jmp _menu
  183.    
  184. _neg:
  185.     mov [temp], ax
  186.    
  187.     mov eax, 4
  188.     mov ebx, 1
  189.     mov ecx, nsign
  190.     mov edx, nslen
  191.     int 0x80
  192.    
  193.     neg word [temp]
  194.     mov ax, [temp]
  195.      
  196. _res:
  197.     mov bl, 10
  198.     xor ah, ah
  199.     div bl
  200.     mov byte[res3], ah
  201.     add byte[res3], 0x30
  202.     mov [temp], al
  203.     mov ax, [temp]
  204.     xor ah, ah
  205.     div bl
  206.     mov byte[res2], ah
  207.     add byte[res2], 0x30
  208.     mov byte[res1], al
  209.     add byte[res1], 0x30
  210.  
  211.     mov eax, 4
  212.     mov ebx, 1
  213.     mov ecx, res1
  214.     mov edx, 1
  215.     int 0x80
  216.  
  217.     mov eax, 4
  218.     mov ebx, 1
  219.     mov ecx, res2
  220.     mov edx, 1
  221.     int 0x80
  222.  
  223.     mov eax, 4
  224.     mov ebx, 1
  225.     mov ecx, res3
  226.     mov edx, 1
  227.     int 0x80
  228.    
  229.     mov eax, 4
  230.     mov ebx, 1
  231.     mov ecx, n
  232.     mov edx, nlen
  233.     int 0x80
  234.    
  235.     jmp _menu
  236.    
  237. _exit:
  238.     mov eax, 1
  239.     xor ebx, ebx
  240.     int 0x80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement