cmk20

Calculator

Jul 13th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. ;Calculator V1.5 By cmk20
  2. ;The functions resetTerminal, newLine, print, and divide we writen by SopaXorzTaker (Library link: https://gist.github.com/SopaXorzTaker/26fcc318bdc3781727ba748ccb3194ff)
  3. ;The dividing function is currently bugged... not sure why
  4. ;
  5.  
  6. ; Initialize the Stack Pointer
  7. mov sp, 0x300
  8. mov bx, 0 ;The term port
  9. jmp main
  10.  
  11. ; Resets the terminal to look pretty
  12. resetTerminal:
  13. push ax
  14. push cx
  15. mov ax, 0x1000
  16. send 0, ax
  17. mov ax, 0x208A
  18. send 0, ax
  19. mov ax, 0x3000
  20. mov cx, 24
  21. .loop:
  22. send 0, ax
  23. sub cx, 1
  24. cmp cx, 0
  25. jne .loop
  26.  
  27. pop cx
  28. pop ax
  29. ret
  30.  
  31. ; Scrolls the text down.
  32. newLine:
  33. send bx, 0x3000
  34. send bx, 0x1000
  35. ret
  36.  
  37. ; Prints a byte (not word!) string.
  38. ; DX - the starting word of the string.
  39. print:
  40. push ax
  41. push bx
  42. push cx
  43. .loop:
  44. mov ax, [dx]
  45. test ax, ax
  46. jz .end
  47. mov bx, ax
  48. mov cx, ax
  49.  
  50. shr bx, 8
  51. test bx, bx
  52. jz .end
  53. send 0, bx
  54.  
  55. and cx, 0xFF
  56. test cx, cx
  57. jz .end
  58. send 0, cx
  59. add dx, 1
  60. jmp .loop
  61.  
  62. .end:
  63. pop ax
  64. pop bx
  65. pop cx
  66. ret
  67.  
  68. ; Prints a number in decimal.
  69. ; AX - the number to print.
  70. printInt:
  71. push ax
  72. push bx
  73. push cx
  74. push dx
  75. push ex
  76.  
  77. mov bx, 0
  78. mov cx, 0
  79. mov dx, 0
  80. mov ex, 0
  81.  
  82. .k10:
  83. cmp ax, 10000
  84. jl .k
  85. sub ax, 10000
  86. add ex, 1
  87. jmp .k10
  88. .k:
  89. cmp ax, 1000
  90. jl .h
  91. sub ax, 1000
  92. add dx, 1
  93. jmp .k
  94. .h:
  95. cmp ax, 100
  96. jl .t
  97. sub ax, 100
  98. add cx, 1
  99. jmp .h
  100. .t:
  101. cmp ax, 10
  102. jl .end
  103. sub ax, 10
  104. add bx, 1
  105. jmp .t
  106.  
  107. .end:
  108. test ex, ex
  109. jnz .digits
  110. test dx, dx
  111. jnz .skip1
  112. test cx, cx
  113. jnz .skip2
  114. test bx, bx
  115. jnz .skip3
  116. jmp .skip4
  117.  
  118. .digits:
  119. mov ex, [ex+decimals]
  120. send 0, ex
  121. .skip1:
  122. mov ex, [dx+decimals]
  123. send 0, ex
  124. .skip2:
  125. mov ex, [cx+decimals]
  126. send 0, ex
  127. .skip3:
  128. mov ex, [bx+decimals]
  129. send 0, ex
  130. .skip4:
  131. mov ex, [ax+decimals]
  132. send 0, ex
  133.  
  134. pop ex
  135. pop dx
  136. pop cx
  137. pop bx
  138. pop ax
  139. ret
  140.  
  141.  
  142. ; Divide a whole number. In: AX - dividend, BX - divisor
  143. ; Out: AX - quotient, BX - remainder
  144. divide:
  145. push cx
  146.  
  147. mov cx, 0
  148.  
  149. test bx, bx
  150. jz .end
  151.  
  152. .loop:
  153. cmp ax, bx
  154. jl .end
  155. sub ax, bx
  156. add cx, 1
  157. jmp .loop
  158.  
  159. .end:
  160. mov bx, ax
  161. mov ax, cx
  162.  
  163. pop cx
  164. ret
  165.  
  166. ;Multiply a number "ax" by "cx"
  167. multiply:
  168. push bx
  169. mov bx, ax ;Store ax's starting value
  170.  
  171. loop:
  172. cmp cx, 1
  173. je .end ;when cx is 1 the multiplication is compleate
  174. add ax, bx ;Add ax to it's starting state
  175. sub cx, 1
  176. jmp loop
  177.  
  178. .end:
  179. pop bx
  180. ret
  181.  
  182. ;Imput a number and save it to ax
  183. inputNum:
  184. push cx
  185. mov ax, 0
  186. send bx, 0x3E ;Print a ">"
  187.  
  188. .loop:
  189. mov cx, 10 ;Set multiplyer to 10
  190. recv dx, bx ;Recv the char
  191. cmp dx, 10 ;If the char is ENTER
  192. je .end ;End the input
  193. sub dx, 48 ;Convert the ascii to a number
  194. call multiply ;Multiply the current number value in ax by ten
  195. add ax, dx ;Add the newly inputed number to ax
  196. push ax
  197. mov ax, dx
  198. call printInt ;Print the newly inputed number
  199. pop ax
  200. jmp .loop
  201.  
  202. .end:
  203. pop cx
  204. ret
  205.  
  206. ;Resets the display for the fist screen (It just displays some strings)
  207. mainMenuReset:
  208. mov ax, 0 ;Reset the registers (for a clean start if the program loops)
  209. mov bx, 0
  210. mov cx, 0
  211. mov dx, 0
  212. mov ex, 0
  213. call resetTerminal
  214. mov dx, info
  215. call print
  216. send bx, 0x1041
  217. mov dx, op1
  218. call print
  219. send bx, 0x1061
  220. mov dx, op2
  221. call print
  222. send bx, 0x1081
  223. mov dx, op3
  224. call print
  225. send bx, 0x10A1
  226. mov dx, op4
  227. call print
  228. send bx, 0x10E1
  229. mov dx, op5
  230. call print
  231. send bx, 0x1101
  232. ret
  233.  
  234. main:
  235. call mainMenuReset ;Reset the menu
  236. call inputNum ;Get the option
  237. cmp ax, 1 ;Then select a option
  238. je adding
  239. cmp ax, 2
  240. je subtracting
  241. cmp ax, 3
  242. je multiplying
  243. cmp ax, 4
  244. je dividing
  245. jmp main ;If the number is not 1 thru 4 loop back to main
  246.  
  247. ;Adding menu
  248. adding:
  249. call resetTerminal ;Reset the term and display some text
  250. mov dx, op1
  251. call print
  252. send bx, 0x1041
  253. mov dx, mOp1
  254. call print
  255. send bx, 0x1061
  256. call inputNum ;Get the first number
  257. mov ex, ax ;Store it in ex temporarily
  258. send bx, 0x1081
  259. mov dx, mOp2
  260. call print
  261. send bx, 0x10A1
  262. call inputNum ;Get the second number
  263. add ex, ax ;Add them together
  264. send bx, 0x10E1
  265. mov dx, answer
  266. call print
  267. send bx, 0x1161
  268. mov ax, ex ;Move the answer to ax then print it
  269. call printInt
  270. send bx, 0x12E1
  271. mov dx, exit
  272. call print
  273. recv dx, 0 ;Wait for a button press then go back to main
  274. jmp main
  275.  
  276. ;Same as adding except we use sub instead of add
  277. subtracting:
  278. call resetTerminal
  279. mov dx, op2
  280. call print
  281. send bx, 0x1041
  282. mov dx, mOp1
  283. call print
  284. send bx, 0x1061
  285. call inputNum
  286. mov ex, ax
  287. send bx, 0x1081
  288. mov dx, mOp2
  289. call print
  290. send bx, 0x10A1
  291. call inputNum
  292. sub ex, ax
  293. send bx, 0x10E1
  294. mov dx, answer
  295. call print
  296. send bx, 0x1161
  297. mov ax, ex
  298. call printInt
  299. send bx, 0x12E1
  300. mov dx, exit
  301. call print
  302. recv dx, 0
  303. jmp main
  304.  
  305. ;Similar to adding however we need to move some registers around to use the multiply function properly
  306. multiplying:
  307. call resetTerminal
  308. mov dx, op3
  309. call print
  310. send bx, 0x1041
  311. mov dx, mOp1
  312. call print
  313. send bx, 0x1061
  314. call inputNum
  315. mov cx, ax ;Put input in cx instead of ex
  316. send bx, 0x1081
  317. mov dx, mOp2
  318. call print
  319. send bx, 0x10A1
  320. call inputNum
  321. xchg ax, cx ;Exchange ex and cx's values to make sure you are multpling number 1 buy number 2
  322. call multiply
  323. mov ex, ax ;Put the result in ex
  324. send bx, 0x10E1
  325. mov dx, answer
  326. call print
  327. send bx, 0x1161
  328. mov ax, ex
  329. call printInt ;Print it
  330. send bx, 0x12E1
  331. mov dx, exit
  332. call print
  333. recv dx, 0
  334. jmp main
  335.  
  336. ;Similar to multiply but it's broken :P
  337. dividing:
  338. call resetTerminal
  339. mov dx, op4
  340. call print
  341. send bx, 0x1041
  342. mov dx, mOp1
  343. call print
  344. send bx, 0x1061
  345. call inputNum
  346. mov cx, ax ;Store the first number in cx, again
  347. send bx, 0x1081
  348. mov dx, mOp2
  349. call print
  350. send bx, 0x10A1
  351. call inputNum
  352. mov bx, cx ;Put the fist number in bx (Needed for the divide function)
  353. xchg ax, bx ;Exchnage the values
  354. call divide
  355. mov cx, bx ;Put the remainder in cx (The quotient is already in ax)
  356. mov bx, 0
  357. send bx, 0x10E1
  358. mov dx, answer
  359. call print
  360. send bx, 0x1101
  361. call printInt ;Print The quotient
  362. send bx, 0x1121
  363. mov dx, dividingAns
  364. call print
  365. send bx, 0x1161
  366. mov ax, cx
  367. call printInt ;Print the remainder
  368. send bx, 0x12E1
  369. mov dx, exit
  370. call print
  371. recv dx, 0
  372. jmp main
  373.  
  374. info: db "Calculator V1.5 By Cmk20", 0
  375. op1: db "1 - Addition", 0
  376. op2: db "2 - Subtraction", 0
  377. op3: db "3 - Multiplication", 0
  378. op4: db "4 - Division (Broken :P)", 0
  379. op5: db "Enter 1, 2, 3, Or 4:", 0
  380. exit: db "Press Any Key To Exit", 0
  381. answer: db "The Answer Is:", 0
  382. dividingAns: db "The Remainder Is:", 0
  383. mOp1: db "Enter First Number:", 0
  384. mOp2: db "Enter Second Number:", 0
  385. decimals: dw "0123456789ABCDEF"
Advertisement
Add Comment
Please, Sign In to add comment