Advertisement
Guest User

Assembly 8086 assignment work calculator

a guest
Apr 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; multi-segment executable file template.
  2.  
  3. data segment
  4.     menu db "Please enter your Choice",0ah,0dh,"Add ->'+'",0ah,0dh,"Sub ->'-'",0ah,0dh,"Mul ->'*'",0ah,0dh,"div ->'/'",0ah,0dh,"Mod ->'%'",0ah,0dh,"Pow ->'^'",0ah,0dh,"Exit ->'x'",0ah,0dh,"$"    
  5.     menuLength dw menuLength - menu  
  6.    
  7.     userSellection db ?
  8.    
  9.     tempNum db 3 dup (?)
  10.     tempNumLength dw tempNumLength - tempNum
  11.    
  12.     firstNumMSG db 0ah,0dh,"Please enter the First Number: $"
  13.     firstNumMSGLen dw firstNumMSGlen - firstNumMSG    
  14.    
  15.     secendNumMSG db 0ah,0dh,"Please enter the Secend Number: $"
  16.     secendNumMSGLen dw firstNumMSGlen - firstNumMSG
  17.    
  18.     resultMsg db 0ah,0dh,"Result: $"
  19.     resultMsgLen dw resultMsgLen - resultMsg
  20.    
  21.     firstNum db ?
  22.     secendNum db ?
  23.     result db ?
  24.    
  25. ends
  26.  
  27. stack segment
  28.     dw   128  dup(0)
  29. ends
  30.  
  31. code segment
  32. start:
  33.      
  34.     mov ax, data
  35.     mov ds, ax
  36.     mov es, ax
  37.  
  38. restartCalc:
  39. mov result, 0
  40. mov userSellection, 0
  41. mov firstNum, 0
  42. mov secendNum, 0
  43.    ; print menu -----
  44.     mov dx, offset (menu)
  45.     mov ah, 9
  46.     int 21h
  47.    
  48.     ;input from user ----
  49.    
  50.     mov ah, 1
  51.     int 21h
  52.     mov userSellection, al
  53.    
  54.     cmp al, 'x' ; end and not get 2 more numbers if option is x
  55.         je end
  56.    
  57.     ;input for numbers - first num
  58.    
  59.     mov bp, 2
  60.     mov si, offset(firstNum)
  61.    
  62.     ;print first request for input
  63.         mov dx, offset (firstNumMSG)
  64.         mov ah, 9
  65.         int 21h
  66.    
  67.     num2:
  68.     mov bx, 0
  69.     mov cx, tempNumLength ;set cx for max len of number
  70.     dec bp ; flag for current number input(2 for the first number, 1 for the secend number)
  71.    
  72.     NextNum:
  73.        
  74.         mov ah, 1
  75.         int 21h
  76.  
  77.         cmp al, 13
  78.         je compNum
  79.    
  80.         mov tempNum[bx], al
  81.         inc bx
  82.    
  83.         loop NextNum
  84.    
  85.     compNum: ;turning the digits in temp to a number stored in cuurnt input state sellected by dh
  86.        
  87.         mov cx, bx
  88.         mov bx, 0
  89.         mov dl, 10
  90.        
  91.         nextDig:
  92.             mov ah,0
  93.             mov al, [si]
  94.        
  95.             mul dl
  96.             mov [si],al
  97.        
  98.             sub tempnum[bx] , 48
  99.             mov al, tempnum[bx]
  100.             add [si], al
  101.        
  102.             inc bx
  103.        
  104.             loop nextDig
  105.     cmp bp, 0
  106.    
  107.     je menuSellection:
  108.    
  109.         mov si, offset(secendNum);moving to the secend num
  110.        
  111.         ;print secend request for input
  112.             mov dx, offset (secendNumMSG)
  113.             mov ah, 9
  114.             int 21h
  115.        
  116.         jmp num2
  117.    
  118.    
  119.     menuSellection: ;checks what option was selected, if option not valid returns to start
  120.         mov al,userSellection
  121.        
  122.         cmp al, '+'
  123.         je addition
  124.        
  125.         cmp al, '-'
  126.         je substruction
  127.        
  128.         cmp al, '*'
  129.         je multiplication
  130.        
  131.         cmp al, '/'
  132.         je divide
  133.        
  134.         cmp al, '%'
  135.         je mod
  136.        
  137.         cmp al, '^'
  138.         je pow
  139.        
  140.         jmp restartCalc
  141.        
  142.     ;calcules for each option    
  143.     addition:
  144.         mov al, firstNum
  145.         add al, secendNum      
  146.         mov result, al
  147.         jmp showResult
  148.    
  149.     substruction:
  150.         mov al, FirstNum
  151.         sub al, secendNum
  152.         mov result,al
  153.         jmp showResult
  154.    
  155.     multiplication:
  156.         mov al, firstNum
  157.         mov ah,0
  158.         mul secendNum
  159.         mov result, al
  160.         jmp showResult
  161.    
  162.     divide:
  163.         mov al, firstNum
  164.         mov ah, 0
  165.         div secendNum
  166.         mov result,al
  167.         jmp showResult
  168.    
  169.     mod:
  170.         mov al, firstNum
  171.         mov ah, 0
  172.         div secendNum
  173.         mov result,ah
  174.         jmp showResult
  175.    
  176.     pow:
  177.         mov cl, secendNum
  178.         mov ch, 0
  179.         mov al, firstNum
  180.         mov ah, 0
  181.         dec cl
  182.         nextPow:
  183.             mul firstNum
  184.             loop nextPow
  185.         mov result, al
  186.         jmp showResult
  187.    
  188.     ;prints the result in the correct length              
  189.     showResult:
  190.         mov dx, offset (resultMsg)
  191.         mov ah, 9
  192.         int 21h
  193.        
  194.         mov al, result
  195.         mov ah, 0
  196.         mov dl,10
  197.         div dl
  198.         add ah,48
  199.         mov tempNum[2],ah
  200.         mov ah,0
  201.         div dl
  202.         add ah,48
  203.         add al,48
  204.         mov tempNum[1],ah
  205.         mov tempNum[0],al
  206.        
  207.         mov ah,result ;finding length of result
  208.        
  209.         cmp ah, 10
  210.         jb oneDigit
  211.        
  212.         cmp Ah, 100
  213.         jb twoDigit
  214.        
  215.         jmp threeDigit
  216.        
  217.         oneDigit:    ;printing setting set accordingly
  218.             mov cx, 1
  219.             mov bx, 2
  220.             jmp printNum
  221.        
  222.         twoDigit:
  223.             mov cx, 2
  224.             mov bx, 1
  225.             jmp printNum
  226.        
  227.         threeDigit:
  228.             mov cx,3
  229.             mov bx,0
  230.             jmp printNum
  231.        
  232.         printNum:    ;printing acoording to length.
  233.             mov ah,2
  234.             mov dl,tempNum[bx]
  235.             int 21h    
  236.             inc bx
  237.             loop printNum
  238.            
  239.     mov cx,4
  240.    
  241.     printSpaces:;prints spaces beetween runs of calculator
  242.         mov ah, 2
  243.         mov dl, 13
  244.         int 21h
  245.         mov ah, 2
  246.         mov dl, 10
  247.         int 21h
  248.         loop printSpaces
  249.         jmp restartCalc
  250.              
  251.     end:
  252.    
  253.     mov ax, 4c00h ; exit to operating system.
  254.     int 21h    
  255. ends
  256.  
  257. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement