Advertisement
chatchai_j

demo-calc.asm

Jan 29th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SYS_EXIT  equ 1
  2. SYS_READ  equ 3
  3. SYS_WRITE equ 4
  4. STDIN     equ 0
  5. STDOUT    equ 1
  6.  
  7.  
  8. section .data           ; initialized data, messages
  9.  
  10.     welcome     db      'Welcome to the Calculator',10,0  
  11.     input1      db      'Enter first number:',0
  12.     input2      db      'Enter second number:',0
  13.     operators   db  '1. +', 10
  14.         db  '2. -', 10
  15.         db  '3. *', 10
  16.         db  '4. /', 10, 0
  17.     choice      db      'Your choice is: ',10,0
  18.     outcome     db      'and the Result is: ',0
  19.     error       db      'SORRY, Wrong choice!',10,0
  20.  
  21.     nlinea      db      10,0
  22.  
  23. section .bss            ;uninitialized data
  24. ; Spaces reserved for storing the values provided by the user.
  25.  
  26.     opc:        resb    2      ;reserves 2 byte in binary
  27.     num1:       resb    2
  28.     num2:       resb    2
  29.     result:     resb    2
  30.  
  31. section .text           ;asm code
  32.  
  33.     global _start         ;must be declared for using gcc
  34.  
  35. _start:
  36.     jmp main
  37.  
  38. ; printstr  print ascii string, which terminate with 0 byte
  39. ; input
  40. ;   eax - point to begining of string
  41. ; return
  42. ;   nothing
  43. printstr:
  44.     push    eax
  45.     push    ebx
  46.     push    ecx
  47.     push    edx
  48.     push    esi
  49.  
  50.     mov ecx, eax    ; we store address of string in eax
  51.     mov eax, SYS_WRITE
  52.     mov ebx, STDOUT
  53.     mov esi, ecx    ; looking for end of stringS
  54.     mov edx, 0
  55. .0:
  56.     cmp byte [esi], 0
  57.     je  .1
  58.     inc edx
  59.     inc esi
  60.     jmp .0
  61. .1:
  62.     int 0x80
  63.     pop esi
  64.     pop edx
  65.     pop ecx
  66.     pop ebx
  67.     pop eax
  68.     ret
  69.  
  70. ; myexit    terminate program
  71. ; input
  72. ;   nothing
  73. ; output
  74. ;   nothing
  75. myexit:
  76.     mov eax, 1
  77.     mov ebx, 0
  78.     int 0x80
  79.  
  80.  
  81. ; main function start here!
  82. main:
  83.     mov eax, welcome
  84.     call    printstr
  85.     mov eax, input1
  86.     call    printstr
  87.  
  88.     ; We get num1 value for first number.
  89.     mov eax, 3
  90.     mov ebx, 0
  91.     mov ecx, num1
  92.     mov edx, 2
  93.     int 80h
  94.  
  95.     mov eax, input2
  96.     call    printstr
  97.  
  98.     ; We get num2 value for second number.
  99.     mov eax, 3
  100.     mov ebx, 0
  101.     mov ecx, num2
  102.     mov edx, 2
  103.     int 80h
  104.     mov al,[num2]
  105.     sub al, '0'
  106.     mov [num2],al
  107.    
  108.     mov al,[num1]
  109.     sub al, '0'
  110.     mov [num1],al
  111.    
  112.     mov eax, operators
  113.     call    printstr
  114.  
  115.     mov eax, choice
  116.     call    printstr
  117.    
  118.     ; Shows our choice
  119.     mov eax,3
  120.     mov ebx,0
  121.     mov ecx,opc
  122.     mov edx,2
  123.     int 80h
  124.  
  125.     mov ah, [opc]   ; Move our choice to the 8bit ah register
  126.     sub ah, '0'     ; Convert from ascii to decimal
  127.    
  128.    
  129.  
  130.     ; We compare the value entered by the user to know what operation to perform.
  131.     ; Basically, it is IF function (cmp)
  132.     cmp ah, 1    
  133.     je addition         ;jump equal or jump zero
  134.     cmp ah, 2
  135.     je subtraction
  136.     cmp ah, 3
  137.     je multiplication
  138.     cmp ah, 4
  139.     je division
  140.  
  141.  
  142.  
  143.  
  144.     ; If the user choose wrong choice, it will show error and close porgram
  145.     mov eax, error
  146.     call printstr
  147.     call myexit
  148.  
  149. addition:            
  150.     ; We keep the numbers in the 8bit al and bl registers
  151.     mov al, [num1]
  152.     mov bl, [num2]
  153.  
  154.     ; Converts from ascii to decimal
  155.     ;sub al, '0'
  156.     ;sub bl, '0'
  157.  
  158.     ; Addition
  159.     add al, bl
  160.  
  161.     ; Again converts from decimal to ascii
  162.     add al, '0'
  163.  
  164.     ; We move the result
  165.     mov [result], al
  166.  
  167.  
  168.  
  169.     ; We end our program
  170.     jmp exit
  171.  
  172. subtraction:
  173.     ; We keep the numbers in 8bit al and bl registers
  174.     mov al, [num1]
  175.     mov bl, [num2]
  176.  
  177.     ; Convert from ascii to decimal
  178.     ;sub al, '0'
  179.     ;sub bl, '0'
  180.  
  181.     ; Subtraction
  182.     sub al, bl
  183.  
  184.     ; Again converts from decimal to ascii
  185.     add al, '0'
  186.  
  187.     ; We move the result
  188.     mov [result], al
  189.  
  190.  
  191.  
  192.     ; We end our program
  193.     jmp exit
  194.  
  195. multiplication:
  196.  
  197.     ; We keep the numbers in 8bit al and bl registers
  198.     mov al, [num1]
  199.     mov bl, [num2]
  200.  
  201.     ; Convert from ascii to decimal
  202.     ;sub al, '0'
  203.     ;sub bl, '0'
  204.  
  205.     ; Multiplication. AX = AL x BL
  206.     mul bl
  207.  
  208.     ; Again converts from decimal to ascii
  209.     add ax, '0'
  210.  
  211.     ; We move the result
  212.     mov [result], ax
  213.  
  214.  
  215.  
  216.     ; We end our program
  217.     jmp exit
  218.  
  219. division:
  220.  
  221.     ; We keep the numbers in 8bit al and bl registers
  222.     mov al, [num1]
  223.     mov bl, [num2]
  224.  
  225.     mov dx, 0
  226.     mov ah, 0
  227.  
  228.     ; Convert from ascii to decimall
  229.     ;sub al, '0'
  230.     ;sub bl, '0'
  231.  
  232.     ; Division. AL = AX / BX
  233.     div bl
  234.  
  235.     ; Again converts from decimal to ascii
  236.     add ax, '0'
  237.    
  238.     ; We move the result
  239.     mov [result], ax
  240.  
  241.    
  242.  
  243.     ; We end our program
  244.     jmp exit
  245.  
  246. exit:
  247.  
  248. ; Prints 'and the Result is: '
  249.     mov eax, outcome
  250.     call printstr
  251.  
  252.     ; Prints outcome
  253.     mov eax, 4
  254.     mov ebx, 1
  255.     mov ecx, result
  256.     mov edx, 1
  257.     int 80h
  258.     ; Prints on screen two new lines
  259.     mov eax, 4
  260.     mov ebx, 1
  261.     mov ecx, nlinea
  262.     ; mov edx, lnlinea
  263.     int 80h
  264.    
  265.     ; We end our program
  266.     mov eax, 1
  267.     mov ebx, 0
  268.     int 80h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement