Advertisement
Guest User

Usombli

a guest
May 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;===*===*===*===*===*===*===
  2. ; Assembly 8086 project
  3. ; 10th grade, Gvahim project
  4. ; Author: Daniel Monastirski
  5. ;===*===*===*===*===*===*===
  6.  
  7. .model small
  8. .stack 100h
  9. .386   
  10. ;Note: the functions multiply and divide can only work on integers.
  11. ;      -12.2+3.5 DOES NOT WORK
  12.  
  13. ;The function divide does show the result including the fraction.
  14.  
  15. _resetScreen MACRO
  16.     mov [calcColor], 30
  17.     lea si, RectCoords
  18.     add si, 8
  19.     push si
  20.     call drawRect
  21. ENDM
  22.  
  23. _callAnsPrint MACRO n, position
  24.     push n                              ;bp+8
  25.     push offset ans                     ;bp+6
  26.     push offset position                ;bp+4
  27.     call printAns
  28. ENDM
  29.  
  30. _drawDigitRect MACRO n
  31.     add si, n
  32.     push si
  33.     call drawRect
  34. ENDM
  35.  
  36. _callCalculatingFunc MACRO func
  37.     lea si, ansFloat
  38.     inc si
  39.     push si
  40.     push offset oprnd2float
  41.     push offset oprnd1float
  42.     call func
  43.     push offset ansInt
  44.     push offset oprnd2intgr
  45.     push offset oprnd1intgr
  46.     call func
  47.     jmp printResult
  48. ENDM
  49.  
  50. _compareTwoArrays MACRO arr1, arr2
  51.     push offset arr2
  52.     push offset arr1
  53.     call compare
  54. ENDM
  55.  
  56. _callDrawRectangle MACRO LocationArray
  57.     push locationArray
  58.     call drawRect
  59. ENDM
  60.  
  61. .data
  62. oprndInput1 db 10 dup (10)              ;raw input from user
  63. oprndInput2 db 10 dup (10)              ;raw input from user
  64. oprnd1intgr db 10 dup (0)               ;Before the dot of oprnd1
  65. oprnd1float db 10 dup (0)               ;After the dot of oprnd1. Recives '0' if oprnd1 is an integer.
  66. oprnd2intgr db 10 dup (0)               ;Before the dot of oprnd2
  67. oprnd2float db 10 dup (0)               ;After the dot of oprnd2. Recives '0' if oprnd2 is an integer.
  68. ans         db 10 dup (0)
  69. ansInt      db 10 dup (0)
  70. ansFloat    db 2Eh,0,0,0,0,0,0,0,0,0
  71. temp        db 10 dup (0)
  72. operator    db 0
  73. sign        db 0
  74. calcX       dw 60
  75. calcY       dw 10
  76. calcColor   db 7
  77. counter     db 0
  78. ;rectangles array:minX, minY, maxX,maxY.contains the values of 26 rectangles: gray background, screen, 24 buttons.
  79. RectCoords  dw 10,10,310,190, 15,15,305,45 ;Gray background and the calculator screen.
  80.             dw 15,50,60,80,   64,50,109,80,   113,50,158,80,   162,50,256,80,   260,50,305,80  
  81.             dw 15,85,60,115,  64,85,109,115,  113,85,158,115,  162,85,207,115,  211,85,256,115,  260,85,305,115  
  82.             dw 15,120,60,150, 64,120,109,150, 113,120,158,150, 162,120,207,150, 211,120,256,150, 260,120,305,150  
  83.             dw 15,155,60,185, 64,155,109,185, 113,155,158,185, 162,155,207,185, 211,155,256,185, 260,155,305,185   
  84.    
  85. button9     dw 128,55,143,59,   139,59,143,75,   128,55,133,67,   128,63,143,67,   128,71,143,75
  86. button8     dw 79,55,94,59,     91,58,94,75,     79,63,94,67,     79,71,94,75,     79,58,83,75
  87. button7     dw 30,55,45,59,     41,59,45,75
  88. button6     dw 128,90,143,94,   128,90,132,110,  128,98,143,102,  128,106,143,110, 139,98,143,110
  89. button5     dw 79,90,94,94,     79,98,94,102,    79,106,94,110,   79,90,84,98,     90,102,94,110
  90. button4     dw 41,90,45,110,    30,98,45,102,    30,90,34,98
  91. button3     dw 128,125,143,129, 139,125,143,145, 128,133,143,137, 128,141,143,145
  92. button2     dw 79,125,94,129,   79,133,94,137,   79,141,94,145,   90,125,94,137,   79,133,83,145
  93. button1     dw 41,125,45,145
  94. button0     dw 41,160,45,180,   30,160,34,180,   30,160,45,164,   30,176,45,180
  95. buttonDot   dw 90,176,94,180
  96.  
  97. buttonSqr   dw 134,160,139,165, 128,165,133,170, 140,165,145,170
  98. buttonPlu   dw 174,132,195,138, 182,125,187,145
  99. buttonMin   dw 223,132,244,138
  100. buttonMul   dw 172,90,177,94,   177,94,182,98,   182,98,187,102,  187,102,192,106, 192,106,197,110, 172,106,177,110, 177,102,182,106, 187,94,192,98,   192,90,197,94
  101. buttonDiv   dw 223,97,244,103,  231,90,236,94,   231,107,236,111
  102. buttonEqu   dw 223,162,244,167, 223,173,244,178
  103.  
  104. buttonFac   dw 182,160,187,172, 182,175,187,180
  105. buttonDel   dw 165,55,168,75,   165,55,173,58,   165,72,173,75,   173,58,176,72 ;D
  106.             dw 180,55,183,75,   180,55,191,58,   180,63,191,67,   180,72,191,75 ;E
  107.             dw 195,55,198,75,   195,72,206,75                                   ;L
  108.             dw 210,55,213,75,   210,55,221,58,   210,63,221,67,   210,72,221,75 ;E
  109.             dw 225,55,238,58,   230,55,233,75                                   ;T
  110.             dw 242,55,245,75,   242,55,253,58,   242,63,253,67,   242,72,253,75 ;E
  111. buttonCos   dw 263,58,266,72,   266,55,274,58,   266,72,274,75                                      ;C
  112.             dw 277,58,280,72,   277,55,288,58,   277,72,288,75,   285,58,288,72                     ;O
  113.             dw 291,55,302,59,   291,55,294,63,   291,63,302,67,   299,63,302,75,   291,71,302,75    ;S
  114. buttonSin   dw 263,90,274,94,   263,90,266,99,   263,98,274,102,  271,98,274,110,  263,106,274,110      ;S
  115.             dw 278,90,282,94,   278,98,282,110                                                          ;I
  116.             dw 286,90,289,110,  289,93,292,96,   292,90,299,93,   299,93,302,110                        ;N
  117. buttonTan   dw 263,130,274,133, 267,125,270,142, 269,142,274,145                                    ;T
  118.             dw 280,125,286,128, 285,128,288,145, 280,132,286,135, 277,134,280,142, 279,142,288,145  ;A
  119.             dw 291,125,294,145, 291,127,296,129, 296,125,300,127, 299,127,302,145                   ;N
  120. buttonCot   dw 263,163,266,177, 266,160,274,163, 266,177,274,180                            ;C
  121.             dw 277,163,280,177, 277,160,288,163, 277,177,288,180, 285,163,288,177           ;O
  122.             dw 291,165,302,168, 295,160,298,177, 297,177,302,180                            ;T
  123. ;Screen display positions. order: up-mid-down-topleft-topright-bottomleft-bottomright-dot. Every row = 32 words = 64 bytes.
  124. pos0        dw 23,20,45,24,   23,28,45,32,   23,36,45,40,   23,20,26,30,   42,20,45,30,   23,30,26,40,   42,30,45,40,    42,37,45,40
  125. pos1        dw 51,20,73,24,   51,28,73,32,   51,36,73,40,   51,20,54,30,   70,20,73,30,   51,30,54,40,   70,30,73,40,    70,37,73,40
  126. pos2        dw 79,20,101,24,  79,28,101,32,  79,36,101,40,  79,20,82,30,   98,20,101,30,  79,30,82,40,   98,30,101,40,   98,37,101,40
  127. pos3        dw 107,20,129,24, 107,28,129,32, 107,36,129,40, 107,20,110,30, 126,20,129,30, 107,30,110,40, 126,30,129,40,  126,37,129,40 
  128. pos4        dw 135,20,157,24, 135,28,157,32, 135,36,157,40, 135,20,138,30, 154,20,157,30, 135,30,138,40, 154,30,157,40,  154,37,157,40
  129. pos5        dw 163,20,185,24, 163,28,185,32, 163,36,185,40, 163,20,166,30, 182,20,185,30, 163,30,166,40, 182,30,185,40,  182,37,185,40
  130. pos6        dw 191,20,213,24, 191,28,213,32, 191,36,213,40, 191,20,194,30, 210,20,213,30, 191,30,194,40, 210,30,213,40,  210,37,213,40
  131. pos7        dw 219,20,241,24, 219,28,241,32, 219,36,241,40, 219,20,222,30, 238,20,241,30, 219,30,222,40, 238,30,241,40,  238,37,241,40
  132. pos8        dw 247,20,269,24, 247,28,269,32, 247,36,269,40, 247,20,250,30, 266,20,269,30, 247,30,250,40, 266,30,269,40,  266,37,269,40
  133. pos9        dw 275,20,297,24, 275,28,297,32, 275,36,297,40, 275,20,278,30, 294,20,297,30, 275,30,278,40, 294,30,297,40,  294,37,297,40
  134.  
  135. diagonal1   dw 69,38,73,40,   67,35,71,37,   65,32,69,34
  136. diagonal2   dw 97,38,101,40,  95,35,99,37,   93,32,97,34
  137. diagonal3   dw 153,38,157,40, 151,35,155,37, 149,32,153,34
  138.  
  139. ;Flags:
  140. swappedArrays db 0              ;If oprndInput2 is bigger than oprndInput1, value changes
  141. usedClick     db 0              ;If the mouse click was on a button, value changes
  142. operatorFlag  db 0              ;If the first operand is less than 10 digits long, value changes
  143. equalsFlag    db 0              ;If the second operand is less than 10 digits long, value changes
  144. shortNumFlag  db 0              ;If the int part of ans has filler 0's to be 10 digits long, value changes.
  145. oprnd1NegFlag db 0              ;If Operand1 is negative, value changes
  146. .code
  147. START:
  148. mov ax,@data
  149. mov ds, ax
  150. ;Setting to graphic mode
  151. mov ax, 13h                
  152. int 10h
  153. begin:
  154. mov [usedClick], 0
  155. mov [operator], 0
  156. call drawCalc
  157.  
  158. mov ax,0h                   ;Resetting mouse port
  159. int 33h
  160. mov ax,1h                   ;Showing the mouse on the screen
  161. int 33h
  162. lea di, oprndInput1
  163. mov [operator], 0
  164.  
  165. click1:
  166.     lea si, pos0
  167.     push di
  168.     push si
  169.     call clickHandler1
  170.     cmp [usedClick], 0          ;checking if the user clicked on an area that is not a button.
  171.     je click1
  172.     mov [usedClick], 0
  173.     inc di
  174.    
  175. click2:
  176.     lea si, pos1
  177.     push di
  178.     push si
  179.     call clickHandler1
  180.     cmp [usedClick], 0         
  181.     je click2
  182.     cmp [operator], 'R'
  183.     je begin
  184.     cmp [operator], 'P'
  185.     je equationReady
  186.     cmp [operator], 0
  187.     jne operandII
  188.     mov [usedClick], 0
  189.     inc di
  190.  
  191. click3:lea si, pos2
  192.     push di
  193.     push si
  194.     call clickHandler1
  195.     cmp [usedClick], 0         
  196.     je click2
  197.     cmp [operator], 'R'
  198.     je begin
  199.     cmp [operator], 'P'
  200.     je equationReady
  201.     cmp [operator], 0
  202.     jne operandII
  203.     mov [usedClick], 0
  204.     inc di
  205.  
  206. click4:
  207.     lea si, pos3
  208.     push di
  209.     push si
  210.     call clickHandler1
  211.     cmp [usedClick], 0         
  212.     je click2
  213.     cmp [operator], 'R'
  214.     je begin
  215.     cmp [operator], 'P'
  216.     je equationReady
  217.     cmp [operator], 0
  218.     jne operandII
  219.     mov [usedClick], 0
  220.     inc di
  221.  
  222. click5:
  223.     lea si, pos4
  224.     push di
  225.     push si
  226.     call clickHandler1
  227.     cmp [usedClick], 0         
  228.     je click2
  229.     cmp [operator], 'R'
  230.     je begin
  231.     cmp [operator], 'P'
  232.     je equationReady
  233.     cmp [operator], 0
  234.     jne operandII
  235.     mov [usedClick], 0
  236.     inc di
  237.  
  238. click6:
  239.     lea si, pos5
  240.     push di
  241.     push si
  242.     call clickHandler1
  243.     cmp [usedClick], 0         
  244.     je click2
  245.     cmp [operator], 'R'
  246.     je begin
  247.     cmp [operator], 'P'
  248.     je equationReady
  249.     cmp [operator], 0
  250.     jne operandII
  251.     mov [usedClick], 0
  252.     inc di
  253.  
  254. click7:
  255.     lea si, pos6
  256.     push di
  257.     push si
  258.     call clickHandler1
  259.     cmp [usedClick], 0         
  260.     je click2
  261.     cmp [operator], 'R'
  262.     je begin
  263.     cmp [operator], 'P'
  264.     je equationReady
  265.     cmp [operator], 0
  266.     jne operandII
  267.     mov [usedClick], 0
  268.     inc di
  269.        
  270. click8:
  271.     lea si, pos7
  272.     push di
  273.     push si
  274.     call clickHandler1
  275.     cmp [usedClick], 0         
  276.     je click2
  277.     cmp [operator], 'R'
  278.     je begin
  279.     cmp [operator], 'P'
  280.     je equationReady
  281.     cmp [operator], 0
  282.     jne operandII
  283.     mov [usedClick], 0
  284.     inc di
  285.  
  286. click9:
  287.     lea si, pos8
  288.     push di
  289.     push si
  290.     call clickHandler1
  291.     cmp [usedClick], 0         
  292.     je click2
  293.     cmp [operator], 'R'
  294.     je begin
  295.     cmp [operator], 'P'
  296.     je equationReady
  297.     cmp [operator], 0
  298.     jne operandII
  299.     mov [usedClick], 0
  300.     inc di
  301.  
  302. click10:
  303.     lea si, pos9
  304.     push di
  305.     push si
  306.     call clickHandler1
  307.     cmp [usedClick], 0         
  308.     je click2
  309.     cmp [operator], 'R'
  310.     je begin
  311.     cmp [operator], 'P'
  312.     je equationReady
  313.     cmp [operator], 0
  314.     jne operandII
  315.     mov [usedClick], 0
  316.     inc di
  317.  
  318. operatorHere:
  319. call CheckLeftClick
  320. shr cx,1
  321. mov [calcColor], 19
  322.     call pressPlus
  323.     call pressMinus
  324.     call pressMultiply
  325.     call pressDivide
  326.     call pressDELETE
  327.     call pressPow
  328. call CheckReleaseClick
  329. cmp [operator], 0
  330. je operatorHere
  331.    
  332. operandII:lea di, oprndInput2
  333. click11:
  334.     lea si, pos0
  335.     push di
  336.     push si
  337.     call clickHandler2
  338.     cmp [usedClick], 0         
  339.     je click11
  340.     cmp [operator], 'R'
  341.     je begin
  342.     cmp [equalsFlag], 1
  343.     je equationReady
  344.     mov [usedClick], 0
  345.     inc di
  346.    
  347. click12:
  348.     lea si, pos1
  349.     push di
  350.     push si
  351.     call clickHandler2
  352.     cmp [usedClick], 0         
  353.     je click12
  354.     cmp [operator], 'R'
  355.     je begin
  356.     cmp [equalsFlag], 1
  357.     je equationReady
  358.     mov [usedClick], 0
  359.     inc di
  360.  
  361. click13:
  362.     lea si, pos2
  363.     push di
  364.     push si
  365.     call clickHandler2
  366.     cmp [usedClick], 0         
  367.     je click13
  368.     cmp [operator], 'R'
  369.     je begin
  370.     cmp [equalsFlag], 1
  371.     je equationReady
  372.     mov [usedClick], 0
  373.     inc di
  374.  
  375. click14:
  376.     lea si, pos3
  377.     push di
  378.     push si
  379.     call clickHandler2
  380.     cmp [usedClick], 0         
  381.     je click14
  382.     cmp [operator], 'R'
  383.     je begin
  384.     cmp [equalsFlag], 1
  385.     je equationReady
  386.     mov [usedClick], 0
  387.     inc di
  388.  
  389. click15:
  390.     lea si, pos4
  391.     push di
  392.     push si
  393.     call clickHandler2
  394.     cmp [usedClick], 0         
  395.     je click15
  396.     cmp [operator], 'R'
  397.     je begin
  398.     cmp [equalsFlag], 1
  399.     je equationReady
  400.     mov [usedClick], 0
  401.     inc di
  402.  
  403. click16:
  404.     lea si, pos5
  405.     push di
  406.     push si
  407.     call clickHandler2
  408.     cmp [usedClick], 0         
  409.     je click16
  410.     cmp [operator], 'R'
  411.     je begin
  412.     cmp [equalsFlag], 1
  413.     je equationReady
  414.     mov [usedClick], 0
  415.     inc di
  416.  
  417. click17:
  418.     lea si, pos6
  419.     push di
  420.     push si
  421.     call clickHandler2
  422.     cmp [usedClick], 0         
  423.     je click17
  424.     cmp [operator], 'R'
  425.     je begin
  426.     cmp [equalsFlag], 1
  427.     je equationReady
  428.     mov [usedClick], 0
  429.     inc di
  430.  
  431. click18:
  432.     lea si, pos7
  433.     push di
  434.     push si
  435.     call clickHandler2
  436.     cmp [usedClick], 0         
  437.     je click18
  438.     cmp [operator], 'R'
  439.     je begin
  440.     cmp [equalsFlag], 1
  441.     je equationReady
  442.     mov [usedClick], 0
  443.     inc di
  444.  
  445. click19:
  446.     lea si, pos8
  447.     push di
  448.     push si
  449.     call clickHandler2
  450.     cmp [usedClick], 0         
  451.     je click19
  452.     cmp [operator], 'R'
  453.     je begin
  454.     cmp [equalsFlag], 1
  455.     je equationReady
  456.     mov [usedClick], 0
  457.     inc di
  458.  
  459. click20:
  460.     lea si, pos9
  461.     push di
  462.     push si
  463.     call clickHandler2
  464.     cmp [usedClick], 0         
  465.     je click20
  466.     cmp [operator], 'R'
  467.     je begin
  468.     cmp [equalsFlag], 1
  469.     je equationReady
  470.     mov [usedClick], 0
  471.     inc di
  472.  
  473. equationReady:
  474. xor bx, bx
  475. push offset RectCoords
  476. call drawCalc
  477. ;Seperating the input operands into an integer part and a float part, for easier calculation
  478. push offset oprndInput1
  479. push offset oprnd1float
  480. push offset oprnd1intgr
  481. call dotCheck
  482. push offset oprndInput2
  483. push offset oprnd2float
  484. push offset oprnd2intgr
  485. call dotCheck
  486.  
  487. ;Checking what operator was chosen and calling the correct function.
  488. PLUSCHECK:  cmp [operator], '+'
  489.             jne MINUSCHECK
  490.             cmp [oprnd1NegFlag], 1
  491.             jne prepareForPlus
  492.             ;If the first operand is negative and |num1| < num2, call minus to calculate oprnd2 -oprnd1
  493.             _compareTwoArrays oprnd1intgr, oprnd2intgr
  494.             cmp [swappedArrays], 1
  495.             je resultNeg1
  496.             _compareTwoArrays oprnd1float, oprnd2float
  497.             cmp [swappedArrays], 1
  498.             je resultNeg1
  499.            
  500.             ;If the first num is negative but |num1| > num2
  501.             mov [sign], 1
  502.             _callCalculatingFunc minus
  503.            
  504.             resultNeg1:
  505.             lea si, ansFloat
  506.             inc si
  507.             push si
  508.             push offset oprnd1float
  509.             push offset oprnd2float
  510.             call minus
  511.             push offset ansInt
  512.             push offset oprnd1intgr
  513.             push offset oprnd2intgr
  514.             call minus
  515.             jmp printResult
  516.            
  517.             prepareForPlus: _callCalculatingFunc plus
  518.            
  519. MINUSCHECK: cmp [operator], '-'
  520.             jne MULTICHECK
  521.             ;Checking if the first num is negative.
  522.             cmp [oprnd1NegFlag], 1
  523.             jne CheckAbiggerThanB
  524.             mov [oprnd1NegFlag], 0
  525.             mov [sign], 1
  526.             _callCalculatingFunc plus
  527.             ;Checking if the first num is smaller than the 2nd num.
  528.             CheckAbiggerThanB:
  529.             _compareTwoArrays oprnd1intgr, oprnd2intgr
  530.             cmp [swappedArrays], 1
  531.             je resultNeg
  532.             _callCalculatingFunc minus
  533.             resultNeg:
  534.             mov [sign], 1
  535.             lea si, ansFloat
  536.             inc si
  537.             push si
  538.             push offset oprnd1float
  539.             push offset oprnd2float
  540.             call minus
  541.             push offset ansInt
  542.             push offset oprnd1intgr
  543.             push offset oprnd2intgr
  544.             call minus
  545.            
  546. MULTICHECK: cmp [operator], '*'
  547.             jne DIVCHECK   
  548.             mov al, [oprnd1NegFlag]
  549.             mov [sign], al
  550.             xor ax,ax
  551.             xor cx,cx
  552.             push offset ansInt
  553.             push offset oprnd2intgr
  554.             push offset oprnd1intgr
  555.             call multiply
  556.             jmp printResult
  557.            
  558. DIVCHECK:   cmp [operator], '/'
  559.             jne FACTCHECK
  560.             mov al, [oprnd1NegFlag]
  561.             mov [sign], al
  562.            
  563.             push offset temp
  564.             call resetArray
  565.             _compareTwoArrays oprnd2intgr, temp     ;If the user tried to divide by 0, display error message.
  566.             cmp [swappedArrays], 0
  567.             jne divisionOk
  568.             call errorsHandler
  569.             jmp programFinished
  570.            
  571.             divisionOk:
  572.             push offset oprnd2intgr
  573.             push offset oprnd1intgr
  574.             call divide
  575.             jmp printResult
  576.            
  577. FACTCHECK:  cmp [operator], '!'
  578.             jne COSCHECK
  579.  
  580. COSCHECK:   cmp [operator], 'C'
  581.             jne SINCHECK
  582.  
  583. SINCHECK:   cmp [operator], 'S'
  584.             jne TANCHECK
  585.  
  586. TANCHECK:   cmp [operator], 'T'
  587.             jne COTCHECK
  588.  
  589. COTCHECK:   cmp [operator], 't' ; Or - Do we need cot?
  590.             jne SQRCHECK
  591.  
  592. SQRCHECK:   cmp [operator], 'P'
  593.             jne printResult
  594.             push offset ansInt
  595.             push offset oprnd1intgr
  596.             call square
  597.            
  598. ;Printing the answer, digit by digit, using a macro
  599. printResult:
  600. mov [calcColor], 0
  601. call createAnswerArray
  602. _callAnsPrint 0, pos0
  603. _callAnsPrint 1, pos1
  604. _callAnsPrint 2, pos2
  605. _callAnsPrint 3, pos3
  606. _callAnsPrint 4, pos4
  607. _callAnsPrint 5, pos5
  608. _callAnsPrint 6, pos6
  609. _callAnsPrint 7, pos7
  610. _callAnsPrint 8, pos8
  611. _callAnsPrint 9, pos9
  612.  
  613. programFinished:
  614. mov ah,00h                  ;wait for input
  615. int 16h
  616. mov ah, 0                   ;Return to text mode
  617. mov al, 2
  618. int 10h
  619. .exit
  620.  
  621.  
  622. clickHandler1 proc
  623.     push bp
  624.     mov bp, sp
  625.     mov si, [bp+4]
  626.     mov di, [bp+6]
  627.     call CheckLeftClick
  628.     shr cx,1
  629.     mov [calcColor], 0
  630.     push di
  631.     push si
  632.     call checkPressedNumButton
  633.     call pressDELETE
  634.     ;Checking for operators
  635.     call pressMinus
  636.     mov [calcColor], 19
  637.     call pressPlus
  638.     call pressMultiply
  639.     call pressDivide
  640.     call pressPow
  641.     call CheckReleaseClick
  642.     pop bp
  643.     ret
  644. clickHandler1 endp
  645.  
  646. clickHandler2 proc
  647.     push bp
  648.     mov bp, sp
  649.     mov si, [bp+4]
  650.     mov di, [bp+6]
  651.     call CheckLeftClick
  652.     shr cx,1
  653.     mov [calcColor], 0
  654.     push di
  655.     push si
  656.     call checkPressedNumButton ;After entering the first number and operator, the only legal available buttons are the numbers, the reset, and the equals buttons.
  657.     call pressDELETE
  658.     call pressEquals
  659.     call CheckReleaseClick
  660.     pop bp
  661.     ret
  662. clickHandler2 endp
  663.  
  664.  
  665. ;Function waits for the mouse to be clicked. Allows user input.
  666. CheckLeftClick proc
  667.     MouseLP:
  668.         mov ax,3h
  669.         int 33h
  670.         cmp bx, 00000001b               ; check left mouse click
  671.     jne MouseLP
  672.     ret
  673. CheckLeftClick endp
  674. ;Function waits for the mouse to be released. Allows user input.
  675. CheckReleaseClick proc
  676.     ReleaseLP:
  677.         mov ax, 3h
  678.         int 33h
  679.         cmp bx, 00000000b
  680.     jne ReleaseLP
  681.     ret
  682. CheckReleaseClick endp
  683. ;Function handling operator +
  684. ;Input: the offsets of the arrays oprndInput1,oprndInput2,ansInt
  685. plus proc
  686.     push bp
  687.     mov bp, sp
  688.     mov si, [bp+4]
  689.     mov di, [bp+6]
  690.     mov bx, [bp+8]
  691.     add si, 9
  692.     add di, 9
  693.     add bx, 9
  694.     digitSum:
  695.         mov al,[si]
  696.         add al,[di]
  697.         add al,ah
  698.         xor ah,ah
  699.         overflow:
  700.             cmp al,10
  701.             jl okayP
  702.             inc ah
  703.             sub al,10
  704.         jmp overflow
  705.         okayP:
  706.             mov [bx],al
  707.             dec si              ;every iteration, the registers will point toward the arrays offsets
  708.             dec di
  709.             dec bx
  710.     cmp bx, [bp+8]
  711.     jge digitSum
  712.     AdditionDone:
  713.     pop bp
  714.     ret 6
  715. plus endp
  716.  
  717. ;function handling si - di => bx
  718. ;Input: the offsets of the arrays oprndInput1,oprndInput2,ansInt
  719. minus proc
  720.     push bp
  721.     mov bp,sp
  722.     mov si, [bp+4]
  723.     mov di, [bp+6]
  724.     mov bx, [bp+8]
  725.     add si, 9
  726.     add di, 9
  727.     add bx, 9
  728.     mov cx, 10
  729.     digitSub:
  730.         mov al,[si]
  731.         sub al,[di]
  732.         sub al,ah
  733.         xor ah,ah
  734.        
  735.         overflowMin:
  736.             cmp al,0
  737.             jge okMin
  738.             inc ah
  739.             add al,10
  740.         jmp overflowMin
  741.         okMin:
  742.             mov [bx],al
  743.             dec si
  744.             dec di
  745.             dec bx
  746.     loop digitSub
  747.     minusComplete:
  748.     pop bp
  749.     ret 6
  750. minus endp
  751.  
  752. ;function handling operator *
  753. ;Input: the offsets of 3 arrays.
  754. ;Output: Result of multiplication of the first 2 arrays, saved in the 3rd array
  755. multiply proc
  756.     push bp
  757.     mov bp,sp            
  758.     mov di, [bp+4]  ;oprnd1
  759.     mov si, [bp+6]  ;oprnd2
  760.     mov bx, [bp+8]
  761.     add di, 9
  762.     xor cx,cx
  763.     xor ax,ax
  764.     multi:
  765.         mov cl, byte ptr[di]
  766.         cmp cx, 0
  767.         je prepareTemp
  768.         oprndDigitLoop: ;representing multiplication by adding n to itself m times, and saving the result in temp
  769.             ;adding [si] to temp cx times
  770.             push bx
  771.             push si
  772.             push di
  773.             push offset temp
  774.             push si
  775.             push offset temp
  776.             call plus
  777.             pop di
  778.             pop si
  779.             pop bx
  780.         loop oprndDigitLoop
  781.        
  782.         prepareTemp:
  783.         push si
  784.         mov si, offset counter
  785.         mov cl, [si]
  786.         cmp cx, 0
  787.         je copyTmp
  788.         xor ax,ax
  789.        
  790.         shiftTemp:
  791.             mov si, offset temp
  792.             add si, 9
  793.             mov al, [si]
  794.             mov [si], byte ptr 0
  795.             dec si
  796.             shiftLeft:
  797.                 mov ah, [si]    ;Saving the value in ah
  798.                 mov [si], al    ;[si] will recieve the value of [si-1]
  799.                 dec si
  800.                 mov al, ah 
  801.             cmp si, offset temp
  802.             jge shiftLeft
  803.         loop shiftTemp 
  804.         ;copying the result from temp and adding it to the answer array
  805.         copyTmp:
  806.             pop si
  807.             push bx
  808.             push si
  809.             push di
  810.            
  811.             push bx
  812.             push bx
  813.             push offset temp
  814.             call plus
  815.            
  816.             ;Resetting temp for future use.
  817.             push offset temp
  818.             call resetArray
  819.             pop di
  820.             pop si
  821.             pop bx
  822.         nothingToMul:
  823.         dec di
  824.         inc [counter]
  825.         cmp [counter], 10
  826.         jle multi
  827.     pop bp
  828.     ret 6
  829. multiply endp
  830. ;Function handling operator /
  831. ;Input: The offsets of the arrays oprndInput1,oprndInput2,ansInt. [remainder is in dx]
  832. divide proc
  833.     push bp
  834.     mov bp,sp
  835.     push offset temp
  836.     call resetArray
  837.     mov bx, offset temp
  838.     add bx, 9
  839.     mov [bx], byte ptr 1
  840.     mov [counter], 0
  841.     findIntDiv: ;Checking how many times oprnd2 can fit inside oprnd1.
  842.         mov si, [bp+4]
  843.         mov di, [bp+6] 
  844.         push di
  845.         push si
  846.         call compare
  847.         cmp [swappedArrays], 1
  848.         je continueDiv
  849.        
  850.         push offset ansInt
  851.         push offset temp
  852.         push offset ansInt
  853.         call plus ;Adding 1 to the result
  854.         mov si, [bp+4]
  855.         mov di, [bp+6]
  856.         push si
  857.         push di
  858.         push si
  859.         call minus ;calculating SI-DI => SI
  860.     jmp findIntDiv
  861.    
  862.     continueDiv:
  863.     push offset temp
  864.     call resetArray
  865.     inc [counter]
  866.     mov bx, offset temp
  867.     mov [bx], byte ptr 1
  868.     lea bx, ansFloat
  869.     add bl, [counter]
  870.     mov si, [bp+4]
  871.     add si, 9
  872.     mov al, [si]
  873.     mov [si], byte ptr 0
  874.     dec si
  875.     mov cx, 9
  876.     shiftRemainder:
  877.         mov ah, [si]    ;Saving the value in ah
  878.         mov [si], al    ;[si] will recieve the value of [si-1]
  879.         dec si
  880.         mov al, ah 
  881.     loop shiftRemainder
  882.  
  883.     findRemainder: ;Checking how many times oprnd2 can fit inside oprnd1.
  884.         mov si, [bp+4]
  885.         mov di, [bp+6]
  886.         push di
  887.         push si
  888.         call compare
  889.         cmp [swappedArrays], 1
  890.         je divDone
  891.         push bx
  892.         mov si, bx
  893.         push bx
  894.         push offset temp
  895.         push si
  896.         call plus ;Adding 1 to the result
  897.         mov si, [bp+4]
  898.         mov di, [bp+6]
  899.         push si
  900.         push di
  901.         push si
  902.         call minus
  903.         pop bx
  904.     jmp findRemainder
  905.     divDone:
  906.     cmp [counter],9
  907.     jle continueDiv
  908.     pop bp
  909.     ret 4
  910. divide endp
  911.  
  912. ;Function calculates oprndInput1 to the power of 2.
  913. square proc
  914.     push bp
  915.     mov bp,sp
  916.     mov di,[bp+4]
  917.     mov bx, [bp+6]
  918.     push bx
  919.     push di
  920.     push di
  921.     call multiply
  922.     pop bp
  923.     ret 4
  924. square endp
  925.  
  926. ;Function resets all program variables that have changed during run time.
  927. programReset proc
  928.     lea di, oprndInput1
  929.     mov cx, 10
  930.     oprndInput1Loop:
  931.         mov byte ptr[di], 10
  932.         inc di
  933.     loop oprndInput1Loop
  934.    
  935.     lea di, oprndInput2
  936.     mov cx, 10
  937.     oprndInput2Loop:
  938.         mov byte ptr[di], 10
  939.         inc di
  940.     loop oprndInput2Loop
  941.  
  942.     push offset oprnd1intgr
  943.     call resetArray
  944.     push offset oprnd1float
  945.     call resetArray
  946.     push offset oprnd2intgr
  947.     call resetArray
  948.     push offset oprnd2float
  949.     call resetArray
  950.     push offset ansInt
  951.     call resetArray
  952.     lea si, ansFloat
  953.     mov byte ptr [si], 2Eh
  954.     inc si
  955.     push si
  956.     call resetArray
  957.     push offset temp
  958.     call resetArray
  959.     ;Resetting flag values
  960.     mov [usedClick], 1
  961.     mov [operatorFlag], 0
  962.     mov [equalsFlag], 0
  963.     mov [operator], 'R'  ;Using the operator variable to notify the program the restart function was called.
  964.     mov [sign], 0
  965.     ret
  966. programReset endp
  967.  
  968. ;Function checks if an array has a decimal point and breaks it to 2 different arrays.
  969. ;Input: The offsets of the original operand array, an array for the integer part, and an array for the float part.
  970. ;Output: An integer part and a float part, in 2 different arrays.
  971. dotCheck proc
  972.     push bp            
  973.     mov bp, sp
  974.     mov si, [bp+4]      ;integer array
  975.     mov di, [bp+6]      ;float array
  976.     mov bx, [bp+8]      ;original input array
  977.     add si, 9
  978.     mov cx, 10          ;counter
  979.     checkForDot:        ;Searching the array to find if the number is an integer or not.
  980.         mov al, [byte ptr bx]
  981.         cmp al, 2Eh
  982.         je numIsFloat   ;If one of the values in the array is 2Eh, the number is not an integer.
  983.         inc bx
  984.         loop checkForDot
  985.         jmp numIsInt    ;If the loop ended and did not jump to numIsFloat, the number is an int.
  986.        
  987.     numIsFloat:         ;Starting from the dot, copying the rest of the array to the float array.
  988.         inc bx
  989.         mov al, [bx]
  990.         push cx
  991.         dec cx
  992.         floatLoop:
  993.             cmp al, 10
  994.             je prepareForInt
  995.             numStartsHere: mov [di], al
  996.             inc bx
  997.             inc di
  998.             inc dx          ;counter
  999.             mov al, [bx]    ;checking if the array is a number that's not 10 digits long.
  1000.         loop floatLoop
  1001.        
  1002.         prepareForInt:
  1003.         mov bx, [bp+8]
  1004.         pop cx
  1005.         inc cx
  1006.         mov dx, 10
  1007.         sub dx, cx
  1008.         add bx, dx
  1009.         xor dx, dx
  1010.         jmp intLoop
  1011.        
  1012.     numIsInt:
  1013.         mov bx, [bp+8]
  1014.         add bx, 9
  1015.         intLoop:
  1016.             mov al, [bx]
  1017.             cmp al, 10
  1018.             je notGoodDigit
  1019.             mov [si], al
  1020.             dec si
  1021.             notGoodDigit:
  1022.             dec bx
  1023.             cmp bx, [bp+8]
  1024.         jge intLoop
  1025.     pop bp
  1026.     ret 6
  1027. dotCheck endp
  1028.  
  1029. ;Function compares the values of 2 arrays and puts into si the offset bigger array and into di the other
  1030. ;Input: the offsets of the two arrays
  1031. ;Output: SI pointing to the array representing the bigger number, and DI to the other.
  1032. ;        the variable swappedArrays will contain 0 if the arrays are equal, -1 if the first one is bigger, and 1 if the second array is bigger.
  1033. compare proc
  1034.     push bp
  1035.     mov bp,sp
  1036.     mov si, [bp+4]     
  1037.     mov di, [bp+6]     
  1038.     mov [swappedArrays], 0
  1039.     mov cx,10
  1040.     chk:
  1041.         mov al, [si]
  1042.         cmp al,[di]     ;if si and di are equal
  1043.         je contChk
  1044.         cmp al,[di]
  1045.         jg bigger
  1046.         smaller:
  1047.         mov [swappedArrays], 1
  1048.         jmp checkCompleted
  1049.         bigger:
  1050.         mov [swappedArrays], -1
  1051.         contChk:
  1052.         inc si
  1053.         inc di
  1054.         cmp [swappedArrays], 0
  1055.         jne checkCompleted
  1056.     loop chk
  1057.     checkCompleted:
  1058.     cmp [swappedArrays],0
  1059.     jle nochange
  1060.     mov si, [bp+6]
  1061.     mov di, [bp+4]
  1062.     jmp cont
  1063.     nochange:
  1064.     mov si, [bp+4]
  1065.     mov di, [bp+6]
  1066.     cont:
  1067.     pop bp
  1068.     ret 4
  1069. compare endp
  1070.  
  1071. ;Function changes the values of an array to the same value
  1072. ;Input: the offset of the array and the value
  1073. resetArray proc
  1074.     push bp
  1075.     mov bp, sp
  1076.     mov si, [bp+4]
  1077.     mov cx, 10
  1078.     reset:
  1079.         mov byte ptr[si], 0
  1080.         inc si
  1081.     loop reset
  1082.     pop bp
  1083.     ret 2
  1084. resetArray endp
  1085.  
  1086. ;Function handles the setting of the rectangle coloring.
  1087. ;Input: none.
  1088. drawCalc proc
  1089.     lea si, RectCoords
  1090.     mov [calcColor], 29h
  1091.     xor bx,bx
  1092.     buttonsLoop:
  1093.         cmp bx, 0
  1094.             jne screenCheck
  1095.             mov [calcColor], 20     ;gray background
  1096.             jmp print
  1097.         screenCheck:
  1098.             cmp bx, 1
  1099.             jne delCheck
  1100.             mov [calcColor], 30     ;screen
  1101.             jmp print
  1102.         delCheck:
  1103.             cmp bx, 5
  1104.             jne normal
  1105.             mov [calcColor], 43     ;DEL button
  1106.             jmp print
  1107.         normal:
  1108.         mov [calcColor], 29h        ;all other buttons.
  1109.         print:
  1110.         _callDrawRectangle si
  1111.         add si, 8
  1112.         inc bx
  1113.         cmp bx, 25
  1114.     jl buttonsLoop
  1115.     mov [calcColor], 0
  1116.     lea si, button9
  1117.     mov bx, 0
  1118.     symbolLoop:                     ;button symbols.
  1119.         _callDrawRectangle si
  1120.         add si, 8
  1121.         inc bx
  1122.         cmp bx, 127
  1123.     jl symbolLoop
  1124.     ret
  1125. drawCalc endp
  1126.  
  1127. ;Function draws a rectangle.
  1128. ;input: an array containing the values minX,minY,maxX,maxY. color is set outside of the function.
  1129. drawRect proc
  1130.     push bp
  1131.     mov bp, sp
  1132.     push ax
  1133.     push cx
  1134.     mov si, [bp+4]
  1135.     mov ax, [si]
  1136.     mov [calcX],ax             
  1137.     button:
  1138.         mov si, [bp+4]
  1139.         add si, 2
  1140.         mov ax, [si]
  1141.         sub si, 2
  1142.         mov [calcY],ax         
  1143.         bRow:
  1144.             mov cx, [calcX]
  1145.             mov dx, [calcY]
  1146.             mov al, [calcColor]
  1147.             mov ah, 0ch
  1148.             int 10h
  1149.             inc[calcY]
  1150.             add si, 6
  1151.             mov ax, [si]
  1152.             sub si, 6
  1153.             cmp [calcY], ax    
  1154.             jbe bRow
  1155.         inc [calcX]
  1156.     add si, 4
  1157.     mov ax, [si]
  1158.     sub si, 4
  1159.     cmp [calcX], ax            
  1160.     jbe button
  1161.     pop cx
  1162.     pop ax
  1163.     pop bp
  1164.     ret 2
  1165. drawRect endp
  1166.  
  1167. ;Function checks which numeric button was pressed.
  1168. ;Input: The offsets of the current operand array and the screen position.
  1169. ;Output: The operand recieves the correct character, and it is printed on the screen.
  1170. checkPressedNumButton proc
  1171.     push bp
  1172.     mov bp, sp
  1173.    
  1174.     pressDot:
  1175.     cmp cx, 64
  1176.     jl press0
  1177.     cmp cx, 109
  1178.     jg press0
  1179.     cmp dx, 155
  1180.     jl press0
  1181.     cmp dx, 185
  1182.     jg press0
  1183.     mov di, [bp+6]
  1184.     mov bl, 2Eh
  1185.     mov [di], bl
  1186.     mov [usedClick], 1
  1187.     mov si, [bp+4]
  1188.     push si
  1189.     call drawDot
  1190.     jmp pressDone
  1191.  
  1192.     press0:
  1193.     cmp cx, 15
  1194.     jl press1
  1195.     cmp cx, 60
  1196.     jg press1
  1197.     cmp dx, 155
  1198.     jl press1
  1199.     cmp dx, 185
  1200.     jg press1
  1201.     mov di, [bp+6]
  1202.     mov bl, 0
  1203.     mov [di], bl
  1204.     mov [usedClick], 1
  1205.     mov si, [bp+4]
  1206.     push si
  1207.     call draw0
  1208.     jmp pressDone
  1209.    
  1210.     press1:
  1211.     cmp cx, 15
  1212.     jl press2
  1213.     cmp cx, 60
  1214.     jg press2
  1215.     cmp dx, 120
  1216.     jl press2
  1217.     cmp dx, 150
  1218.     jg press2
  1219.     mov di, [bp+6]
  1220.     mov bl, 1
  1221.     mov [di], bl
  1222.     mov [usedClick], 1
  1223.     mov si, [bp+4]
  1224.     push si
  1225.     call draw1
  1226.     jmp pressDone
  1227.    
  1228.     press2:
  1229.     cmp cx, 64
  1230.     jl press3
  1231.     cmp cx, 109
  1232.     jg press3
  1233.     cmp dx, 120
  1234.     jl press3
  1235.     cmp dx, 150
  1236.     jg press3
  1237.     mov di, [bp+6]
  1238.     mov bl, 2
  1239.     mov [di], bl
  1240.     mov [usedClick], 1
  1241.     mov si, [bp+4]
  1242.     push si
  1243.     call draw2
  1244.     jmp pressDone
  1245.  
  1246.     press3:
  1247.     cmp cx, 113
  1248.     jl press4
  1249.     cmp cx, 158
  1250.     jg press4
  1251.     cmp dx, 120
  1252.     jl press4
  1253.     cmp dx, 150
  1254.     jg press4
  1255.     mov di, [bp+6]
  1256.     mov bl, 3
  1257.     mov [di], bl
  1258.     mov [usedClick], 1
  1259.     mov si, [bp+4]
  1260.     push si
  1261.     call draw3
  1262.     jmp pressDone
  1263.    
  1264.     press4:
  1265.     cmp cx, 15
  1266.     jl press5
  1267.     cmp cx, 60
  1268.     jg press5
  1269.     cmp dx, 85
  1270.     jl press5
  1271.     cmp dx, 115
  1272.     jg press5
  1273.     mov di, [bp+6]
  1274.     mov bl, 4
  1275.     mov [di], bl
  1276.     mov [usedClick], 1
  1277.     mov si, [bp+4]
  1278.     push si
  1279.     call draw4
  1280.     jmp pressDone
  1281.    
  1282.     press5:
  1283.     cmp cx, 64
  1284.     jl press6
  1285.     cmp cx, 109
  1286.     jg press6
  1287.     cmp dx, 85
  1288.     jl press6
  1289.     cmp dx, 115
  1290.     jg press6
  1291.     mov di, [bp+6]
  1292.     mov bl, 5
  1293.     mov [di], bl
  1294.     mov [usedClick], 1
  1295.     mov si, [bp+4]
  1296.     push si
  1297.     call draw5
  1298.     jmp pressDone
  1299.    
  1300.     press6:
  1301.     cmp cx, 113
  1302.     jl press7
  1303.     cmp cx, 158
  1304.     jg press7
  1305.     cmp dx, 85
  1306.     jl press7
  1307.     cmp dx, 115
  1308.     jg press7
  1309.     mov di, [bp+6]
  1310.     mov bl, 6
  1311.     mov [di], bl
  1312.     mov [usedClick], 1
  1313.     mov si, [bp+4]
  1314.     push si
  1315.     call draw6
  1316.     jmp pressDone
  1317.    
  1318.     press7:
  1319.     cmp cx, 15
  1320.     jl press8
  1321.     cmp cx, 60
  1322.     jg press8
  1323.     cmp dx, 50
  1324.     jl press8
  1325.     cmp dx, 80
  1326.     jg press8
  1327.     mov [usedClick], 1
  1328.     mov di, [bp+6]
  1329.     mov bl, 7
  1330.     mov [di], bl
  1331.     mov si, [bp+4]
  1332.     push si
  1333.     call draw7
  1334.     jmp pressDone
  1335.    
  1336.     press8:
  1337.     cmp cx, 64
  1338.     jl press9
  1339.     cmp cx, 109
  1340.     jg press9
  1341.     cmp dx, 50
  1342.     jl press9
  1343.     cmp dx, 80
  1344.     jg press9
  1345.     mov [usedClick], 1
  1346.     mov di, [bp+6]
  1347.     mov bl, 8
  1348.     mov [di], bl
  1349.     mov si, [bp+4]
  1350.     push si
  1351.     call draw8
  1352.     jmp pressDone
  1353.    
  1354.     press9:
  1355.     cmp cx, 113
  1356.     jl pressDone
  1357.     cmp cx, 158
  1358.     jg pressDone
  1359.     cmp dx, 50
  1360.     jl pressDone
  1361.     cmp dx, 80
  1362.     jg pressDone
  1363.     mov [usedClick], 1
  1364.     mov di, [bp+6]
  1365.     mov bl, 9
  1366.     mov [di], bl
  1367.     mov si, [bp+4]
  1368.     push si
  1369.     call draw9
  1370.    
  1371.     pressDone:
  1372.     pop bp
  1373.     ret 4
  1374. checkPressedNumButton endp
  1375.  
  1376. ;Function checks if user pressed the delete button.
  1377. ;Input: The cursor location, saved in CX and DX.
  1378. ;Output: If the function confirms the user clicked on the delete button, it calls the reset function.
  1379. pressDELETE proc  
  1380.     cmp cx, 162
  1381.     jl noAC
  1382.     cmp cx, 256
  1383.     jg noAC
  1384.     cmp dx, 50
  1385.     jl noAC
  1386.     cmp dx, 80
  1387.     jg noAC
  1388.     call programReset
  1389.     noAC: ret
  1390. pressDELETE endp
  1391.  
  1392. ;The function draws the digit 9 on the screen.
  1393. ;Input: Screen position
  1394. ;Output: The digit 9 is printed on screen.
  1395. draw9 proc
  1396.     push bp
  1397.     mov bp, sp
  1398.     mov si, [bp+4]
  1399.     xor bx,bx
  1400.     yes9:
  1401.     _callDrawRectangle si
  1402.     add si, 8
  1403.     inc bx
  1404.     cmp bx,4
  1405.     jle yes9
  1406.     add si, 8
  1407.     _callDrawRectangle si
  1408.     pop bp
  1409.     ret 2
  1410. draw9 endp
  1411. ;The function draws the digit 8 on the screen.
  1412. ;Input: Screen position
  1413. ;Output: The digit 8 is printed on screen.
  1414. draw8 proc
  1415.     push bp
  1416.     mov bp, sp
  1417.     mov si, [bp+4]
  1418.     xor bx,bx
  1419.     yes8:
  1420.     _callDrawRectangle si
  1421.     add si, 8
  1422.     inc bx
  1423.     cmp bx,6
  1424.     jle yes8
  1425.     pop bp
  1426.     ret 2
  1427. draw8 endp
  1428. ;The function draws the digit 7 on the screen.
  1429. ;Input: Screen position
  1430. ;Output: The digit 7 is printed on screen.
  1431. draw7 proc
  1432.     push bp
  1433.     mov bp, sp
  1434.     mov si, [bp+4]
  1435.     _callDrawRectangle si
  1436.     add si, 32
  1437.     _callDrawRectangle si
  1438.     add si, 16
  1439.     _callDrawRectangle si
  1440.     pop bp
  1441.     ret 2
  1442. draw7 endp
  1443. ;The function draws the digit 6 on the screen.
  1444. ;Input: Screen position
  1445. ;Output: The digit 6 is printed on screen.
  1446. draw6 proc     
  1447.     push bp
  1448.     mov bp, sp
  1449.     mov si, [bp+4]
  1450.     _callDrawRectangle si
  1451.     add si, 8
  1452.     _callDrawRectangle si
  1453.     add si, 8
  1454.     _callDrawRectangle si
  1455.     add si, 8
  1456.     _callDrawRectangle si
  1457.     add si, 16
  1458.     _callDrawRectangle si
  1459.     add si, 8
  1460.     _callDrawRectangle si
  1461.     pop bp
  1462.     ret 2
  1463. draw6 endp
  1464. ;The function draws the digit 5 on the screen.
  1465. ;Input: Screen position
  1466. ;Output: The digit 5 is printed on screen.
  1467. draw5 proc     
  1468.     push bp        
  1469.     mov bp, sp
  1470.     mov si, [bp+4]
  1471.     _callDrawRectangle si
  1472.     add si, 8
  1473.     _callDrawRectangle si
  1474.     add si, 8
  1475.     _callDrawRectangle si
  1476.     add si, 8
  1477.     _callDrawRectangle si
  1478.     add si, 24
  1479.     _callDrawRectangle si
  1480.     pop bp
  1481.     ret 2
  1482. draw5 endp
  1483. ;The function draws the digit 4 on the screen.
  1484. ;Input: Screen position
  1485. ;Output: The digit 4 is printed on screen.
  1486. draw4 proc     
  1487.     push bp        
  1488.     mov bp, sp
  1489.     mov si, [bp+4]
  1490.     add si, 8
  1491.     _callDrawRectangle si
  1492.     add si, 16
  1493.     _callDrawRectangle si
  1494.     add si, 8
  1495.     _callDrawRectangle si
  1496.     add si, 16
  1497.     _callDrawRectangle si
  1498.     pop bp
  1499.     ret 2
  1500. draw4 endp
  1501. ;The function draws the digit 3 on the screen.
  1502. ;Input: Screen position
  1503. ;Output: The digit 3 is printed on screen.
  1504. draw3 proc     
  1505.     push bp        
  1506.     mov bp, sp
  1507.     mov si, [bp+4]
  1508.     _callDrawRectangle si
  1509.     add si, 8
  1510.     _callDrawRectangle si
  1511.     add si, 8
  1512.     _callDrawRectangle si
  1513.     add si, 16
  1514.     _callDrawRectangle si
  1515.     add si, 16
  1516.     _callDrawRectangle si
  1517.     pop bp
  1518.     ret 2
  1519. draw3 endp
  1520. ;The function draws the digit 2 on the screen.
  1521. ;Input: Screen position
  1522. ;Output: The digit 2 is printed on screen.
  1523. draw2 proc     
  1524.     push bp        
  1525.     mov bp, sp
  1526.     mov si, [bp+4]
  1527.     _callDrawRectangle si
  1528.     add si, 8
  1529.     _callDrawRectangle si
  1530.     add si, 8
  1531.     _callDrawRectangle si
  1532.     add si, 16
  1533.     _callDrawRectangle si
  1534.     add si, 8
  1535.     _callDrawRectangle si
  1536.     pop bp
  1537.     ret 2
  1538. draw2 endp
  1539. ;The function draws the digit 1 on the screen.
  1540. ;Input: Screen position
  1541. ;Output: The digit 1 is printed on screen.
  1542. draw1 proc     
  1543.     push bp        
  1544.     mov bp, sp
  1545.     mov si, [bp+4]
  1546.     add si,32
  1547.     _callDrawRectangle si
  1548.     add si, 16
  1549.     _callDrawRectangle si
  1550.     pop bp
  1551.     ret 2
  1552. draw1 endp
  1553. ;The function draws the digit 0 on the screen.
  1554. ;Input: Screen position
  1555. ;Output: The digit 0 is printed on screen.
  1556. draw0 proc
  1557.     push bp
  1558.     mov bp, sp
  1559.     mov si, [bp+4]
  1560.     _callDrawRectangle si
  1561.     add si,16
  1562.     xor bx,bx
  1563.     yes0:
  1564.     _callDrawRectangle si
  1565.     add si, 8
  1566.     inc bx
  1567.     cmp bx,5
  1568.     jle yes0
  1569.     pop bp
  1570.     ret 2
  1571. draw0 endp
  1572. ;The function draws a decimal dot on the screen.
  1573. ;Input: Screen position
  1574. ;Output: The character '.' is printed on screen.
  1575. drawDot proc
  1576.     push bp
  1577.     mov bp, sp
  1578.     mov si, [bp+4]
  1579.     add si, 56
  1580.     _callDrawRectangle si
  1581.     pop bp
  1582.     ret 2
  1583. drawDot endp
  1584. ;The function draws the negative sign on the screen.
  1585. ;Input: Screen position
  1586. ;Output: The negative sign is printed on screen.
  1587. drawNeg proc
  1588.     push bp
  1589.     mov bp, sp
  1590.     mov si, [bp+4]
  1591.     add si, 8
  1592.     _callDrawRectangle si
  1593.     pop bp
  1594.     ret 2
  1595. drawNeg endp
  1596. ;******************************************************************
  1597. pressPow proc
  1598.     cmp cx, 113
  1599.     jl noPow
  1600.     cmp cx, 158
  1601.     jg noPow
  1602.     cmp dx, 155
  1603.     jl noPow
  1604.     cmp dx, 185
  1605.     jg noPow
  1606.     lea si, buttonSqr
  1607.     _callDrawRectangle si
  1608.     add si,8
  1609.     _callDrawRectangle si
  1610.     add si,8
  1611.     _callDrawRectangle si
  1612.     mov [usedClick], 1
  1613.     mov [operatorFlag], 1
  1614.     mov[operator], 'P'
  1615.     noPow:
  1616.     ret
  1617. pressPow endp
  1618.  
  1619. pressPlus proc
  1620.     cmp cx, 162
  1621.     jl noPl
  1622.     cmp cx, 207
  1623.     jg noPl
  1624.     cmp dx, 120
  1625.     jl noPl
  1626.     cmp dx, 150
  1627.     jg noPl
  1628.     lea si, buttonPlu
  1629.     xor bx,bx
  1630.     yesPl:
  1631.     inc bx
  1632.     _callDrawRectangle si
  1633.     add si, 8
  1634.     cmp bx, 2
  1635.     jl yesPl
  1636.     mov [usedClick], 1
  1637.     mov [operatorFlag], 1
  1638.     mov[operator], '+'
  1639.     _resetScreen
  1640.     noPl:
  1641.     ret
  1642. pressPlus endp
  1643.  
  1644. pressMinus proc
  1645.     cmp cx, 211     ;Checking if the user clicked inside the minus button.
  1646.     jl noMi
  1647.     cmp cx, 256
  1648.     jg noMi
  1649.     cmp dx, 120
  1650.     jl noMi
  1651.     cmp dx, 150
  1652.     jg noMi
  1653.     ;Minus can also be used to represent a negative number.
  1654.     cmp si, offset pos0 ;If this is the first click, the first operand is negative.
  1655.     jne minusOP
  1656.     mov [oprnd1NegFlag], 1
  1657.     mov [usedClick], 1
  1658.     add si, 8
  1659.     _callDrawRectangle si
  1660.     jmp noMi
  1661.     minusOP:
  1662.     mov [calcColor], 19
  1663.     lea si, buttonMin
  1664.     _callDrawRectangle si
  1665.     mov [usedClick], 1
  1666.     mov [operatorFlag], 1
  1667.     mov[operator], '-'
  1668.     _resetScreen
  1669.     noMi:
  1670.     ret
  1671. pressMinus endp
  1672.  
  1673.  
  1674. pressMultiply proc
  1675.     cmp cx, 162     ;Checking if the user clicked inside the minus button.
  1676.     jl noMul
  1677.     cmp cx, 207
  1678.     jg noMul
  1679.     cmp dx, 85
  1680.     jl noMul
  1681.     cmp dx, 115
  1682.     jg noMul
  1683.     lea si, buttonMul
  1684.     mov cx, 9
  1685.     yesMul:
  1686.         _callDrawRectangle si
  1687.         add si, 8
  1688.     loop yesMul
  1689.     mov [usedClick], 1
  1690.     mov [operatorFlag], 1
  1691.     mov[operator], '*'
  1692.     _resetScreen
  1693.     noMul:
  1694.     ret
  1695. pressMultiply endp
  1696.  
  1697. pressDivide proc
  1698.     cmp cx, 211     ;Checking if the user clicked inside the divide button.
  1699.     jl noDiv
  1700.     cmp cx, 256
  1701.     jg noDiv
  1702.     cmp dx, 85
  1703.     jl noDiv
  1704.     cmp dx, 115
  1705.     jg noDiv
  1706.     lea si, buttonDiv
  1707.     mov cx, 3
  1708.     yesDiv:
  1709.         _callDrawRectangle si
  1710.         add si, 8              
  1711.     loop yesDiv
  1712.     mov [usedClick], 1
  1713.     mov [operatorFlag], 1
  1714.     mov[operator], '/'
  1715.     _resetScreen
  1716.     noDiv:
  1717.     ret
  1718. pressDivide endp
  1719.  
  1720. pressEquals proc  
  1721.     cmp cx, 211     ;Checking if the user clicked inside the minus button.
  1722.     jl noEqu
  1723.     cmp cx, 256
  1724.     jg noEqu
  1725.     cmp dx, 155
  1726.     jl noEqu
  1727.     cmp dx, 185
  1728.     jg noEqu
  1729.     mov [calcColor], 42
  1730.     lea si, buttonEqu
  1731.     mov cx, 2
  1732.     yesEqu:
  1733.         _callDrawRectangle si
  1734.         add si, 8
  1735.     loop yesEqu
  1736.     mov [usedClick], 1
  1737.     mov [equalsFlag], 1
  1738.     _resetScreen
  1739.     noEqu:
  1740.     ret
  1741. pressEquals endp
  1742.  
  1743. printAns proc
  1744.     push bp
  1745.     mov bp, sp
  1746.     mov di, [bp+4]  ;di = screen pos
  1747.     mov si, [bp+6]  ;si = ansInt digit
  1748.     add si, [bp+8]
  1749.     checkDigit:
  1750.     mov al, byte ptr [si]
  1751.     ans0:
  1752.         cmp al,0
  1753.         jne ans1
  1754.         push di
  1755.         call draw0
  1756.         jmp NothingToPrint
  1757.        
  1758.     ans1:  
  1759.         cmp al, 1
  1760.         jne ans2
  1761.         push di
  1762.         call draw1
  1763.         jmp NothingToPrint
  1764.        
  1765.     ans2:  
  1766.         cmp al, 2
  1767.         jne ans3
  1768.         push di
  1769.         call draw2
  1770.         jmp NothingToPrint
  1771.        
  1772.     ans3:  
  1773.         cmp al, 3
  1774.         jne ans4
  1775.         push di
  1776.         call draw3
  1777.         jmp NothingToPrint
  1778.        
  1779.     ans4:  
  1780.         cmp al, 4
  1781.         jne ans5
  1782.         push di
  1783.         call draw4
  1784.         jmp NothingToPrint
  1785.        
  1786.     ans5:  
  1787.         cmp al, 5
  1788.         jne ans6
  1789.         push di
  1790.         call draw5
  1791.         jmp NothingToPrint
  1792.        
  1793.     ans6:  
  1794.         cmp al, 6
  1795.         jne ans7
  1796.         push di
  1797.         call draw6
  1798.         jmp NothingToPrint
  1799.        
  1800.     ans7:  
  1801.         cmp al, 7
  1802.         jne ans8
  1803.         push di
  1804.         call draw7
  1805.         jmp NothingToPrint
  1806.        
  1807.     ans8:  
  1808.         cmp al, 8
  1809.         jne ans9
  1810.         push di
  1811.         call draw8
  1812.         jmp NothingToPrint
  1813.     ans9:  
  1814.         cmp al, 9
  1815.         jne ansD
  1816.         push di
  1817.         call draw9
  1818.         jmp NothingToPrint
  1819.        
  1820.     ansD:  
  1821.         cmp al, '.'
  1822.         jne ansN
  1823.         push di
  1824.         call drawDot
  1825.         jmp NothingToPrint
  1826.        
  1827.     ansN:  
  1828.         cmp al, '-'
  1829.         jne NothingToPrint
  1830.         push di
  1831.         call drawNeg
  1832.        
  1833.     NothingToPrint:
  1834.     pop bp
  1835.     ret 6
  1836. printAns endp
  1837.  
  1838. createAnswerArray proc
  1839.     push bp
  1840.     mov bp, sp
  1841.     lea si, ans
  1842.     lea di, ansInt
  1843.     lea bx, ansFloat
  1844.     mov cx, 10
  1845.    
  1846.     copySign:
  1847.         cmp [sign], 1
  1848.         jne copyInt
  1849.         mov [si], byte ptr 2Dh
  1850.         inc si
  1851.         dec cx
  1852.         inc di
  1853.     copyInt:
  1854.         cmp cx, 1
  1855.         je numStart
  1856.         cmp byte ptr[di], 0
  1857.         jne numStart
  1858.         cmp [shortNumFlag], 0
  1859.         je numShort
  1860.         numStart:
  1861.             mov [shortNumFlag], 1
  1862.             mov al, byte ptr[di]
  1863.             mov [si], al
  1864.             inc si
  1865.         numShort: inc di
  1866.     loop copyInt
  1867.     xor cx,cx
  1868.     copyFloat:
  1869.         cmp cx, 9
  1870.         je copyDone
  1871.         mov al, [bx]
  1872.         mov [si], al
  1873.         inc si
  1874.         inc bx
  1875.         inc cx
  1876.     jmp copyFloat
  1877.     copyDone:
  1878.     pop bp
  1879.     ret
  1880. createAnswerArray endp
  1881.  
  1882. errorsHandler proc
  1883.     push bp
  1884.     mov bp, sp
  1885.     lea si, pos0
  1886.     _callDrawRectangle si
  1887.     add si, 8
  1888.     _callDrawRectangle si
  1889.     add si, 8
  1890.     _callDrawRectangle si
  1891.     add si, 8
  1892.     _callDrawRectangle si
  1893.     add si, 16
  1894.     _callDrawRectangle si
  1895.    
  1896.     lea si, pos1
  1897.     _callDrawRectangle si
  1898.     add si, 8
  1899.     _callDrawRectangle si
  1900.     add si, 16
  1901.     _callDrawRectangle si
  1902.     add si, 8
  1903.     _callDrawRectangle si
  1904.     add si, 8
  1905.     _callDrawRectangle si
  1906.    
  1907.     lea si, diagonal1
  1908.     _callDrawRectangle si
  1909.     add si, 8
  1910.     _callDrawRectangle si
  1911.     add si, 8
  1912.     _callDrawRectangle si
  1913.    
  1914.     lea si, pos2
  1915.     _callDrawRectangle si
  1916.     add si, 8
  1917.     _callDrawRectangle si
  1918.     add si, 16
  1919.     _callDrawRectangle si
  1920.     add si, 8
  1921.     _callDrawRectangle si
  1922.     add si, 8
  1923.     _callDrawRectangle si
  1924.    
  1925.     lea si, diagonal2
  1926.     _callDrawRectangle si
  1927.     add si, 8
  1928.     _callDrawRectangle si
  1929.     add si, 8
  1930.     _callDrawRectangle si
  1931.    
  1932.     lea si, pos3
  1933.     _callDrawRectangle si
  1934.     add si, 16
  1935.     _callDrawRectangle si
  1936.     add si, 8
  1937.     _callDrawRectangle si
  1938.     add si, 8
  1939.     _callDrawRectangle si
  1940.     add si, 8
  1941.     _callDrawRectangle si
  1942.     add si, 8
  1943.     _callDrawRectangle si
  1944.    
  1945.     lea si, pos4
  1946.     _callDrawRectangle si
  1947.     add si, 8
  1948.     _callDrawRectangle si
  1949.     add si, 16
  1950.     _callDrawRectangle si
  1951.     add si, 8
  1952.     _callDrawRectangle si
  1953.     add si, 8
  1954.     _callDrawRectangle si
  1955.    
  1956.     lea si, diagonal3
  1957.     _callDrawRectangle si
  1958.     add si, 8
  1959.     _callDrawRectangle si
  1960.     add si, 8
  1961.     _callDrawRectangle si
  1962.    
  1963.     lea si, pos5
  1964.     add si, 32
  1965.     _callDrawRectangle si
  1966.     add si, 24
  1967.     _callDrawRectangle si
  1968.     pop bp
  1969.     ret
  1970. errorsHandler endp
  1971.  
  1972. mov ax,4C00h
  1973. int 21h
  1974. end START
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement