fork download
  1. %macro IO 4
  2. mov rax,%1
  3. mov rdi,%2
  4. mov rsi,%3
  5. mov rdx,%4
  6. syscall
  7. %endmacro
  8. section .data
  9. m1 db "enter choice (+,-,*, /)" ,10 ; 10d -> line feed
  10. l1 equ $-m1
  11. m2 db "Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /) using suitable macros. Define procedure for each operation." ,10
  12. l2 equ $-m2
  13. m3 db "rahul ghosh 3236" ,10
  14. l3 equ $-m3
  15. madd db "addition here" ,10
  16. l4 equ $-madd
  17. msub db "subtraction here" ,10
  18. l5 equ $-msub
  19. mmul db "multiplication here" ,10
  20. l6 equ $-mmul
  21. mdiv db "division here" ,10
  22. l7 equ $-mdiv
  23. mspace db 10
  24. m_result db "result is "
  25. m_result_l equ $-m_result
  26. m_qou db "qoutient is "
  27. m_qou_l equ $-m_qou
  28. m_rem db "remainder is "
  29. m_rem_l equ $-m_rem
  30. m_default db "enter correct choice",10
  31. m_default_l equ $-m_default
  32. section .bss
  33. choice resb 2
  34. _output resq 1
  35. _n1 resq 1
  36. _n2 resq 1
  37. temp_1 resq 1
  38. temp_2 resq 1
  39. section .text
  40. global _start
  41. _start:
  42. IO 1,1,m2,l2
  43. IO 1,1,m3,l3
  44. IO 1,1,m1,l1
  45. IO 0,0,choice,2
  46. cmp byte [choice],'+'
  47. jne case2
  48. call add_fun
  49. jmp exit
  50. case2:
  51. cmp byte [choice],'-'
  52. jne case3
  53. call sub_fun
  54. jmp exit
  55. case3:
  56. cmp byte [choice],'*'
  57. jne case4
  58. call mul_fun
  59. jmp exit
  60. case4:
  61. cmp byte [choice],'/'
  62. jne case5
  63. call div_fun
  64. jmp exit
  65. case5:
  66. cmp byte [choice],'a'
  67. jne error
  68. call add_fun
  69. call sub_fun
  70. call mul_fun
  71. call div_fun
  72. jmp exit
  73. error:
  74. IO 1,1,m_default,m_default_l
  75. jmp exit
  76. mov rax, 60
  77. mov rdi, 0
  78. syscall
  79. add_fun:
  80. IO 1,1,madd,l4
  81. mov qword[_output],0
  82. IO 0,0,_n1,17
  83. IO 1,1,_n1,17
  84. call ascii_to_hex
  85. add qword[_output],rbx
  86. IO 0,0,_n1,17
  87. IO 1,1,_n1,17
  88. call ascii_to_hex
  89. add qword[_output],rbx
  90. mov rbx,[_output]
  91. IO 1,1,mspace,1
  92. IO 1,1,m_result,m_result_l
  93. call hex_to_ascii
  94. ret
  95. sub_fun:
  96. IO 1,1,msub,l5
  97. mov qword[_output],0
  98. IO 0,0,_n1,17
  99. IO 1,1,_n1,17
  100. ;IO 1,1,mspace,1
  101. call ascii_to_hex
  102. add qword[_output],rbx
  103. IO 0,0,_n1,17
  104. IO 1,1,_n1,17
  105. ;IO 1,1,mspace,1
  106. call ascii_to_hex
  107. sub qword[_output],rbx
  108. mov rbx,[_output]
  109. IO 1,1,mspace,1
  110. IO 1,1,m_result,m_result_l
  111. call hex_to_ascii
  112.  
  113. ret
  114. mul_fun:
  115. IO 1,1,mmul,l6 ; message
  116. IO 0,0,_n1,17 ; n1 input
  117. IO 1,1,_n1,17
  118. call ascii_to_hex; conversion returns hex value in rbx
  119. mov [temp_1],rbx ; storing hex in temp_1
  120. IO 0,0,_n1,17 ;n2 input
  121. IO 1,1,_n1,17
  122. call ascii_to_hex
  123. mov [temp_2],rbx ; putting hex of n2 in temp_2
  124. mov rax,[temp_1] ; temp_1->rax
  125. mov rbx,[temp_2] ;temp_2->rbx
  126. mul rbx ; multiplication
  127. push rax
  128. push rdx
  129. IO 1,1,mspace,1
  130. IO 1,1,m_result,m_result_l
  131. pop rdx
  132. mov rbx,rdx; setting rbx value for conversion
  133. call hex_to_ascii
  134. pop rax
  135. mov rbx,rax; setting rbx value for conversion
  136. call hex_to_ascii ; final output
  137. ret
  138. div_fun:
  139. IO 1,1,mdiv,l7
  140. IO 0,0,_n1,17 ; n1 input
  141. IO 1,1,_n1,17
  142. call ascii_to_hex; conversion returns hex value in rbx
  143. mov [temp_1],rbx ; storing hex in temp_1
  144. IO 0,0,_n1,17 ;n2 input
  145. IO 1,1,_n1,17
  146. call ascii_to_hex
  147. mov [temp_2],rbx ; putting hex of n2 in temp_2
  148. mov rax,[temp_1] ; temp_1->rax
  149. mov rbx,[temp_2] ;temp_2->rbx
  150. xor rdx,rdx
  151. mov rax,[temp_1] ; temp_1->rax
  152. mov rbx,[temp_2] ; temp_2->rbx
  153. div rbx ; div
  154. push rax
  155. push rdx
  156. IO 1,1,mspace,1
  157. IO 1,1,m_rem,m_rem_l
  158. pop rdx
  159. mov rbx,rdx
  160. call hex_to_ascii; remainder output
  161. IO 1,1,mspace,1
  162. IO 1,1,m_qou,m_qou_l
  163. pop rax
  164. mov rbx,rax
  165. call hex_to_ascii; quotient output
  166. ret
  167. ascii_to_hex:
  168. mov rsi, _n1
  169. mov rcx, 16
  170. xor rbx, rbx
  171. next1:
  172. rol rbx, 4
  173. mov al, [rsi]
  174. cmp al,47h
  175. jge error
  176. cmp al, 39h
  177. jbe sub30h
  178. sub al, 7
  179. sub30h:
  180. sub al, 30h
  181. add bl, al
  182. inc rsi
  183. loop next1
  184. ret
  185. hex_to_ascii:
  186. mov rcx, 16
  187. mov rsi,_output
  188. next2:
  189. rol rbx, 4
  190. mov al, bl
  191. and al, 0Fh
  192. cmp al, 9
  193. jbe add30h
  194. add al, 7
  195. add30h:
  196. add al, 30h
  197. mov [rsi], al
  198. inc rsi
  199. loop next2
  200. IO 1,1,_output,16
  201. IO 1,1,mspace,1
  202. ret
Success #stdin #stdout 0s 5692KB
stdin
+
1
stdout
Write a switch case driven X86/64 ALP to perform 64-bit hexadecimal arithmetic operations (+,-,*, /) using suitable macros. Define procedure for each operation.
rahul ghosh 3236
enter choice (+,-,*, /)
addition here
1
1

result is CFBBBBBBBBBBBBA0