Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. section .bss
  2. num1 resb 2
  3. num2 resb 2
  4. res resb 2
  5. op resb 2
  6.  
  7. section .data
  8. equal db ' = '
  9. nwln db 0xa
  10.  
  11. section .text
  12. global _start
  13.  
  14. _start:
  15. Loop:
  16. ; Read First Number
  17. mov eax, 3 ; System Call (Read)
  18. mov ebx, 0 ; File Descriptor (stdin)
  19. mov ecx, num1 ; Move data into 'num1'
  20. mov edx, 2
  21. int 0x80 ; Call Kernel
  22.  
  23. ; Check for '.' to end the loop
  24. mov al, [num1]
  25. cmp al, '.'
  26. je End
  27.  
  28. ; Read Operator
  29. mov eax, 3 ; System Call (Read)
  30. mov ebx, 0 ; File Descriptor (stdin)
  31. mov ecx, op ; Move data into 'op'
  32. mov edx, 2
  33. int 0x80 ; Call Kernel
  34.  
  35. ; Read Second Number
  36. mov eax, 3 ; System Call (Read)
  37. mov ebx, 0 ; File Descriptor (stdin)
  38. mov ecx, num2 ; Move data into 'num2'
  39. mov edx, 2
  40. int 0x80 ; Call Kernel
  41.  
  42. ; Check operator
  43. mov al, [op]
  44.  
  45. cmp al, '+'
  46. je Plus
  47. cmp al, '-'
  48. je Minus
  49. cmp al, '*'
  50. je Multiply
  51. cmp al, '/'
  52. je Divide
  53.  
  54. ;------------------------------------------------------------;
  55. Plus:
  56. call Addition
  57. jmp End
  58.  
  59. Addition:
  60. ; Move x and y to registry
  61. mov ax, [num1]
  62. sub ax, '0'
  63. mov bx, [num2]
  64. sub bx, '0'
  65.  
  66. ; Add ebx to eax
  67. add ax, bx
  68. add ax, '0'
  69.  
  70. ; Move value in eax to [res]
  71. mov [res], ax
  72. call Print
  73. ret
  74.  
  75. ;------------------------------------------------------------;
  76. Minus:
  77. call Subtraction
  78. jmp End
  79.  
  80. Subtraction:
  81. ; Move x and y to registry
  82. mov ax, [num1]
  83. sub ax, '0'
  84. mov bx, [num2]
  85. sub bx, '0'
  86.  
  87. ; Subtract ebx from eax
  88. sub ax, bx
  89. add ax, '0'
  90.  
  91. ; Move value in eax to [res]
  92. mov [res], ax
  93.  
  94. call Print
  95. ret
  96.  
  97. ;------------------------------------------------------------;
  98. Multiply:
  99. call Multiplication
  100. jmp End
  101.  
  102. Multiplication:
  103. ; Move x and y to registry
  104. mov ax, [num1]
  105. sub ax, '0'
  106. mov bx, [num2]
  107. sub bx, '0'
  108.  
  109. ; Multiply ax with bx
  110. mul bx
  111. add ax, '0'
  112.  
  113. ; Move value in eax to [res]
  114. mov [res], ax
  115.  
  116. call Print
  117. ret
  118.  
  119. ;------------------------------------------------------------;
  120. Divide:
  121. call Division
  122. ;jmp End
  123.  
  124. Division:
  125. ; Move x and y to registry
  126. mov al, [num1]
  127. sub al, '0'
  128. mov bl, [num2]
  129. sub bl, '0'
  130.  
  131. ; Multiply ax with bx
  132. div bl
  133. add al, '0'
  134.  
  135. ; Move value in eax to [res]
  136. mov [res], ax
  137.  
  138. call Print
  139. ret
  140.  
  141. ;------------------------------------------------------------;
  142. Print:
  143. ; Write First Number
  144. mov eax, 4 ; System Call (Write)
  145. mov ebx, 1 ; File Descriptor (stdout)
  146. mov ecx, num1 ; Write 'num1'
  147. mov edx, 2
  148. int 0x80 ; Call Kernel
  149. ; Write Operator
  150. mov eax, 4 ; System Call (Write)
  151. mov ebx, 1 ; File Descriptor (stdout)
  152. mov ecx, op ; Write 'op'
  153. mov edx, 2
  154. int 0x80 ; Call Kernel
  155. ; Write Second Number
  156. mov eax, 4 ; System Call (Write)
  157. mov ebx, 1 ; File Descriptor (stdout)
  158. mov ecx, num2 ; Write 'num2'
  159. mov edx, 1
  160. int 0x80 ; Call Kernel
  161. ; Write Equal Sign
  162. mov eax, 4 ; System Call (Write)
  163. mov ebx, 1 ; File Descriptor (stdout)
  164. mov ecx, equal ; Write 'equal'
  165. mov edx, 3
  166. int 0x80 ; Call Kernel
  167. ; Write Result
  168. mov eax, 4 ; System Call (Write)
  169. mov ebx, 1 ; File Descriptor (stdout)
  170. mov ecx, res ; Write 'res'
  171. mov edx, 1
  172. int 0x80 ; Call Kernel
  173. ;New Line
  174. mov eax, 4 ; System Call (Write)
  175. mov ebx, 1 ; File Descriptor (stdout)
  176. mov ecx, nwln ; Write ' '
  177. mov edx, 1
  178. int 0x80 ; Call Kernel
  179. jmp Loop
  180.  
  181. ;------------------------------------------------------------;
  182. End:
  183. mov eax,1 ; System Call (sys_exit)
  184. int 0x80 ; Call Kernel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement