Advertisement
Guest User

Untitled

a guest
May 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. MSG1 DB "x = ","$" ; string to display for (1)
  2. MSG2 DB "y = ","$" ; string to display for (1)
  3. MSG3 DB 0DH,0AH,"x+y = ","$" ; string for sum format
  4. MSG4 DB 0DH,0AH,"x-y = ","$" ; string for sub format
  5. MSG5 DB 0DH,0AH,"Error","$" ; error display message
  6. START: ; initialize index
  7. MOV DI,00H ; keep position for XLAT
  8. MOV BX,200H
  9. MOV SI,04H
  10. READ_HEX_NUMBER:
  11. MOV DH,02H ; counter for pair of 2HEX remaining
  12. READ_FIRST:
  13. MOV AH,08H
  14. INT 21H
  15. CMP AL,0DH ; if ENTER is pressed
  16. JE ERROR ; then goto ERROR
  17. CMP AL,30H ; if digit is valid then read again
  18. JL READ_FIRST
  19. CMP AL,39H ; if digit is valid the goto STORE_NUM
  20. JLE STORE_NUM1
  21. CMP AL,41H
  22. JL READ_FIRST
  23. CMP AL,46H
  24. JLE STORE_NUM1
  25. CMP AL,61H
  26. JL READ_FIRST
  27. CMP AL,66H
  28. JG READ_FIRST
  29.  
  30. STORE_NUM1: ; stores digit in array
  31. CMP AL,60H
  32. JLE SKIP_LINE
  33. SUB AL,20H
  34. SKIP_LINE:
  35. MOV [BX+DI],AL ; increase counter of array position
  36. INC DI
  37. READ_SECOND:
  38.  
  39. MOV AH,08H ; same procedure as for first digit
  40. INT 21H
  41. CMP AL,0DH ; if ENTER is pressed
  42. JE ERROR ; then goto ERROR
  43. CMP AL,30H
  44. JL READ_SECOND
  45. CMP AL,39H
  46. JLE CONTINUE
  47. CMP AL,41H
  48. JL READ_SECOND
  49. CMP AL,46H
  50. JLE CONTINUE
  51. CMP AL,61H
  52. JL READ_SECOND
  53. CMP AL,66H
  54. JG READ_SECOND
  55.  
  56.  
  57. CONTINUE: ; store second digit in array
  58. CMP AL,60H
  59. JLE SKIP_LINE2
  60. SUB AL,20H
  61. SKIP_LINE2:
  62. MOV [BX+DI],AL ; increase counter of array position
  63. INC DI
  64. DEC DH ; decrease remaining numbers left to read
  65. CMP DH,00H ; if 2 numbers got read then continue
  66. JNZ READ_FIRST ; else read 2nd number
  67. MOV DX, offset MSG1 ; display string MSG1
  68. MOV AH,09H
  69. INT 21H
  70. MOV AL,00H ; display 1st digit of 1st number
  71. XLAT
  72. MOV DL,AL
  73. MOV AH,02H
  74. INT 21H
  75. MOV AL,01H ; display 2nd digit of 1st number
  76. XLAT
  77. MOV DL,AL
  78. MOV AH,02H
  79. INT 21H
  80. MOV DL,20H ; print "space" character
  81. MOV AH,02H
  82. INT 21H
  83. MOV DX, offset MSG2 ; display string MSG2
  84. MOV AH,09H
  85. INT 21H
  86. MOV AL,02H ; display 1st digit of 2nd number
  87. XLAT
  88. MOV DL,AL
  89. MOV AH,02H
  90. INT 21H
  91. MOV AL,03H ; display 2nd digit of 2nd number
  92. XLAT
  93. MOV DL,AL
  94. MOV AH,02H
  95. INT 21H
  96. MOV AH,08H
  97. INT 21H
  98. CMP AL,0DH ; if ENTER is pressed after 2 numbers
  99. JE PRINT_SUM_SUB ; then print sum and difference of numbers
  100. ; else if there are more than 2 numbers
  101. WAIT_FOR_ENTER: ; then wait for ENTER to display error message
  102. MOV AH,08H
  103. INT 21H
  104. CMP AL,0DH
  105. JE ERROR
  106. JMP WAIT_FOR_ENTER
  107.  
  108. ERROR:
  109. MOV DX, offset MSG5 ; display "Error" message on screen
  110. MOV AH,09H
  111. INT 21H
  112. JMP FINISH
  113. PRINT_SUM_SUB:
  114. MOV AL,00H
  115. XLAT
  116. CALL HEXPROD ; call hexprod to keep values for hex numbers
  117. MOV CL,04H ; define shift value for ROL
  118. MOV DL,CH
  119. ROL DL,CL ; take first ASCII value and convert it
  120. MOV AL,01H
  121. XLAT
  122. CALL HEXPROD
  123. MOV CL,04H
  124. ADD DL,CH ; take second ASCII code to complete first number
  125. MOV AL,02H
  126. XLAT
  127. CALL HEXPROD
  128. MOV CL,04H
  129. ADD DH,CH
  130. ROL DH,CL
  131. MOV AL,03H ; start translating second hex number
  132. XLAT
  133. CALL HEXPROD
  134. MOV CL,04H
  135. ADD DH,CH
  136. MOV BL,DH
  137. MOV BH,00H ;first number saved in BX
  138. MOV DH,00H ;second number saved in DX
  139. MOV CX,BX
  140. ADD BX,DX
  141. MOV AX,BX
  142. CALL HEX_TO_DECIMAL
  143. AND DX,00FFH
  144. SUB DX,CX
  145. MOV CX,DX
  146. MOV DX, offset MSG3 ; display MSG3 string
  147. MOV AH,09H
  148. INT 21H
  149. MOV BX,300H ; take from array output values
  150. MOV AL,00H
  151. XLAT
  152. CMP AL,00H ; if left most digit is zero then dont display it
  153. JE SKIP1 ; and move to 2nd digit
  154. ADD AL,30H
  155. MOV DL,AL
  156. MOV AH,02H
  157. INT 21H
  158. JMP NEXT_NUM2
  159. SKIP1: ; if both 1st digit and 2nd digit is zero
  160. MOV AL,01H ; then dont display it and move to 3rd digit
  161. XLAT
  162. CMP AL,00H
  163. JE SKIP7
  164. NEXT_NUM2:
  165. MOV AL,01H
  166. XLAT
  167. CMP AL,00H
  168. ADD AL,30H
  169. MOV DL,AL
  170. MOV AH,02H
  171. INT 21H
  172. SKIP7:
  173. MOV AL,02H
  174. XLAT
  175. ADD AL,30H
  176. MOV DL,AL
  177. MOV AH,02H
  178. INT 21H
  179. MOV DX, offset MSG4 ; display MSG4 string
  180. MOV AH,09H
  181. INT 21H
  182. CMP CX,00H
  183. JGE CONT3 ; if result < 0
  184. NEG CX ; convert to positive
  185. MOV DL,2DH ; output minus sign
  186. MOV AH,02H
  187. INT 21H
  188. CONT3:
  189. MOV AX,CX
  190. CALL HEX_TO_DECIMAL ; same logic as before to print the sub
  191. MOV BX,300H
  192. MOV AL,00H
  193. XLAT
  194. CMP AL,00H
  195. JE SKIP5
  196. ADD AL,30H
  197. MOV DL,AL
  198. MOV AH,02H
  199. INT 21H
  200. JMP NEXT_NUM1
  201. SKIP5:
  202. MOV AL,01H
  203. XLAT
  204. CMP AL,00H
  205. JE SKIP6
  206. NEXT_NUM1:
  207. MOV AL,01H
  208. XLAT
  209. ADD AL,30H
  210. MOV DL,AL
  211. MOV AH,02H
  212. INT 21H
  213. SKIP6:
  214. MOV AL,02H
  215. XLAT
  216. ADD AL,30H
  217. MOV DL,AL
  218. MOV AH,02H
  219. INT 21H
  220. FINISH:
  221. MOV AH,02H
  222. MOV DL,0AH ; print "\n" character
  223. INT 21H
  224. MOV AH,02H
  225. MOV DX,0DH ; print carriage return character
  226. INT 21H
  227. MOV AH,02H
  228. MOV DL,0AH ; print "\n" character
  229. INT 21H
  230. MOV AH,02H
  231. MOV DX,0DH ; print carriage return character
  232. INT 21H
  233. JMP START
  234.  
  235. HEX_TO_DECIMAL: ; convert HEX number to decimal
  236. PUSH BX
  237. MOV SI,00H
  238. MOV DH,64H
  239. MOV BX,300H ; store each decimal digit in array
  240. DIV DH ; divide by 100 to get the right most digit as remainder
  241. MOV [BX+SI],AL
  242. AND AX,0FF00H
  243. XCHG AH,AL
  244. MOV DH,0AH ; divide by 10 the rest of the previous division
  245. DIV DH ; to get the left most digit as quotient
  246. INC SI ; and the middle digit as remainder
  247. MOV [BX+SI],AL
  248. INC SI
  249. MOV [BX+SI],AH
  250. POP BX
  251. RET
  252. HEXPROD: ; convert ASCII to HEX
  253. MOV CL,AL ; CL contains the value of ASCII
  254. MOV CH,00H ; CH contains the value of HEX -> result
  255. MOV AL,30H
  256. HEXSTART:
  257. CMP AL,CL
  258. JE HEXFOUND
  259. INC CH
  260. INC AL
  261. JNC SKIP0
  262. CMC
  263. SKIP0:
  264. DAA
  265. CMP AL,40H
  266. JNE AVOID
  267. INC AL
  268. JNC SKIP3
  269. CMC
  270. SKIP3:
  271. DAA
  272. AVOID:
  273. JMP HEXSTART
  274. HEXFOUND:
  275. RET
  276.  
  277. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement