Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. section .data
  2.  
  3. ; Messages
  4.  
  5. msg1 db 10,'-Calculator-',10,0
  6. lmsg1 equ $ - msg1
  7.  
  8. msg2 db 10,'Number 1: ',0
  9. lmsg2 equ $ - msg2
  10.  
  11. msg3 db 'Number 2: ',0
  12. lmsg3 equ $ - msg3
  13.  
  14. msg4 db 10,'1. Add',10,0
  15. lmsg4 equ $ - msg4
  16.  
  17. msg5 db '2. Subtract',10,0
  18. lmsg5 equ $ - msg5
  19.  
  20. msg6 db '3. Multiply',10,0
  21. lmsg6 equ $ - msg6
  22.  
  23. msg7 db '4. Divide',10,0
  24. lmsg7 equ $ - msg7
  25.  
  26. msg8 db 'Operation: ',0
  27. lmsg8 equ $ - msg8
  28.  
  29. msg9 db 10,'Result: ',0
  30. lmsg9 equ $ - msg9
  31.  
  32. msg10 db 10,'Invalid Option',10,0
  33. lmsg10 equ $ - msg10
  34.  
  35. nlinea db 10,10,0
  36. lnlinea equ $ - nlinea
  37.  
  38. section .bss
  39.  
  40. ; Spaces reserved for storing the values ​​provided by the user.
  41.  
  42. opc resb 2
  43. num1 resb 2
  44. num2 resb 2
  45. result resb 2
  46.  
  47. section .text
  48.  
  49. global _start
  50.  
  51. _start:
  52.  
  53. ; Print on screen the message 1
  54. mov eax, 4
  55. mov ebx, 1
  56. mov ecx, msg1
  57. mov edx, lmsg1
  58. int 80h
  59.  
  60. ; Print on screen the message 2
  61. mov eax, 4
  62. mov ebx, 1
  63. mov ecx, msg2
  64. mov edx, lmsg2
  65. int 80h
  66.  
  67. ; We get num1 value.
  68. mov eax, 3
  69. mov ebx, 0
  70. mov ecx, num1
  71. mov edx, 2
  72. int 80h
  73.  
  74. ; Print on screen the message 3
  75. mov eax, 4
  76. mov ebx, 1
  77. mov ecx, msg3
  78. mov edx, lmsg3
  79. int 80h
  80.  
  81. ; We get num2 value.
  82. mov eax, 3
  83. mov ebx, 0
  84. mov ecx, num2
  85. mov edx, 2
  86. int 80h
  87.  
  88. ; Print on screen the message 4
  89. mov eax, 4
  90. mov ebx, 1
  91. mov ecx, msg4
  92. mov edx, lmsg4
  93. int 80h
  94.  
  95. ; Print on screen the message 5
  96. mov eax, 4
  97. mov ebx, 1
  98. mov ecx, msg5
  99. mov edx, lmsg5
  100. int 80h
  101.  
  102. ; Print on screen the message 6
  103. mov eax, 4
  104. mov ebx, 1
  105. mov ecx, msg6
  106. mov edx, lmsg6
  107. int 80h
  108.  
  109. ; Print on screen the message 7
  110. mov eax, 4
  111. mov ebx, 1
  112. mov ecx, msg7
  113. mov edx, lmsg7
  114. int 80h
  115.  
  116. ; Print on screen the message 8
  117. mov eax, 4
  118. mov ebx, 1
  119. mov ecx, msg8
  120. mov edx, lmsg8
  121. int 80h
  122.  
  123. ; We get the option selected.
  124. mov ebx,0
  125. mov ecx,opc
  126. mov edx,2
  127. mov eax,3
  128. int 80h
  129.  
  130. mov ah, [opc] ; Move the selected option to the registry ah
  131. sub ah, '0' ; Convert from ascii to decimal
  132.  
  133. ; We compare the value entered by the user to know what operation to perform.
  134.  
  135. cmp ah, 1
  136. je add
  137. cmp ah, 2
  138. je subtract
  139. cmp ah, 3
  140. je multiply
  141. cmp ah, 4
  142. je divide
  143.  
  144. ; If the value entered by the user does not meet any of the above
  145. ; conditions then we show an error message and we close the program.
  146. mov eax, 4
  147. mov ebx, 1
  148. mov ecx, msg10
  149. mov edx, lmsg10
  150. int 80h
  151.  
  152. jmp exit
  153.  
  154. add:
  155. ; We keep the numbers in the registers eax and ebx
  156. mov eax, [num1]
  157. mov ebx, [num2]
  158.  
  159. ; Convert from ascii to decimal
  160. sub eax, '0'
  161. sub ebx, '0'
  162.  
  163. ; Add
  164. add eax, ebx
  165.  
  166. ; Conversion from decimal to ascii
  167. add eax, '0'
  168.  
  169. ; We move the result
  170. mov [result], eax
  171.  
  172. ; Print on screen the message 9
  173. mov eax, 4
  174. mov ebx, 1
  175. mov ecx, msg9
  176. mov edx, lmsg9
  177. int 80h
  178.  
  179. ; Print on screen the result
  180. mov eax, 4
  181. mov ebx, 1
  182. mov ecx, result
  183. mov edx, 1
  184. int 80h
  185.  
  186. ; We end the program
  187. jmp exit
  188.  
  189. subtract:
  190. ; We keep the numbers in the registers eax and ebx
  191. mov eax, [num1]
  192. mov ebx, [num2]
  193.  
  194. ; Convert from ascii to decimal
  195. sub eax, '0'
  196. sub ebx, '0'
  197.  
  198. ; Subtract
  199. sub eax, ebx
  200.  
  201. ; Conversion from decimal to ascii
  202. add eax, '0'
  203.  
  204. ; We move the result
  205. mov [result], eax
  206.  
  207. ; Print on screen the message 9
  208. mov eax, 4
  209. mov ebx, 1
  210. mov ecx, msg9
  211. mov edx, lmsg9
  212. int 80h
  213.  
  214. ; Print on screen the result
  215. mov eax, 4
  216. mov ebx, 1
  217. mov ecx, result
  218. mov edx, 1
  219. int 80h
  220.  
  221. ; We end the program
  222. jmp exit
  223.  
  224. multiply:
  225.  
  226. ; We store the numbers in registers ax and bx
  227. mov ax, [num1]
  228. mov bx, [num2]
  229.  
  230. ; Convert from ascii to decimal
  231. sub ax, '0'
  232. sub bx, '0'
  233.  
  234. ; Multiply. AL = AX x BX
  235. mul bx
  236.  
  237. ; Conversion from decimal to ascii
  238. add al, '0'
  239.  
  240. ; We move the result
  241. mov [result], al
  242.  
  243. ; Print on screen the message 9
  244. mov eax, 4
  245. mov ebx, 1
  246. mov ecx, msg9
  247. mov edx, lmsg9
  248. int 80h
  249.  
  250. ; Print on screen the result
  251. mov eax, 4
  252. mov ebx, 1
  253. mov ecx, result
  254. mov edx, 1
  255. int 80h
  256.  
  257. ; We end the program
  258. jmp exit
  259.  
  260. divide:
  261. ; IN THIS LABEL IS THE ERROR!
  262.  
  263. ; We store the numbers in registers ax and bx
  264. mov dx, 0
  265. mov ax, [num1]
  266. mov bx, [num2]
  267.  
  268. ; Convert from ascii to decimall
  269. sub ax, '0'
  270. sub bx, '0'
  271. ; Division. AX = DX:AX / BX
  272. div bx
  273.  
  274. ; Conversion from decimal to ascii
  275. add ax, '0'
  276. ; We move the result
  277. mov [result], ax
  278.  
  279. ; Print on screen the message 9
  280. mov eax, 4
  281. mov ebx, 1
  282. mov ecx, msg9
  283. mov edx, lmsg9
  284. int 80h
  285.  
  286. ; Print on screen the result
  287. ; ALWAYS PRINTS 1
  288. mov eax, 4
  289. mov ebx, 1
  290. mov ecx, result
  291. mov edx, 1
  292. int 80h
  293.  
  294. ; We end the program
  295. jmp exit
  296.  
  297. exit:
  298. ; Print on screen two new lines
  299. mov eax, 4
  300. mov ebx, 1
  301. mov ecx, nlinea
  302. mov edx, lnlinea
  303. int 80h
  304. ; End the program
  305. mov eax, 1
  306. mov ebx, 0
  307. int 80h
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement