add.asm 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. SYS_EXIT equ 1
  2. SYS_READ equ 3
  3. SYS_WRITE equ 4
  4. STDIN equ 0
  5. STDOUT equ 1
  6. segment .data
  7. msg1 db "Enter a digit ", 0xA,0xD
  8. len1 equ $- msg1
  9. msg2 db "Please enter a second digit", 0xA,0xD
  10. len2 equ $- msg2
  11. msg3 db "The sum is: "
  12. len3 equ $- msg3
  13. segment .bss
  14. num1 resb 2
  15. num2 resb 2
  16. res resb 1
  17. section .text
  18. global _start ;must be declared for using gcc
  19. _start: ;tell linker entry point
  20. mov eax, SYS_WRITE
  21. mov ebx, STDOUT
  22. mov ecx, msg1
  23. mov edx, len1
  24. int 0x80
  25. mov eax, SYS_READ
  26. mov ebx, STDIN
  27. mov ecx, num1
  28. mov edx, 2
  29. int 0x80
  30. mov eax, SYS_WRITE
  31. mov ebx, STDOUT
  32. mov ecx, msg2
  33. mov edx, len2
  34. int 0x80
  35. mov eax, SYS_READ
  36. mov ebx, STDIN
  37. mov ecx, num2
  38. mov edx, 2
  39. int 0x80
  40. mov eax, SYS_WRITE
  41. mov ebx, STDOUT
  42. mov ecx, msg3
  43. mov edx, len3
  44. int 0x80
  45. ; moving the first number to eax register and second number to ebx
  46. ; and subtracting ascii '0' to convert it into a decimal number
  47. mov eax, [num1]
  48. sub eax, '0'
  49. mov ebx, [num2]
  50. sub ebx, '0'
  51. ; add eax and ebx
  52. add eax, ebx
  53. ; add '0' to to convert the sum from decimal to ASCII
  54. add eax, '0'
  55. ; storing the sum in memory location res
  56. mov [res], eax
  57. ; print the sum
  58. mov eax, SYS_WRITE
  59. mov ebx, STDOUT
  60. mov ecx, res
  61. mov edx, 1
  62. int 0x80
  63. exit:
  64. mov eax, SYS_EXIT
  65. xor ebx, ebx
  66. int 0x80